forked from oopd-gu-chalmers/lab2-old
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DrawPanel.java
51 lines (43 loc) · 1.77 KB
/
DrawPanel.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.*;
// This panel represent the animated part of the view with the car images.
public class DrawPanel extends JPanel{
// Just a single image, TODO: Generalize
BufferedImage volvoImage;
// To keep track of a singel cars position
Point volvoPoint = new Point();
// TODO: Make this genereal for all cars
void moveit(int x, int y){
volvoPoint.x = x;
volvoPoint.y = y;
}
// Initializes the panel and reads the images
public DrawPanel(int x, int y) {
this.setDoubleBuffered(true);
this.setPreferredSize(new Dimension(x, y));
this.setBackground(Color.green);
// Print an error message in case file is not found with a try/catch block
try {
// You can remove the "pics" part if running outside of IntelliJ and
// everything is in the same main folder.
// volvoImage = ImageIO.read(new File("Volvo240.jpg"));
// Rememember to rightclick src New -> Package -> name: pics -> MOVE *.jpg to pics.
// if you are starting in IntelliJ.
volvoImage = ImageIO.read(DrawPanel.class.getResourceAsStream("pics/Volvo240.jpg"));
} catch (IOException ex)
{
ex.printStackTrace();
}
}
// This method is called each time the panel updates/refreshes/repaints itself
// TODO: Change to suit your needs.
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(volvoImage, volvoPoint.x, volvoPoint.y, null); // see javadoc for more info on the parameters
}
}