繁体   English   中英

在不同类别的同一面板上添加图形

[英]Adding graphics on same Panel from different classes

我正在研究一个模拟交通路口的项目。 到目前为止,我已经完成了地图,交通信号灯。 现在,我想在项目中添加一些运动,一些汽车。 我面临的问题是我无法在同一面板上添加来自不同类的图形 有人可以给我一个很好的教程,让我学习如何移动多个图形(在我的情况下为汽车)。

主班:

import java.awt.Color;

import javax.swing.JFrame;

public class Main {

    public static void main(String[] args) {
        MyMap map = new MyMap(); 
        MyCar car = new MyCar();
        Thread x = new Thread(map);
        x.start();
        JFrame f = new JFrame("Broadway Intersection");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        map.setBackground(Color.white);
        f.add(map);
        f.add(car);
        f.setSize(1366, 738);
        f.setVisible(true); 
    }

}

地图类:

import java.awt.Color;
import java.awt.Graphics;

import javax.swing.JPanel;

public class MyMap extends JPanel implements Runnable {
    Color color = Color.RED;
    Color color2 = Color.RED;
    Color color3 = Color.RED;
    Color color4 = Color.RED;
    int[] faza = new int[4];
    int k;
    int faza_curenta = 0;

    public MyMap() {

        faza[0] = 20;
        faza[1] = 20;
        faza[2] = 20;
        faza[3] = 20;
    }

    public void run() {
        color = Color.GREEN;
        while (true) {
            System.out.println("Faza = " + faza_curenta + " k= " + k);
            k++;
            if (k == faza[0]) {
                faza_curenta = 1;
                color = Color.RED;
                color2 = Color.GREEN;
            } else if (k == (faza[0] + faza[1])) {
                faza_curenta = 2;
                color = Color.RED;
                color2 = Color.RED;
                color3 = Color.GREEN;
            } else if (k == (faza[0] + faza[1] + faza[2])) {
                faza_curenta = 3;
                color = Color.RED;
                color3 = Color.RED;
                color4 = Color.GREEN;
            } else if (k == (faza[0] + faza[1] + faza[2] + faza[3])) {
                faza_curenta = 0;
                color = Color.GREEN;
                color4 = Color.RED;
                k = 0;
            }
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    public void paintComponent(final Graphics g) {
        super.paintComponent(g);

        g.setColor(Color.GREEN);
        g.fillRect(0, 0, 500, 250); // stanga sus
        g.fillRect(900, 0, 500, 250); // dreapta sus
        g.fillRect(0, 500, 500, 250);// stanga jos
        g.fillRect(900, 500, 500, 250); // dreapta jos

        g.setColor(Color.GRAY);
        g.fillRect(500, 0, 400, 900);
        g.fillRect(0, 250, 500, 250);
        g.fillRect(900, 250, 500, 250);

        g.setColor(Color.WHITE);
        g.fillRect(695, 0, 5, 100);// linii verticale
        g.fillRect(695, 150, 5, 100);
        g.fillRect(695, 500, 5, 100);
        g.fillRect(695, 650, 5, 50);

        g.fillRect(0, 370, 50, 5);
        g.fillRect(100, 370, 100, 5); // linii orizontale
        g.fillRect(250, 370, 100, 5);
        g.fillRect(400, 370, 100, 5);
        g.fillRect(900, 370, 100, 5);
        g.fillRect(1050, 370, 100, 5);
        g.fillRect(1200, 370, 100, 5);

        g.setColor(Color.BLACK);
        g.fillRect(470, 220, 30, 30); // semafor Nord
        g.setColor(color);
        g.fillOval(475, 225, 20, 20); // semafor Nord

        g.setColor(Color.BLACK);
        g.fillRect(900, 220, 30, 30); // semafor Est
        g.setColor(color2);
        g.fillOval(905, 225, 20, 20); // semafor Nord

        g.setColor(Color.BLACK);
        g.fillRect(470, 500, 30, 30); // semafor Vest
        g.setColor(color4);
        g.fillOval(475, 505, 20, 20); // semafor Nord

        g.setColor(Color.BLACK);
        g.fillRect(900, 500, 30, 30); // semafor Sud
        g.setColor(color3);
        g.fillOval(905, 505, 20, 20); // semafor Nord
        repaint();
    }

}

最后是MyCar类:

import java.awt.Color;
import java.awt.Graphics;

import javax.swing.JPanel;

public class MyCar extends JPanel  {
    int x; // X position
      int y; // Y position
      int xSpeed; // Speed in the X direction
      int ySpeed; // Speed in the Y direction

    public void paintComponent(final Graphics g) {
        super.paintComponent(g);

        g.setColor(Color.GREEN);
        g.fillRect(420, 200, 30, 30); 
        repaint();
    }   
}

您可以设置Panel.setLayout(null); 并通过在其上设置图像图标来添加JButton,现在您可以通过button.setLocation(x,y);控制Button(CAR)的位置button.setLocation(x,y);

暂无
暂无

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

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