0

I have a problem with put drawn circle into middle of Frame by using methods getWidth() and getHeight(). I tried something with Image package but no idea where to implement this methods:

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Frame;
import java.awt.Image;

public class Circle extends Frame {

public Circle() {

    setSize(400,400);
    setLocationRelativeTo(null);
    setVisible(true);
}

public Color() {
}

public void paint(Graphics g) {

    g.setColor(Color.ORANGE);
    g.fillOval(200, 200, 200, 200);
}

public static void main(String[] args) {

    Circle c = new Circle();

    c.paint(null);
}
}

Then I have to use method setColor(Color) and Color class constructor to make random color of this circle (after every run of this program). I opened Color constructor but there is an error :/

Fastkowy
  • 1,285
  • 3
  • 15
  • 16

2 Answers2

3

Simply call getWidth() and getHeight() from within the paint(...) method and use the results returned for your fillOval(...) parameters.

But having said that, it's a better idea to draw in a Canvas that is added to the Frame. And having said that, it's much better still to draw in the paintComponent(...) method of a JPanel that is added to the contentPane of a JFrame in a Swing application.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
3

Better to extract all the paint functionality to a JComponent here to take full advantage of Swing's optimized paint model using paintComponent.

The Circle is actually a JFrame. Inside in its constructor, a new component is created which handles the painting of the circle. The Color constructor has been removed as this is invalid syntax.

The circle co-ordinates are start in the top left-hand corner and take the full available width & height for drawing.

Also would recommend using lightweight Swing components over old-style AWT component.

public class Circle extends JFrame {

    public Circle() {
        setSize(400, 400);
        add(new CirclePanel());
        setLocationRelativeTo(null);
        setVisible(true);
    }

    public static void main(String[] args) {
        Circle c = new Circle();
    }
}

class CirclePanel extends JComponent {
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.setColor(Color.ORANGE);
        g.fillOval(0, 0, getWidth(), getHeight());  
    }
}

See: Painting in AWT and Swing

Reimeus
  • 158,255
  • 15
  • 216
  • 276
  • Thanks, "could you only explain, why there is Circle object created?" sorry , i get it now :) – Fastkowy Nov 29 '12 at 23:16
  • 1
    See this [post](http://stackoverflow.com/questions/4246351/creating-random-colour-in-java) for creating a random color. – Reimeus Nov 29 '12 at 23:22
  • Edited ^. So do I have to create class (Colors) like this? – Fastkowy Nov 29 '12 at 23:40
  • Please don't change the code in the original question, it detracts from any answers given. If you want to create a new post, people are only too happy to help. – Reimeus Nov 29 '12 at 23:47