一个红绿灯的java代码,为什么编译起来,运行了applet小程序,显示的是空白界面??
importjava.awt.*;importjava.applet.*;publicclassTrafficLightAppletextendsApplet{publi...
import java.awt.*;
import java.applet.*;
public class TrafficLightApplet extends Applet {
public void panint(Graphics g){
int appletWidth = getSize().width;
int appletHeight = getSize().height;
int boxWidth;
if(appletWidth/5<appletHeight/3)
boxWidth = appletWidth/5;
else
boxWidth = appletHeight/3;
int boxHeight = boxWidth/3;
int xMargin = (appletWidth - boxWidth)/2;
int yMargin = (appletHeight - boxHeight)/2;
g.drawRect(xMargin,yMargin,boxWidth,boxHeight);
int diameter = 3*boxWidth/4;
int slotSize = boxWidth;
int slotMargin = (slotSize - diameter)/2;
int lightXCoord = xMargin+slotMargin;
g.setColor(Color.red);
g.fillOval(lightXCoord,yMargin+slotMargin,diameter,diameter);
g.setColor(Color.yellow);
g.fillOval(lightXCoord,yMargin+slotMargin+slotSize,diameter,diameter);
g.setColor(Color.green);
g.fillOval(lightXCoord,yMargin+slotMargin+2*slotSize,diameter,diameter);
}
} 展开
import java.applet.*;
public class TrafficLightApplet extends Applet {
public void panint(Graphics g){
int appletWidth = getSize().width;
int appletHeight = getSize().height;
int boxWidth;
if(appletWidth/5<appletHeight/3)
boxWidth = appletWidth/5;
else
boxWidth = appletHeight/3;
int boxHeight = boxWidth/3;
int xMargin = (appletWidth - boxWidth)/2;
int yMargin = (appletHeight - boxHeight)/2;
g.drawRect(xMargin,yMargin,boxWidth,boxHeight);
int diameter = 3*boxWidth/4;
int slotSize = boxWidth;
int slotMargin = (slotSize - diameter)/2;
int lightXCoord = xMargin+slotMargin;
g.setColor(Color.red);
g.fillOval(lightXCoord,yMargin+slotMargin,diameter,diameter);
g.setColor(Color.yellow);
g.fillOval(lightXCoord,yMargin+slotMargin+slotSize,diameter,diameter);
g.setColor(Color.green);
g.fillOval(lightXCoord,yMargin+slotMargin+2*slotSize,diameter,diameter);
}
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询