0

First of, we need a JFrame object.

Then, we create an object of a class extending the JComponent class and inserting the appropriate instructions in the paintComponent(Graphics g) method, where g is actually a Graphics2D object and attach this to the JFrame object.

Now, what I've been taught is that to draw a rectangle, I do:

class RectangleComponent extends JComponent {}

Now, to draw a circle, I do:

class CircleComponent extends JComponent{}

and so on,

However, I want to have one common class that can draw multiple shapes.

I did the following:

class Component extends JComponent {
    Shape shape;
    public Component(Shape shape) {
        this.shape = shape;
    }
    public void paintComponent(Graphics g) {
        Graphics2D g2 = (Graphics2D) g;
        g2.draw(this.shape);
    }
}

In the main, when I get an instruction to draw a rectangle, I do:

Shape currentShape = new Rectangle(args);
Component component = new Component(currentShape);
frame.add(component);

Immediately following this, I get an instruction to draw a circle and I do:

Shape currentShape = new Ellipse2D.Double(args);
Component component = new Component(currentShape);
frame.add(component);

Being a rookie that I am, this is where I get lost. I think this makes the circle overwrite the rectangle. But that's not my intention. What should I do to make them both appear together? I think I should draw them both on the same Component object. But how do I accommodate it in my current implementation?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
B-Mac
  • 557
  • 2
  • 14
  • Components cannot be stacked on top of one another. In this case, a single drawing component might keep a list of all the shapes it needs to render. Note: `public void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D) g;` should be `public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D) g;` – Andrew Thompson Apr 06 '15 at 06:09
  • What does the extra `super.paintComponent(g);` do? Why is it mandatory? I ask this because my instructor has not mentioned anything about it, neither has this book: Big Java Early Objects – B-Mac Apr 06 '15 at 06:10
  • Which part of [the documentation](https://docs.oracle.com/javase/8/docs/api/javax/swing/JComponent.html#paintComponent-java.awt.Graphics-) are you confused about? – Andrew Thompson Apr 06 '15 at 06:22
  • The part involving calling the super class method. – B-Mac Apr 06 '15 at 06:29
  • 1
    *"Further, if you do not invoker super's implementation you must honor the opaque property, that is if this component is opaque, you must completely fill in the background in a non-opaque color. If you do not honor the opaque property you will likely see visual artifacts."* So what don't you understand about that? Be specific, since I don't intend playing 20 questions. – Andrew Thompson Apr 06 '15 at 07:00
  • 1
    A complete example is cited [here](http://stackoverflow.com/a/11944233/230513). – trashgod Apr 06 '15 at 09:27
  • @Grendan, you have posted 30 questions on the forum and not once have you "accepted" an answer. Learn how to use the forum properly. "Accepting" an answer is a way to thank people for the help given and a way to let people know a solution has been found. – camickr Apr 06 '15 at 14:46

0 Answers0