第一次海报和明显的n00b java学生。我一直在研究这个问题,过去两天在互联网上搜索,我尝试的任何事情都没有结果。这最初是一个使用 JFrame 的程序,我需要将其转换为 Applet。代码编译良好,没有错误并启动小程序,仅此而已。我得到一个灰色窗口并显示“Applet 已启动”。它应该显示在计时器上将微笑切换为皱眉并再次返回的图形。我不是在寻找任何人来完成我的作业(因为我相信你不会)我只需要一些指导。
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();
}
}
}