I've been trying to visually simulate a traffic problem, but for some reason I get only blank output in my swing window. Instead of a constantly-moving picture with vehicles, I get only a grey screen.
My drawing class looks as follows:
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JPanel;
public class RoadNetwork extends JPanel {
BufferedImage tempicon = ImageIO.read(getClass().getResource("Truck.png"));
BufferedImage truckicon = new BufferedImage(tempicon.getWidth(), tempicon.getHeight(), BufferedImage.TYPE_INT_ARGB);
public RoadNetwork() throws IOException {
repaint();
}
protected void paintComponent (Graphics g) {
super.paintComponent(g);
g.clearRect(0, 0, 600, 600); // insert window size parameters here
for (int i = 0; i < AMEC.vehiclecounter; i++) {
if (AMEC.vehicle[i].spawned == true && AMEC.vehicle[i].finished == false) { // if the truck is somewhere on the plant
g.drawImage(truckicon, AMEC.getcoord(i)[0], AMEC.getcoord(i)[1], this);
}
}
g.drawImage(truckicon, 100, 100, this);
}
}
The call to the class in my main function is done as follows:
RoadNetwork roadnetwork = new RoadNetwork();
roadnetwork.setVisible(true);
JFrame frame = new JFrame();
frame.add(roadnetwork);
frame.setSize(600, 600);
frame.setVisible(true);
Then, with every iteration of my simulation, I call
roadnetwork.repaint();
What am I missing?
BufferedImage tempicon = ImageIO.read(getClass().getResource("Truck.png"));
BufferedImage truckicon = new BufferedImage(tempicon.getWidth(), tempicon.getHeight(), BufferedImage.TYPE_INT_ARGB);
At the end of the 2nd code line, truckicon
is still completely invisible (since it is a transparent image that we have drawn nothing to). Try instead.
BufferedImage truckicon = ImageIO.read(getClass().getResource("Truck.png"));
I hate not knowing why something works. But I remember going through the Graphics totorial and seeing them do this in the constructor where they seem to draw the second image
BufferedImage img = ImageIO.read(imageSrc);
int w = img.getWidth(null);
int h = img.getHeight(null);
BufferedImage bi = new
BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
Graphics g = bi.getGraphics();
g.drawImage(img, 0, 0, null);
Here's the fix I made. I wish I could give more explanation, but feel free to check out the tutorial I linked
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class RoadNetwork extends JPanel {
BufferedImage tempicon;
BufferedImage truckicon;
public RoadNetwork() throws IOException {
tempicon = ImageIO.read(getClass().getResource("resources/stack_reverse.png"));
int w = tempicon.getWidth(null);
int h = tempicon.getHeight(null);
truckicon = new
BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
Graphics g = truckicon.getGraphics();
g.drawImage(tempicon, 0, 0, null);
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
//g.clearRect(0, 0, 600, 600); // insert window size parameters here
// for (int i = 0; i < AMEC.vehiclecounter; i++) {
// if (AMEC.vehicle[i].spawned == true && AMEC.vehicle[i].finished ==
// false) { // if the truck is somewhere on the plant
// g.drawImage(truckicon, AMEC.getcoord(i)[0], AMEC.getcoord(i)[1],
// this);
// }
// }
g.drawImage(truckicon, 100, 100, this);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
public void run() {
try {
RoadNetwork roadnetwork = new RoadNetwork();
roadnetwork.setVisible(true);
JFrame frame = new JFrame();
frame.add(roadnetwork);
frame.setSize(600, 600);
frame.setVisible(true);
} catch (IOException es) {
es.printStackTrace();
}
}
});
}
}
Also, I was going to suggest @AndrewThompson's answer, but because I didn't know the reason you were using two BufferedImages, I didn't want to suggest it. You may have been trying to do what the tutorial was doing.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.