coopwatts
coopwatts

Reputation: 690

Java - Learning inheritance, using Graphics2D, object is redrawn when resizing the frame

I am working on a lab to practice inheritance, in which we are to create a horizontal ellipse as "Shape1" and then create a "Shape2" which extends Shape1 which draws it's superclass Shape1, and then draws a vertical ellipse over top to create a new looking shape. The shape is displaying fine in terms of inheritance and looks (color/location etc) however when running the program, the frame width is set to 1000, and the height is set to 700, but If I drag the frame by the corner to enlarge it, the shape is drawn over and over again as I keep dragging the frame larger. Ideally the shape should just stay where it is relative to the frame size. I think this is happening because while I drag the frame larger, the draw method is being called over and over again by the system, but I am not sure where this is happening or how to fix it. Any suggestions?

All classes are displayed below:

Shape1:

   public class Shape1 {
   private double x, y, r;
   protected Color col;
   private Random randGen = new Random();

   public Shape1(double x, double y, double r) {
      this.x = x;
      this.y = y;
      this.r = r;
      this.col = new Color(randGen.nextFloat(), randGen.nextFloat(), randGen.nextFloat());
   }

   public double getX() {
      return this.x;
   }

   public double getY() {
      return this.y;
   }

   public double getR() {
      return this.r;
   }

   public void draw(Graphics2D g2){
      //Create a horizontal ellipse
      Ellipse2D horizontalEllipse = new Ellipse2D.Double(x - 2*r, y - r, 4 * r, 2 * r);

      g2.setPaint(col);
      g2.fill(horizontalEllipse);
   }

}

Shape2:

public class Shape2 extends Shape1 {

   public Shape2(double x, double y, double r) {
      super(x, y, r);
   }


   public void draw(Graphics2D g2) {
       //Create a horizontal ellipse
       Ellipse2D verticalEllipse = new Ellipse2D.Double(super.getX() - super.getR(),
                                                           super.getY() - 2*super.getR(),
                                                           2 * super.getR(), 4 * super.getR());

       super.draw(g2);
       g2.fill(verticalEllipse);
   }
}

ShapeComponent:

public class ShapeComponent extends JComponent {

   //Instance variables here
   private Random coordGen = new Random();
   private final int FRAME_WIDTH = 1000;
   private final int FRAME_HEIGHT = 700;

   public void paintComponent(Graphics g) {
      Graphics2D g2 = (Graphics2D) g;

      Shape2 myShape = new Shape2(1 + coordGen.nextInt(FRAME_WIDTH), 1 + coordGen.nextInt(FRAME_HEIGHT), 20);
      //Draw shape here
      myShape.draw(g2);
   }
}

ShapeViewer(Where the JFrame is created):

public class ShapeViewer {
   public static void main(String[] args) {

      final int FRAME_WIDTH = 1000;
      final int FRAME_HEIGHT = 700;

      //A new frame
      JFrame frame = new JFrame();

      frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
      frame.setTitle("Lab 5");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      ShapeComponent component = new ShapeComponent();
      frame.add(component);

      //We can see it!
      frame.setVisible(true);  
   }
}

Upvotes: 0

Views: 205

Answers (1)

camickr
camickr

Reputation: 324128

because while I drag the frame larger, the draw method is being called over and over again by the system,

Correct, all components are repainted when the frame is resized.

Any suggestions?

Painting code should be based on properties of your class. If you want the painting to be a fixed size then you define the properties that control the painting and set these properties outside the painting method.

For example, you would never invoke Random.nextInt(...) in the painting method. This means the value will change every time the component is repainted.

So the Shape should be created in the constructor of your class and its size would be defined there, not each time you paint it.

Upvotes: 3

Related Questions