-2

I'm doing this exercise where I'm supposed to write a program that simulates a race between two cars.

I've created a JFrame and added two rectangles that are supposed to be the tracks.

But I can't insert the cars. I have googled and tried some solutions but it just doesn not work out.

Here is my code.

public class Race extends JComponent {
private ImageIcon image;
public void paint(Graphics g) {
    g.setColor(Color.GRAY);
    g.fill3DRect(30, 150, 530, 55,true);
    g.setColor(Color.GRAY);
    g.fill3DRect(30, 250, 530, 55, true);
    g.setColor(Color.BLACK);
    g.fill3DRect(90, 130, 12, 189, true);
}
public static void main(String[] a) {
    JFrame window = new JFrame();
    window.setPreferredSize(new Dimension(600, 400));
    window.getContentPane().setBackground(Color.GREEN);
    window.setResizable(false);
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.getContentPane().add(new Race());
    window.pack();
    window.setVisible(true);
}
}

Where and how can I add two pictures?

Thanks

Jad Chahine
  • 6,849
  • 8
  • 37
  • 59
FR1TTE
  • 1
  • 2
  • Possible duplicate of [Displaying an image in a JFrame](http://stackoverflow.com/questions/20098124/displaying-an-image-in-a-jframe) – Petter Friberg Oct 22 '15 at 12:46
  • 2
    `I've created a JFrame and added two rectangles that are supposed to be the tracks` - well if you can draw rectangles, then you can draw images, just use the `Graphics.drawImage(...)` method. – camickr Oct 22 '15 at 14:42

1 Answers1

0

This is how you would add a image into your JFrame

frame.add(new JLabel(new ImageIcon("Path/To/Your/Image.png")));
Ryan
  • 1,972
  • 2
  • 23
  • 36