Getting an error at frame.add(game);:
Multiple markers at this line
- Debug Current Instruction Pointer
- The method add(Component) in the type Container is not applicable for the arguments
(Display)
My code:
import java.awt.Canvas;
import java.awt.Component;
import javax.swing.JFrame;
public class Display {
public static final int WIDTH = 800;
public static final int HEIGHT = 600;
public static void main(String[] args){
Display game = new Display();
JFrame frame = new JFrame();
frame.add(game);
frame.setSize(WIDTH, HEIGHT);
frame.setResizable(false);
frame.setVisible(true);
}
}
Your class Display should extend a Component (Container, Button, Canvas, Label ...). I think you would like to extend JPanel which is the most common, but it really depends on what Display class is intended to do:
public class Display extends JPanel {
}
Your Display should extend JPanel or some other Component as mentioned by the other answer.
For your purpose you should also override the paintComponent(Graphics g) method when you are ready to draw something on the Displayand have a constructor if your going to use it as a Component.
Related
I'm trying to make a test window with some text on it, when I run my code, it doesn't draw the string. I specified the color for it. Can anybody help me with this?
import javax.swing.*;
import java.awt.*;
class Main
{
public static void main(String[] args) {
DrawFrame f = new DrawFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
class DrawFrame extends JFrame
{
public DrawFrame(){
setTitle("For Aylin");
setSize(1280,720);
DrawPanel panel = new DrawPanel();
Container cp = getContentPane();
cp.add(panel);
}
}
class DrawPanel extends JPanel
{
public void paintComponents(Graphics g)
{
super.paintComponents(g);
g.setColor(Color.darkGray);
g.drawString("Hi", 100, 10);
}
}
You should override the JPanel's paintComponent method not its paintComponents method as they are for two very different purposes. The first paints the component itself (what you want) while the second gets the child components held by this parent to paint themselves.
Also remember to change the super call so that it matches, and to use the #Override annotation above the method.
So if I use this code, I get a screen with nothing. I should display a green rectangle. Had this problem previously but couldn't solve it.
package _47b3n.squaregen;
import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import javax.swing.JFrame;
public class Main extends Component {
private static final long serialVersionUID = 5547487570978675247L;
public static void main(String [] args) {
new Main();
}
public Main() {
JFrame frame = new JFrame();
frame.setSize(200, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setVisible(true);
repaint();
}
public void render(Graphics g) {
g.setColor(Color.GREEN);
g.drawRect(10,10,10,10);
}
}
Custom painting is done by overriding the paintComponent() method of a JPanel and then you add the panel to the frame.
Read the section from the Swing tutorial on Custom Painting for more information and working examples.
I am trying to move from a static main() to a non static method called paintComponent(), but the problem I am having is I can not move from Static to Non static in the way which I have. The class is a follows, where hunter and hunted are external classes:
import javax.swing.JFrame;
import java.awt.Graphics;
public class Main extends JFrame{ //Public class: Available for all other classes to refer to
private static final long serialVersionUID = -4511248732627763442L;
public static void main(String[] args){
frame();
repaint();
move(); //Passes to the method move() in the class Main()
}
public static JFrame frame(){
JFrame frame = new JFrame("Hunter VS Hunted"); //Sets the window title
frame.setExtendedState(JFrame.MAXIMIZED_BOTH); //Sets the size of the window
frame.setVisible(true); //Says to display the window
frame.setResizable(false); //Sets it so the screen cannot be adjusted
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Closes the window when the x is pushed
System.out.println("Frame works.");
return frame;
}
public void paintComponent(Graphics g){
super.paintComponents(g); //Assigns a graphics value to g, so that it can be passed to other methods
Hunted.paint(g);
Hunter.paint(g);
System.out.println("Main.paintComponent works.");
}
public static void move(){
Hunter.move(); //Passes to move() in the Hunter class
Hunted.move(); //Passes to move() in the Hunter class
}
}
Bear in mind I am a beginner, so please try to keep it simple!
You need to call repaint and paintComponents using an object.
JFrame frame = frame();
frame.repaint();
move();
Every non-static method needs to be called from an object. Static methods belongs to the class, so you can call them without an object.
repaint and paintComponents belongs to the JFrame object. They are not static and so they need to be called using an object (repaint will call paintComponents).
Your 'frame()' method will return a JFrame object. So you can call the repaint() method using the JFrame object that is returned from the 'frame()' method.
Having said that, I'm not sure what you are trying to accomplish in your code, and even if my explanation will resolve a Compilation-Error, without further elaboration, it might not accomplish what you are trying to achieve.
you have got some messed up code! but let me clean it up a bit, make it more presentable. However it is up to you to get it to work now. I have created another class for your jframe and adjusted your extra / repetitive code in the constractor.
btw i know nothing about haunted and haunter.
import java.awt.Container;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Main {
public static void main(String[] args){
myFrame x = new myFrame("Hunter VS Hunted");
x.ui(x.getContentPane());
}
}
class myFrame extends JFrame {
public myFrame(String name){
// constractor
super(name); //Sets the window title
setExtendedState(JFrame.MAXIMIZED_BOTH); //Sets the size of the window
setVisible(true); //Says to display the window
setResizable(false); //Sets it so the screen cannot be adjusted
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Closes the window when the x is pushed
}
public void ui(final Container pane){
JLabel test = new JLabel("test frame");
pane.add(test);
System.out.println("Frame works.");
}
}
now if you include graphics in this class, everytime you call up repaint(), it will call the function paintComponent(Graphics g). good luck :)
I have a class named Window that extends JFrame and sets up the basic layout. To that JFrame I want to add different JPanels and a seperate class named Tracking is deciding which JPanel is being showed in the JFrame. I would like to have the Tracking class being able to change what JPanel is being showed in the JFrame.
Each JPanel is a seperate class, the JFrame and the Tracking class are also seperate classes.
Window.java
package Setup;
import javax.swing.JFrame;
public class Window extends JFrame
{
private static final int width = 1280;
private static final int height = 720;
public Window()
{
setResizable(false);
setSize(width, height);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// frame.setUndecorated(true);
// frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
setVisible(true);
}
public static void main(String[] args)
{
new Window();
}
}
Animation.java
package Setup;
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.JPanel;
public class Animation extends JPanel
{
private final int width = super.getWidth();
private final int height = super.getHeight();
private Image dbImage;
private Graphics dbGraphics;
public Animation()
{
setSize(width, height);
setVisible(true);
}
#Override
public void paint(Graphics g)
{
dbImage = createImage(getWidth(), getHeight());
dbGraphics = dbImage.getGraphics();
paintComponent(dbGraphics);
g.drawImage(dbImage, 0, 0, this);
}
#Override
public void paintComponent(Graphics g)
{
//ADD Animation to get attention from users
g.drawString("TEST TEST TEST", 50, 50);
repaint();
}
}
You have any number of cohoes
You Could...
Pass a reference of the frame or the controller. This would then allow you access to all the components on the frame.
This is a bad idea as it would expose the frame to the controller unnecessarily, give he controller more power then it actually needs...
You Could...
Pass the list of panels to the controller. This wold allow he controller to change the panels as it required.
This not too bad an idea, but a clever controller would be able to use the getParent method to deduce the top level container, again, exposing parts of your application unnecessarily
You Could...
Create a simple model that has simple accessors that would allow the controller to the ability to suggest to the UI what views should be active. These could be named, for instances, or you could simply supply next/previous/first/last methods to allow the controller to change the view.
This allows you to change the views ordering in a pluggable fashion,simply by supplying a new model, which wouldn't affect any other part of the application
I am trying to learn java and i am practicing with a simple program with 2 simple buttons. Here is my code :
import javax.swing.*;
public class Main {
public static void main(String[] args) {
JFrame frame = new JFrame("Askhsh 3");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ColorJPanel application = new ColorJPanel();
frame.add(application);
frame.setSize(500,500);
frame.setVisible(true);
}
}
And the class ColorJPanel:
import java.awt.*;
import javax.swing.*;
public class ColorJPanel extends JPanel{
public void paintComponent(Graphics g)
{
super.paintComponent(g);
this.setBackground(Color.WHITE);
JButton arxikopoihsh = new JButton("Αρχικοποίκηση");
JButton klhrwsh = new JButton("Κλήρωση");
add(arxikopoihsh);
add(klhrwsh);
this.revalidate();
this.repaint();
}
}
As you can see the only thing i want to do for now is to place 2 simple buttons that do nothing! Here is my output:
http://imageshack.us/photo/my-images/847/efarmogh.jpg/
When i am running the application i am seeing the buttons filling the window!
Note that if i remove the "this.revalidate();" command i have to resize the window to see the buttons !
Thanks very much for your time :)
Don't add components in paintComponent. This method is for painting only, not for program logic or to build GUI's. Know that this method gets called many times, often by the JVM and most of the time this is out of your control, and also know that when you ask for it to be called via the repaint() method, this is only a suggestion and the paint manager may sometimes choose to ignore your request. The paintComponent method must be lean and fast as anything that slows it down will slow down the perceived responsiveness of your application.
In your current code, I don't even see a need to have a paintComponent method override, so unless you need it (if doing for instance custom painting of the component), I suggest that you get rid of this method (and the calls to repaint and revalidate). Instead, add your components in the class's constructor and make sure to pack your top level container after adding components and before calling setVisible(true). Most important -- read the Swing tutorials as this is all covered there.
e.g.,
Main.java
import javax.swing.*;
public class Main {
public static void main(String[] args) {
JFrame frame = new JFrame("Askhsh 3");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ColorJPanel application = new ColorJPanel();
frame.add(application);
frame.pack();
frame.setVisible(true);
}
}
ColorJPanel.Java
import java.awt.*;
import javax.swing.*;
public class ColorJPanel extends JPanel{
public static final int CJP_WIDTH = 500;
public static final int CJP_HEIGHT = 500;
public ColorJPanel() {
this.setBackground(Color.WHITE);
JButton arxikopoihsh = new JButton("Αρχικοποίκηση");
JButton klhrwsh = new JButton("Κλήρωση");
add(arxikopoihsh);
add(klhrwsh);
}
// let the component size itself
public Dimension getPreferredSize() {
return new Dimension(CJP_WIDTH, CJP_HEIGHT);
}
}