Join 150,386 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,123 people online right now. Registration is fast and FREE... Join Now!
/** Creates a new instance of Planet */ public Planet(String title) { // Set the title, size, location and default close operation this.setTitle(title); this.setSize(500, 480); this.setLocation(200,200); this.setDefaultCloseOperation(EXIT_ON_CLOSE);
// Create the ArrayList lifeContainer = new ArrayList<LifeObjects>();
thePlanet = new JPanel(); thePlanet.setBackground(Color.WHITE); thePlanet.addMouseListener(new MouseWatcher());
Container cp = getContentPane();
cp.add(thePlanet, BorderLayout.CENTER);
}
public void paint(Graphics g) { super.paint(g); for (LifeObjects output : lifeContainer) { g.setColor(output.getColour()); g.fillOval(output.getX(), output.getY() , output.getWidth(), output.getHeight()); } }
public class MouseWatcher extends MouseAdapter { public void mouseClicked(MouseEvent e) { Blue blueObject = new Blue(Color.BLUE,e.getX(),e.getY()); lifeContainer.add(blueObject); repaint(); } }
}
LifeObjects // This is a class that others inherit from - should have made this abstract but never mind for now
CODE
package lifeo; import java.awt.*; /** * * @author andy */ public class LifeObjects { // Fields private Color lifeColour; private int width, height, X, Y;
/** Creates a new instance of LifeObjects */ public LifeObjects(Color colour, int X, int Y) { lifeColour = colour; width = width; height = height; setX(X); setY(Y); }
public void setX(int X) { this.X = X; }
public void setY(int Y) { this.Y = Y; }
public int getX() { return this.X; }
public int getY() { return this.Y; }
public Color getColour() { return lifeColour; }
public int getWidth() { return width; }
public int getHeight() { return height; }
public void setWidth(int width) { this.width = width; }
public void setHeight(int height) { this.height = height; }
}
Blue // Finally one of the object that are created when clicked
CODE
package lifeo; import java.awt.*; /** * * @author andy */ public class Blue extends LifeObjects { // Fields private int width = 20; private int height = 20;
/** Creates a new instance of Blue */ public Blue(Color colour, int X, int Y) { super(colour,X,Y); super.setWidth(width); super.setHeight(height); }
}
As i say when you click on the JPanel the Blue ball just is off mark, and i just cant figure why
Could someone explain in layman's terms what this does
In order to draw on anything, you need it's graphics context. Basically, you need the graphics object associated with it. Doing a getGraphics(); call is expensive in terms of time. Most Java apps use the one handed to them in the paint method. Which brings us to back you your problem.
You have a perfectly good JFrame that has it's own perfectly good canvas to draw on, and you filled the whole thing up with a JPanel! The Graphics object that the paint method for Planet gets is the one you'd get if you called this.getGraphics();. You can paint on it all you like, but there's a big ole JPanel in front of it. What you did was manually get a Graphics object for the object in fron of your JFrame and painted on that.
public class Planet extends JFrame { // You do not need another JPanel // private JPanel thePlanet; private ArrayList<LifeObject> lifeObjects;
// private classes, no one else needs to share private class MouseWatcher extends MouseAdapter { public void mouseClicked(MouseEvent e) { lifeObjects.add(new LifeObjectBlue(e.getPoint())); repaint(); } }
// might as well make it abstract // it's not like it defines a size :p private abstract class LifeObject { private Color lifeColour; protected int width, height, X, Y; public LifeObject(Color colour, int X, int Y) { this.lifeColour = colour; //this.width = width; //this.height = height; this.X = X; this.Y = Y; } // there will be no set method, you create it, you're stuck with it public Color getColour() { return lifeColour; } public int getX() { return this.X; } public int getY() { return this.Y; } public int getWidth() { return width; } public int getHeight() { return height; } }
private class LifeObjectBlue extends LifeObject { public LifeObjectBlue(Point pt) { super(Color.BLUE,pt.x,pt.y); this.width = 20; this.height = 20; }
}
public Planet(String title) { // Set the title, size, location and default close operation this.setTitle(title); this.setSize(500, 480); this.setLocation(200,200); this.setDefaultCloseOperation(EXIT_ON_CLOSE);
// Create the ArrayList this.lifeObjects = new ArrayList<LifeObject>();
Interesting, my teachings so far have said that the JFrame is like a picture frame, and the Jpanel is the canvas that you paint on, how could you paint without it? but as you demonstrate, you can ! its good to see a different way of doing things, thank you very much.
You can redefine the paint function of any GUI component, so you can effectively use any of them as a canvas. However there is a component called Canvas, that I would recommend to check out, because if you paint on the usual components (like JPanel), you can run into some strange problems, notably: they can have borders, so if you deal with mouse actions, you'll have problems with calculating the exact coordinates, etc. You can avoid these if you use the Canvas instead.
Interesting, my teachings so far have said that the JFrame is like a picture frame, and the Jpanel is the canvas that you paint on,
JFrame is a top level display object. Yes, JPanel is the canvas. However, all Swing componets extend component and implement paint. So you can basically paint on anything. ( Interesting fact: in MS windows, all controls are windows with different formatting flags. )
Yes, using a JPanel as your canvas is most common. In which case you override the paint in your extension of the JPanel object. Here's a more standard design pattern for the same program.
public class Planet extends JPanel implements MouseListener { private ArrayList<LifeObject> lifeObjects;
private abstract class LifeObject { protected Color lifeColour; protected int width, height, x, y; public Color getColour() { return lifeColour; } public int getX() { return this.x; } public int getY() { return this.y; } public int getWidth() { return width; } public int getHeight() { return height; } }