dcsimg
I'm trying to write a DrawCircle.java program.
0 posts in topic
Flat View  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
{
int radius, x, y;
Color color;

public void init()
{
String inRadius = getParameter("radius");
String inX = getParameter("x");
String inY = getParameter("y");
String inColor = getParameter("color");
if (inRadius == null)
radius = 100;
if (inX == null)
x = 110;
if (inY == null)
y = 110;
if (inColor == null)
color = Color.blue;
try
{
radius = Integer.parseInt(inRadius);
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);
Ellipse2D.Double circle = new Ellipse2D.Double(x, y, radius, radius);
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);

// add panel to frame

DrawPanel panel = new DrawPanel();
Container contentPane = getContentPane();
contentPane.add(panel);
}

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();
double inRadius = 100;

Ellipse2D circle = new Ellipse2D.Double();
circle.setFrameFromCenter(inX, inY,
inX + inRadius, inY + inRadius);
g2.draw(circle);
}
}


I know what I have is not exactly what it's supposed to look like, but I was trying to start small and then add to it to get the eight circles. But I can't get the samll part to work. I'd appreciate any help. Thanks.

   <<Less
About | Sitemap | Contact