I have a class Roundabout(JFrame), Surface(JPanel) and Spawn. In the Surface paintComponent
I already draw some trafficlights, because they are always on the roundabout. But I also want to draw components at runtime. So I added a mousePressed event to the JFrame, the method I call here evetually calls spawnPedestrian
from Spawn.java, I can see this because of the println
. But when spawnPedestrian
is called nothing is painted! If I create a pedestrian in the Surface class and paint it, it works. How I can make spawnPedestrian
work?. I have tried to include the minimal code snippet (see below).
human.png
roundabout.png
public class Spawn {
public void spawnPedestrian(){
//Create a new pedestrian.
Pedestrian p = new Pedestrian(100,100);
Roundabout.getSurface().add(p);
Roundabout.getSurface().revalidate();
Roundabout.getSurface().repaint();
}
}
public class Roundabout extends JFrame{
static Surface surface=new Surface();
public Roundabout(){
initUI();
}
private void initUI() {
setTitle("Roundabout");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
add(surface);
this.addMouseListener(new MouseAdapter() {// empty implementation of all
// MouseListener`s methods
@Override
public void mousePressed(MouseEvent e) {
System.out.println(e.getX() + "," + e.getY());
Spawn spawn=new Spawn();
spawn.spawnPedestrian();
}
});
setSize(1618,850);
setLocationRelativeTo(null);
}
public static JPanel getSurface() {
return surface;
}
public static void main(String[] args) {
//Swing thread
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
Roundabout roundabout=new Roundabout();
roundabout.setVisible(true);
}
});
}
class Surface extends JPanel {
Track track=new Track();
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
//Make sure the track is painted first
track.paint(g);
}
}
public class Track{
BufferedImage track;
Point trackPosition;
static final Point TRACK_POS = new Point(0, 0);
static final Point SENSOR_POS = new Point(250, 70);
public Track(){
try {
track = ImageIO.read(Roundabout.class.getResource("images/roundabout.png"));
} catch (Exception ex) {
System.out.println("Problem loading track image: " + ex);
}
trackPosition=new Point(TRACK_POS.x,TRACK_POS.y);
}
public void paint(Graphics g)
{
g.drawImage(track,TRACK_POS.x, TRACK_POS.y, null);
}
}
public class Spawn {
//Needs x/y pos
public void spawnPedestrian(){
Pedestrian p = new Pedestrian(30,50);
System.out.println("Spawn me ");
Roundabout.getSurface().add(p);
Roundabout.getSurface().revalidate();
Roundabout.getSurface().repaint();
}
}
}
public class Pedestrian extends JComponent {
BufferedImage pedestrian;
Point pedestrianPosition;
double pedestrianRotation = 0;
int pedestrianW, pedestrianH;
public Pedestrian(int x, int y){
try {
pedestrian = ImageIO.read(Car.class.getResource("images/human.png"));
} catch (IOException e) {
System.out.println("Problem loading pedestrian images: " + e);
}
pedestrianPosition = new Point(x,y);
pedestrianW = pedestrian.getWidth();
pedestrianH = pedestrian.getHeight();
}
public void paint(Graphics g) {
Graphics2D g2d = (Graphics2D) g.create();
g2d.rotate(Math.toRadians(pedestrianRotation), pedestrianPosition.x, pedestrianPosition.y);
g2d.drawImage(pedestrian, pedestrianPosition.x, pedestrianPosition.y, null);
}
EDIT : Thought I solved it by setting the Surface
layout to null
and and by using setBounds
for Pedestrian
. But the more I move the image to the right, the higher I have to set height
and width
in setBounds()
. Anyone who can help?
EDIT: Modified my post, it is a runnable example now, you only have to pick your own images for Track
and Pedestrian
.
EDIT: One copy paste action to get the code and images added.