Hello again! Today's problem is different. For class, I was required to write a program that created 10 concentric circles, using a loop. After a few fruitless tries, I managed to get both of my programs to compile!
Go to give it a try.. And realized that I was supposed to write it as an applet, yet I have it as a JFrame (The nice thing is that it works!). This is the part that I am a little confused on.. What do I need to do to make my program run as an applet? ((The teacher provided the .html portion for the applet, also named ConcentricCircles)).
My book says that you import javax.swing.JApplet; and java.awt.*;, when I replaced JFrame with JApplet, it just broke things. In ConPanel (the class), should I remove the panel set up portion to help fix it?
and ConPanel (the second program)//**************************************************
// ConcentricCircles.java
//
// Demonstrates basic drawing methods and the use of color for concentric circles.
//************************************************** ******
import javax.swing.JFrame;
public class ConcentricCircles
{
//-----------------------------------------------------------------
// Creates main Frame of the program
//-----------------------------------------------------------------
public static void main (String [] args)
{
JFrame frame = new JFrame ("Concentric Circles");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
ConPanel panel = new ConPanel();
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
}
TLDR: What do I need to do in order to get my program to launch in/as an applet, and how would I accomplish this?import java.awt.*;
import javax.swing.JPanel;
public class ConPanel extends JPanel
{
private final int MAX_WIDTH = 300, NUM_RINGS = 10, RING_WIDTH = 20;
//Sets up the panel for the circles
public ConPanel ()
{
setBackground (Color.white);
setPreferredSize (new Dimension (300,300));
}
//Draws 10 Concentric circles in alternating colors
public void paintComponent (Graphics page)
{
super.paintComponent (page);
int x = 0, y = 0, diameter = MAX_WIDTH;
page.setColor (Color.red);
for (int count = 0; count < NUM_RINGS; count++)
{
if (page.getColor() == Color.black) //alternate colors
page.setColor (Color.red);
else
page.setColor (Color.black);
page.fillOval (x, y, diameter, diameter);
diameter -= (2 * RING_WIDTH);
x += RING_WIDTH;
y += RING_WIDTH;
}
}
}
Thanks very much for any help! ((And the quote boxes helped the code some, but still flattened it up against the margin, sorry!))
XI Wiki

