I am trying to draw two rectangles in a new frame, and I am making some mistake which I can't figure out. It should be easy but I just don't see it.
Here are the two classes:
Drawn rectangles:
import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Rectangle; import javax.swing.JComponent; //Draws two rectangles public class Four { public void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D) g; Rectangle box = new Rectangle(5,10,10,10); g2.draw(box); box.translate(15,25); g2.draw(box); } }
And the frame:
import java.awt.Component; import javax.swing.JFrame; public class BoxViewer { public static void main(String[] args) { JFrame frame = new JFrame(); frame.setSize(300,400); frame.setTitle("Two Rectangles"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Four component = new Four(); //Mixes up both classes frame.add(component); //Error is here frame.setVisible(true); } }