• Navigation
Results 1 to 3 of 3
  1. #1
    New Merits
    Join Date
    Mar 2010
    Posts
    200
    BG Level
    4
    FFXI Server
    Valefor
    WoW Realm
    Arathor

    Java assistance needed (Changing JFrame to JApplet)

    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?

    //**************************************************
    // 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);
    }
    }
    and ConPanel (the second program)

    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;
    }
    }
    }
    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?

    Thanks very much for any help! ((And the quote boxes helped the code some, but still flattened it up against the margin, sorry!))

  2. #2
    Relic Weapons
    Join Date
    Oct 2006
    Posts
    335
    BG Level
    4

    First, you really should review what the fundamental differences are between a JFrame and a JApplet (specifically, what methods are required for each object). Given that, you should be able to recognize why copy/replace wouldn't work for this.

    An overview of the actual steps you need to convert a JFrame to a JApplet is covered in the Applets tutorial: http://java.sun.com/docs/books/tutor...ingApplet.html . If you understand what it is you're doing in the applet setup, you should realize that you do not need to modify your ConPanel class in the conversion process.

    If your instructor gave you the HTML source you will use to embed the applet, you'll know what additional steps you need to take in terms of the extra files needed to set up the applet properly.

    Second, if the exercise is literally to "draw 10 concentric circles," your program doesn't do this, as it is written now. The fix is simple, but it is, after all, your homework and you should figure out why.

  3. #3
    New Merits
    Join Date
    Mar 2010
    Posts
    200
    BG Level
    4
    FFXI Server
    Valefor
    WoW Realm
    Arathor

    Ah, thanks for the pointer, I tried to look it up yet didn't find this site; I'll take a look through.

Similar Threads

  1. BlueGartr Forum Assistance Needed
    By Sepukku in forum Tech
    Replies: 6
    Last Post: 2009-08-03, 04:00
  2. Need a software to control a PC
    By Ratatapa in forum Tech
    Replies: 13
    Last Post: 2008-08-19, 02:41
  3. Replies: 1
    Last Post: 2008-05-13, 11:29