0

First time poster and obvious n00b java student. I have been working on this forever, scouring the internet for the last two days and anything I try yields no results. This was originally a program using JFrame and I need to convert it to an Applet. The code compiles fine with no errors and starts the applet but that's it. I get a gray window and says "Applet Started". It is supposed to display graphics that switch a smile to a frown and back again on a timer. I'm not looking for anyone to finish my homework (as I trust you wouldn't) I just need some guidance.

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JApplet;
import javax.swing.JPanel;
import javax.swing.Timer;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Dimension;
public class GAPanel extends JApplet {

public void init() {

    System.out.println("In init");
    this.setSize(new Dimension(600, 600));
    setLayout(new BorderLayout());

}

protected class GAPane extends JPanel implements ActionListener {


public final int FACE_FROWN = 1;
public final int FACE_SMILE = 2;    
private int face = FACE_SMILE;
private Timer timer = null;
{
    timer = new Timer(1000, this);
    timer.start();
}

public void paintComponent(Graphics paint) {

    super.paintComponents(paint);
    System.out.println("In paint");
    this.setBackground(java.awt.Color.blue);
    Graphics2D betterpaint = (Graphics2D) paint;
    betterpaint.drawOval(300, 300, 100, 100);

    paint.setColor(Color.yellow);
    betterpaint.fillOval(300, 300, 100, 100);

    paint.setColor(Color.black);
    betterpaint.drawOval(325, 325, 10, 10);

    paint.setColor(Color.white);
    betterpaint.fillOval(325, 325, 10, 10);
    paint.setColor(Color.black);
    betterpaint.drawOval(365, 325, 10, 10);
    paint.setColor(Color.white);
    betterpaint.fillOval(365, 325, 10, 10);
    paint.setColor(Color.black);
    betterpaint.fillOval(329, 329, 3, 3);
    betterpaint.fillOval(368, 329, 3, 3);
    paint.setColor(Color.black);


    switch (face){
        case FACE_FROWN:
         betterpaint.drawArc(325, 350, 50, 45, 0, 180);
            break;
        case FACE_SMILE:
         betterpaint.drawArc(325, 325, 50, 50, 200, 140);
            break;
    }

}


public void actionPerformed(ActionEvent ae) {

    face = face == FACE_FROWN ? FACE_SMILE : FACE_FROWN;

    this.repaint();

        }
    }
}   
2
  • "This was originally a program using JFrame and I need to convert it to an Applet." I see from the code you mean JApplet but ..why? It is usually better to launch the frame from a link using Java Web Start. Commented May 6, 2013 at 2:37
  • 1
    Change public void paintComponent(Graphics paint) { super.paintComponents(paint); to public void paintComponent(Graphics paint) { super.paintComponent(paint); (note use of plural/singular forms)! Commented May 6, 2013 at 2:40

2 Answers 2

1

Your JApplet's init() overrride is kind of sparse:

public void init() {
    System.out.println("In init");
    this.setSize(new Dimension(600, 600));
    setLayout(new BorderLayout());
}

In fact you don't do anything really useful in it such as adding a GUI to your JApplet's contentPane, so it makes sense that nothing will show. I suggest that you do this, that you create your JPanel and add it to the applet in the init() method. If you haven't gone through the applet tutorials you will want to do that now as this is all explained well there.

1
  • Thanks for the help. this.add(new GAPane()); in my init method fixed it. The setLayout(new BorderLayout()); shouldn't haven't been in there at all. Finals week... everything has been fuzzy. Cheers! Commented May 9, 2013 at 3:13
0

Although JFrames and JApplet's are different utilities, they share a lot of basic functions you are required to use to actually initialize them. From what I can see, you've created a JPanel that holds your smiley face code, but you never actually designated it to be the content pane for your applet. This is probably why you are seeing a gray screen: your applet was created, but not filled with any content.

setContentPane(new GAPane());

I think that should do the trick. Though there are a lot of other things I think you may run into with this code if you just switched it to JFrame without understanding JApplets. Check out this Oracle page, it has a nice documentation and a few examples too:

http://docs.oracle.com/javase/tutorial/uiswing/components/applet.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.