I would like to know a little more about Graphics and how to use it.
I have this class :
public class Rectangle
{
private int x, y, length, width;
// constructor, getters and setters
public void drawItself(Graphics g)
{
g.drawRect(x, y, length, width);
}
}
And a very simple frame like this :
public class LittleFrame extends JFrame
{
Rectangle rec = new Rectangle(30,30,200,100);
public LittleFrame()
{
this.getContentPane().setBackground(Color.LIGHT_GRAY);
this.setSize(600,400);
this.setVisible(true);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args)
{
new LittleFrame();
}
}
What I simply want to do is to add this rectangle into my LittleFrame’s container. But I have no idea of how to do it.
I suggest you create an extra class that extends
JPanellike the one below:Then, in your
LittleFrameclass, you will need to add this new panel to your frame content pane and add yourRectangleto the list of rectangles to be painted. At the end of theLittleFrameconstructor, add: