BinaryLife
BinaryLife

Reputation: 173

How do I draw a circle and rectangle with specified radius?

I am trying to draw a circle of a radius 60 centerd in the lower right quarter of the frame, and a square of radius 50 centered in the upper half of the frame.

The frame size is 300 x 300. I've done this till now.

import java.awt.*;
import javax.swing.*;
import java.awt.geom.*;
public class Test {

    public static void main ( String[] args){

        JFrameTest5 frame = new JFrameTest5();
         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         frame.setVisible(true);
         frame.setTitle("Test");


    }
}
class JFrameTest5 extends JFrame {

    public JFrameTest5()
       {
           setLocation(0,0);
           setSize(300,300);

        PanelTest1 panel = new PanelTest1();
     add(panel);        
       }


       }

class PanelTest1 extends JPanel

{

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

        Ellipse2D circle = new Ellipse2D.Double(250, 225, 120,120);
        g2.draw(circle);

        Rectangle2D rect = new Rectangle2D.Double(75,0,100,100);
        g2.draw(rect);  

    }

}

The problem is the circle and the rectangle don't seem to be right , are there another methods to set the exact radius ?

Upvotes: 1

Views: 4997

Answers (1)

trashgod
trashgod

Reputation: 205845

The example below includes several important changes:

  • Use constants wherever possible.

  • Use panel-relative geometry.

  • Use initial threads correctly.

  • Use pack() to size the enclosing frame.

Code:

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;
import javax.swing.JFrame;
import javax.swing.JPanel;

/** #see http://stackoverflow.com/a/10255685/230513 */
public class Test {

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                JFrameTest frame = new JFrameTest();
            }
        });
    }
}

class JFrameTest extends JFrame {

    public JFrameTest() {
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setTitle("Test");
        this.add(new JPanelTest());
        this.pack();
        this.setLocationByPlatform(true);
        this.setVisible(true);
    }
}

class JPanelTest extends JPanel {

    private static final int R = 60;
    private static final int D = 2 * R;
    private static final int W = 50;
    private static final int E = 2 * W;

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(300, 300);
    }

    @Override
    public void paintComponent(Graphics g) {
        Graphics2D g2 = (Graphics2D) g;
        Ellipse2D circle = new Ellipse2D.Double(
            getWidth() - D, getHeight() - D, D, D);
        g2.draw(circle);
        Rectangle2D rect = new Rectangle2D.Double(0, 0, E, E);
        g2.draw(rect);
    }
}

Upvotes: 5

Related Questions