0

I am working on a project that simulates a traffic intersection. So far I did the map, traffic lights. Now I want to add some movement, some cars in my project. The problem that I am facing is that i can't add graphics on the same Panel from different classes. And can someone give me a good tutorial where I can learn how to move multiple graphics (cars in my case) .

Main class:

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

}

Map class:

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

}

And finally MyCar class:

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();
    }   
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
Petrica
  • 37
  • 2
  • 10
  • 1
    Not sure if it will help, but you might be able to take a look at [this](http://stackoverflow.com/questions/14886232/swing-animation-running-extremely-slow/14902184#14902184). Basically, it uses other compnents to render the cars and performs its own layout to position the "cars". Another choice would be to paint the map and the paint the "cars" ontop of it as part of the rending process.... – MadProgrammer Dec 31 '13 at 10:58
  • well , i was thinking to add a new car at each 10 seconds , randomly on the street . Not so complicated as your tutorial – Petrica Dec 31 '13 at 11:02
  • No, but the concept is there ;) – MadProgrammer Dec 31 '13 at 11:02
  • ok , but can you tell me how to add graphics from different classes on the sam Panel ?? i am searching on the internet for 30-40 minutes and nothing.. – Petrica Dec 31 '13 at 11:06
  • Are you trying to layer one on top of the other? – Paul Samsotha Dec 31 '13 at 11:09
  • yeah , i want to add cars (blocks) on the map . – Petrica Dec 31 '13 at 11:11
  • I would suggest doing what @MadProgrammer suggested. You should be painting on the same panel. Trying to layer them may give you problems and unnecessary headaches – Paul Samsotha Dec 31 '13 at 11:11
  • 1. never to call repaint(); inside paintComponent, remove this code line, bacause can caused endless loop, 2. change RUnnable#Thread to Swing Timer otherwise repaint must be called after Thread.sleep(int) ended (dirty hack) 3. you cant to moving with JPanel into JPanel, put everything harcoded in paintCOmponent into array, inside paintComponent to loop inside this array, you can to create two arrays a) roads with traffics light and b) one or endless number of car(s) in 2nd. array – mKorbel Dec 31 '13 at 11:13
  • @mKorbel if i put everything hardcoded in my paintComponent in MyMap how can i make new blocks each 10 seconds ? and to move them? – Petrica Dec 31 '13 at 11:18
  • search im posts tagged by paintComponent in posts by Hovercraft Full Of Eels or MadProgrammer (most active and with SSCCE in their answers) – mKorbel Dec 31 '13 at 13:01

1 Answers1

0

You can set Panel.setLayout(null); and add JButton by setting image icon on it now you can control Button (CAR) location by button.setLocation(x,y);

Sarz
  • 1,970
  • 4
  • 23
  • 43