【问题标题】:Adding graphics on same Panel from different classes在同一面板上添加来自不同类的图形
【发布时间】:2013-12-31 10:52:08
【问题描述】:

我正在做一个模拟交通路口的项目。到目前为止,我做了地图,交通信号灯。现在我想在我的项目中添加一些动作,一些汽车。我面临的问题是我无法在同一个面板上添加来自不同类的图形。谁能给我一个很好的教程,让我可以学习如何移动多个图形(在我的例子中是汽车)。

主类:

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

【问题讨论】:

  • 不确定它是否会有所帮助,但您可以查看this。基本上,它使用其他组件来渲染汽车并执行自己的布局来定位“汽车”。另一种选择是绘制地图并在其上绘制“汽车”作为渲染过程的一部分......
  • 好吧,我想每隔 10 秒添加一辆新车,随机在街上。没有你的教程那么复杂
  • 不,但概念是存在的 ;)
  • 好的,但你能告诉我如何在 sam 面板上添加来自不同类的图形吗?我在互联网上搜索了 30-40 分钟,什么也没有……
  • 您是否要在另一个之上叠加一层?

标签: java swing graphics jpanel paintcomponent


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-02-21
    • 1970-01-01
    • 2017-02-13
    • 2021-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多