我想我在这个程序中有了正确的jframe,但是为什么当我运行它时却什么也没出现?
我有两个不同的班级,这是我的第一堂课。只需忽略最后一种方法,在该方法中我将在其中绘制带有圆圈的矩形作为交通信号灯。
这是我的代码。
package trafficlight;
import java.awt.Color;
import java.awt.Graphics;
public class TrafficLight {
private int goDuration;
private int stopDuration;
private int warnDuration;
public enum State {STOP, GO, WARN};
public Color GO_COLOR = Color.green;
public Color STOP_COLOR = Color.red;
public Color OFF_COLOR = Color.darkGray;
public Color WARNING_COLOR = Color.yellow;
private State currentState;
public TrafficLight() {
goDuration = 2;
stopDuration = 2;
warnDuration =1;
currentState = State.GO;
}
public void changeLight(){
if(currentState == State.GO){
currentState = State.WARN;
}
if(currentState == State.WARN){
currentState = State.STOP;
}
if(currentState == State.STOP){
currentState = State.GO;
}
}
public int getGoDuration() {
return goDuration;
}
public void setGoDuration(int goDuration) {
this.goDuration = goDuration;
}
public int getStopDuration() {
return stopDuration;
}
public void setStopDuration(int stopDuration) {
this.stopDuration = stopDuration;
}
public int getWarnDuration() {
return warnDuration;
}
public void setWarnDuration(int warnDuration) {
this.warnDuration = warnDuration;
}
public State getCurrentState() {
return currentState;
}
public void setCurrentState(State currentState) {
this.currentState = currentState;
}
public int getCurrentDuration(){
int duration = 0;
if (currentState == State.STOP){
duration = stopDuration;
}
if (currentState == State.GO){
duration = goDuration;
}
if (currentState == State.WARN){
duration = warnDuration;
}
return duration;
}
public void draw(Graphics canvas) {
canvas.drawRect(125,185,100,250);
canvas.drawOval(145,200,60,60);
canvas.drawOval(145,280,60,60);
canvas.drawOval(145,360,60,60);
if (currentState == State.STOP){
}
}
}
这是我的第二堂课。
package trafficlight;
import java.awt.*;
import javax.swing.*;
public class TrafficLightDriver extends JFrame {
private static TrafficLight light;
public void message() {
}
public static void main(String[] args) {
TrafficLightDriver myFrame = new TrafficLightDriver();
int delay, answer;
String valueString;
do {
valueString = JOptionPane.showInputDialog("What is the green light delay? (1.. 10)");
light.setGoDuration(Integer.parseInt(valueString));
valueString = JOptionPane.showInputDialog("What is the yellow light delay? (1.. 10)");
light.setWarnDuration(Integer.parseInt(valueString));
valueString = JOptionPane.showInputDialog("What is the red light delay? (1.. 10)");
light.setStopDuration(Integer.parseInt(valueString));
for (int i = 1; i <= 10; i++) {
delay = light.getCurrentDuration();
Wait.manySec(delay);
light.changeLight();
myFrame.repaint();
}
answer = JOptionPane.showConfirmDialog(null, "Would you like to run the light again?",
null, JOptionPane.YES_NO_OPTION);
} while (answer == JOptionPane.YES_OPTION);
System.exit(0);
}
@Override
public void paint(Graphics canvas) {
light.draw(canvas);
}
public TrafficLightDriver() { //constructor
setSize(350, 600);
setDefaultCloseOperation(EXIT_ON_CLOSE);
light = new TrafficLight();
setVisible(true);
}
}
这是我的等候课
package trafficlight;
public class Wait {
public static void oneSec() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
System.err.println(e);
}
}
public static void manySec(long s) {
try {
Thread.sleep(s * 1000);
} catch (InterruptedException e) {
System.err.println(e);
}
}
public static void tenthOfSec(long s) {
try {
Thread.sleep(s * 100);
} catch (InterruptedException e) {
System.err.println(e);
}
}
}
最佳答案
您应该避免覆盖顶级容器的paint
,而应使用类似JPanel
的方法并覆盖其paintComponent
方法。
绘制过程是一系列链接在一起的方法,所有这些方法彼此构建在一起以生成输出,在中断此更改的那一刻,您就可以引入工件和不规则性。确保始终通过调用super.paintXxx
尊重油漆链
Swing是一个单线程框架。也就是说,只有一个线程负责处理系统内的所有事件,包括重绘请求。这意味着,如果由于任何原因阻塞了该线程,则将阻止处理任何新事件,这些新事件会使您的程序看起来好像已挂起。还要求您确保在此线程的上下文中对UI进行任何更新。
从通读开始
Concurrency in Swing
Performing Custom Painting
Painting in AWT and Swing
现在,我不知道您的Wait
类如何工作,所以我无法对此部分发表评论,但是您的TrafficLight
不会更新自身以反映其当前状态...
更新...
您也有两个main
方法,这非常令人困惑。应用程序逻辑似乎在TrafficLightDriver
中,您应确保在执行程序时正在运行此类。
您的changeLight
方法中存在逻辑问题
public void changeLight(){
if(currentState == State.GO){
currentState = State.WARN;
}
if(currentState == State.WARN){
currentState = State.STOP;
}
if(currentState == State.STOP){
currentState = State.GO;
}
}
基本上,这就是...
如果currentState为GO,请将currentState设置为WARN ...
如果currentState为WARN,则将currentState设置为STOP ...
如果currentState为STOP,则将currentState设置为GO ...
鉴于默认状态为
GO
,因此当您调用此方法时,状态永远不会更改为GO
以外的任何其他值。相反,您应该使用if-else
语句public void changeLight() {
if (currentState == State.GO) {
currentState = State.WARN;
} else if (currentState == State.WARN) {
currentState = State.STOP;
} else if (currentState == State.STOP) {
currentState = State.GO;
}
}
更新
渲染灯光本身很大程度上取决于个人喜好,例如,我可能很想做类似的事情。
switch (getCurrentState()) {
case GO:
canvas.setColor(GO_COLOR);
canvas.drawOval(145,360,60,60);
break;
case WARN:
canvas.setColor(WARNING_COLOR);
canvas.drawOval(145,280,60,60);
break;
case STOP:
canvas.setColor(STOP_COLOR);
canvas.drawOval(145,200,60,60);
break;
}
canvas.setColor(OFF_COLOR);
canvas.drawRect(125,185,100,250);
canvas.drawOval(145,200,60,60);
canvas.drawOval(145,280,60,60);
canvas.drawOval(145,360,60,60);
这将填充活动的光源,但随后将其他所有东西渲染到顶部,因此光源总是被勾勒出轮廓