繁体   English   中英

如何添加油漆(图形G)到另一个类?

[英]how to add paint(graphics g) into another class?

我遇到一个计时器类,该类将更改我的GUI程序上的指示灯。 但是我已经使用了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();
    }
}

我有一个错误,说找不到后退符号。 任何帮助将是巨大的!

编辑:

我进行了一些更改并尝试添加绘画(返回图形); 进入课堂。 出现一些错误消息(表达式的非法开头),但是我在右边吗?

首先,您不应该有一个单独的类来implements ActionListner因为这会使一切变得更加复杂。 其次,就像SLaks所说的那样,您需要看一下绘画的工作方式。 在此处检查此链接。

现在,我要做的事情是这样的:

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();
    }
}

如果您在理解代码时遇到问题,请随时提出。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM