1

I've a VERY simple AWT Painting. Just playing aound to make something bigger. But can't get it working ...

What happens is that only elypse2 is shown - regardless of repaint()ing it or not.

I also tried to use Swing components instead of AWT (JFrame, JComponent) but this also changes nothing.

Is using a Layout Manager necessary? But I want to draw only graphical components, like arcs, rectangles, line, poly-lines, aso ...

Here's the main():

public static void main(String[] args) {
  Frame testFrame = new Frame("Grafx-Test");
  testFrame.setSize(300, 200);
  testFrame.setAlwaysOnTop(true);

  java.awt.EventQueue.invokeLater(new Runnable() {
    @Override
    public void run() {
      testFrame.setVisible(true);
    }
  });

  Elypse elypse = new Elypse(new Point(70, 80), 30, 30, Color.BLUE, false);
  testFrame.add(elypse);

  Elypse elypse2 = new Elypse(new Point(70, 50), 50, 30, Color.BLUE, true);
  testFrame.add(elypse2);
}

and here the used class:

public class Elypse extends Canvas {

  private Point start;
  private int width;
  private int height;
  private Color c;
  private boolean filled;

  public Elypse(Point start, int width, int height, Color c, boolean filled) {
    this.start = start;
    this.width = width;
    this.height = height;
    this.c = c;
    this.filled = filled;
  }

  @Override
  public void paint(Graphics g) {
    g.setColor(c);
    if (filled) {
      g.fillOval(start.x, start.y, width, height);
    }
    else {
      g.drawOval(start.x, start.y, width, height);
    }
  }
}
mpunktw
  • 267
  • 2
  • 4
  • 12

1 Answers1

2

You neglect to pack() the enclosing Window. Note the characteristic symptom in your original code: resizing the frame, which generates an update, causes elypse2 to appear.

Addendum: You can see both Elypse instances by using a layout such as GridLayout.

testFrame.setLayout(new GridLayout(0, 1));

image

As tested:

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Point;

public class Test {

    public static void main(String[] args) {
        Frame testFrame = new Frame("Grafx-Test");
        testFrame.setAlwaysOnTop(true);
        Elypse elypse = new Elypse(new Point(70, 80), 30, 30, Color.BLUE, false);
        testFrame.add(elypse);
        Elypse elypse2 = new Elypse(new Point(70, 50), 50, 30, Color.BLUE, true);
        testFrame.add(elypse2);
        testFrame.pack();
        testFrame.setVisible(true);
    }

    private static class Elypse extends Canvas {

        private Point start;
        private int width;
        private int height;
        private Color c;
        private boolean filled;

        public Elypse(Point start, int width, int height, Color c, boolean filled) {
            this.start = start;
            this.width = width;
            this.height = height;
            this.c = c;
            this.filled = filled;
        }

        @Override
        public void paint(Graphics g) {
            g.setColor(c);
            if (filled) {
                g.fillOval(start.x, start.y, width, height);
            } else {
                g.drawOval(start.x, start.y, width, height);
            }
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(320, 240);
        }
    }
}
trashgod
  • 203,806
  • 29
  • 246
  • 1,045