jGuru Forums

I'm trying to write a DrawCircle.java program.
0 posts in topic
Flat View
TOPIC ACTIONS:

Posted By:   Dianne_Calhoun
Posted On:   Saturday, October 27, 2001 02:58 PM

I need help figuring out how to get this DrawCircle.java and DrawCircleTest.java to work. Instructions for program: Write a program that draws a series of eight concentric circles. The circles should be separated by 10 pixels. Draw the ovals by using instances of Ellipse2D.Double and the method draw of the class Graphics2D. DrawCircle.java import java.awt.*; import java.awt.geom.*; import javax.swing.*; public class DrawCircle extends JFrame { int radius, x, y; Color color; public void init() { String inRadius = getParameter("radius"); String inX = getParameter("x"); String inY = getParamet   More>>

I need help figuring out how to get this DrawCircle.java and DrawCircleTest.java to work.

Instructions for program: Write a program that draws a series of eight concentric circles. The
circles should be separated by 10 pixels. Draw the ovals by using
instances of Ellipse2D.Double and the method draw of the class
Graphics2D.

DrawCircle.java

```
import java.awt.*;

import java.awt.geom.*;

import javax.swing.*;

public class DrawCircle extends JFrame

{

Color color;

public void init()

{

String inX = getParameter("x");

String inY = getParameter("y");

String inColor = getParameter("color");

if (inX == null)

x = 110;

if (inY == null)

y = 110;

if (inColor == null)

color = Color.blue;

try

{

x = Integer.parseInt(inX);

y = Integer.parseInt(inY);

color = Color.decode(inColor);

}

catch (NumberFormatException e)

{

showStatus("Parameter error" + e.getMessage());

}

}

public void paint(Graphics screen)

{

Graphics2D screen2D = (Graphics2D)screen;

screen2D.setColor(Color.white);

screen2D.fillRect(0, 0, getSize().width, getSize().height);

screen2D.setColor(color);

screen2D.fill(circle);

}

}

```

DrawCircleTest.java

```
import java.awt.*;

import java.awt.geom.*;

import javax.swing.*;

public class DrawCircleTest

{

public static void main(String[] args)

{

DrawFrame frame = new DrawFrame();

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.show();

}

}

/**

A frame that contains a panel with circles

*/

class DrawFrame extends JFrame

{

public DrawFrame()

{

setTitle("DrawCircleTest");

setSize(WIDTH, HEIGHT);

DrawPanel panel = new DrawPanel();

Container contentPane = getContentPane();

}

public static final int WIDTH = 400;

public static final int HEIGHT = 400;

}

/**

A panel that displays ellipses.

*/

class DrawPanel extends JPanel

{

public void paintComponent(Graphics g)

{

super.paintComponent(g);

Graphics2D g2 = (Graphics2D)g;

// draw the enclosed ellipse

Ellipse2D ellipse = new Ellipse2D.Double();

g2.draw(ellipse);

// draw a circle

double inX = CenterX();

double inY = CenterY();

Ellipse2D circle = new Ellipse2D.Double();

circle.setFrameFromCenter(inX, inY,