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();
}
}
}
JApplet
but ..why? It is usually better to launch the frame from a link using Java Web Start.public void paintComponent(Graphics paint) { super.paintComponents(paint);
topublic void paintComponent(Graphics paint) { super.paintComponent(paint);
(note use of plural/singular forms)!