[英]how to add paint(graphics g) into another class?
I'm having a issue where I have a timer class that will change the light on my GUI program. 我遇到一个计时器类,该类将更改我的GUI程序上的指示灯。 But I already used the public void method line so I'm struggling now to add the paint graphics G in.
但是我已经使用了public void方法行,因此我现在正努力添加绘画图形G。
/*public void paint(Graphics back)
{
back.setColor(Color.black);
back.fillRect(30,30,330,900); //make traffic light for car
back.fillRect(440,140,330,720); //make traffic light for peds
back.setColor(Color.red); //creates red circle
back.fillOval(45,45, 300, 280);
back.fillOval(455,155, 300, 280); //creates red ped circle
back.setColor(Color.yellow); //creates yellow circle
back.fillOval(45, 335, 300,280);
back.setColor(Color.green);
back.fillOval(45,625, 300, 280);
back.fillOval(455,555, 300, 280); //creates green ped circle
}*/
class LightChange implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
paint (Graphics back);
seconds++;
if (seconds == 0 & seconds <= 5)
{
back.setColor(Color.red); //creates red circle
back.fillOval(45,45, 300, 280);
}
else
{
back.setColor(Color.yellow); //creates yellow circle
back.fillOval(45, 335, 300,280);
}
repaint();
}
}
I have a error saying it can't find the back symbol. 我有一个错误,说找不到后退符号。 Any help would be great!
任何帮助将是巨大的!
EDIT: 编辑:
I made some changes and tried adding paint (Graphics back); 我进行了一些更改并尝试添加绘画(返回图形); into the class.
进入课堂。 Some error messages come up (illegal start of expression)but am I on the right lines?
出现一些错误消息(表达式的非法开头),但是我在右边吗?
First, you shouldn't have a separated class that implements ActionListner
because that will make everything way much more complicated. 首先,您不应该有一个单独的类来
implements ActionListner
因为这会使一切变得更加复杂。 Secondly, like SLaks said you need to take a look at how painting works. 其次,就像SLaks所说的那样,您需要看一下绘画的工作方式。 Check this link here .
在此处检查此链接。
Now, the way I would do it, it's something like this: 现在,我要做的事情是这样的:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JPanel;
import javax.swing.Timer;
public class Draw extends JPanel implements ActionListener {
private Color topColorTraffic = Color.red;
private Color middleColorTraffic = Color.gray;
private Color bottomColorTraffic = Color.gray;
private Color topColorPeds = Color.gray;
private Color bottomColorPeds = Color.green;
// WE INITIALIZED THE UPPER VARIABLE LIKE THIS BECAUSE WE PRESUMED THAT
// IS RED FOR THE CARS AND GREEN FOR THE PEDS
private long startTime; // This will get the time when we've started the "animation"
private long targetTimeTraffic; // This will be the time we want the have
// between red changes to
// yellow in the traffic
private long targetTimeYellowLight; //This will be the time we want the yellow
//light to be displayed
private Timer t;
public Draw() {
startTime = System.currentTimeMillis();
targetTimeTraffic = 3000; // 3 sec
targetTimeYellowLight = 1200; //1.2 sec
t = new Timer(5, this); // this will call actionPerformed every 5 miliseconds
t.start();
}
public void paintComponent(Graphics back) {
super.paintComponent(back);
back.setColor(Color.black);
back.fillRect(30, 30, 330, 900); // make traffic light for car
back.fillRect(440, 140, 330, 720); // make traffic light for peds
back.setColor(topColorTraffic); // creates the top traffic circle
back.fillOval(45, 45, 300, 280);
back.setColor(topColorPeds); // creates the top peds circle
back.fillOval(455, 155, 300, 280);
back.setColor(middleColorTraffic); // creates the middle traffic circle
back.fillOval(45, 335, 300, 280);
back.setColor(bottomColorTraffic); // creates the bottom traffic circle
back.fillOval(45, 625, 300, 280);
back.setColor(bottomColorPeds); // creates the bottom peds circle
back.fillOval(455, 555, 300, 280);
}
public void actionPerformed(ActionEvent arg0) {
// changing from red to yellow in traffic
long currentTime = System.currentTimeMillis();
if (targetTimeTraffic <= currentTime - startTime) { //1st delay 3 sec
topColorTraffic = Color.gray;
middleColorTraffic = Color.yellow;
topColorPeds = Color.red;
bottomColorPeds = Color.gray;
}
//changing from yellow to green
if (targetTimeTraffic <= currentTime - startTime - targetTimeYellowLight) { //2nd delay 3 sec + another 1.2 sec
//but 3 sec were already for the 1st delay
// so we get only 1.2 sec delay
topColorTraffic = Color.gray;
middleColorTraffic = Color.yellow;
bottomColorTraffic = Color.green;
middleColorTraffic = Color.gray;
//startTime = System.currentTimeMillis();
}
//changing from green to yellow
if (targetTimeTraffic <= currentTime - startTime - targetTimeYellowLight - targetTimeTraffic) {
topColorTraffic = Color.gray;
bottomColorTraffic = Color.gray;
middleColorTraffic = Color.yellow;
}
//changing from yellow to red
if (targetTimeTraffic <= currentTime - startTime - 2* targetTimeYellowLight - targetTimeTraffic) {
topColorTraffic = Color.red;
middleColorTraffic = Color.gray;
topColorPeds = Color.gray;
bottomColorPeds = Color.green;
startTime = System.currentTimeMillis(); //reinitialize the variable
}
repaint();
}
}
If you have problems understanding the code please feel free to ask. 如果您在理解代码时遇到问题,请随时提出。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.