无法在Swing中显示图形

时间:2014-01-08 11:58:21

标签: java image swing embedded-resource

我一直试图在视觉上模拟交通问题,但由于某种原因,我在摆动窗口中只得到空白输出。我只得到一个灰色的屏幕,而不是一张带有车辆的不断移动的画面。

我的绘图课程如下:

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

我的main函数中对类的调用如下:

RoadNetwork roadnetwork = new RoadNetwork();
roadnetwork.setVisible(true);
JFrame frame = new JFrame();
frame.add(roadnetwork);
frame.setSize(600, 600);
frame.setVisible(true);

然后,在模拟的每次迭代中,我都会调用

roadnetwork.repaint();

我错过了什么?

2 个答案:

答案 0 :(得分:4)

 BufferedImage tempicon = ImageIO.read(getClass().getResource("Truck.png"));
 BufferedImage truckicon = new BufferedImage(tempicon.getWidth(), tempicon.getHeight(), BufferedImage.TYPE_INT_ARGB);

在第二个代码行的末尾,truckicon仍然完全不可见(因为它是一个我们没有得到任何东西的透明图像)。请改为尝试。

 BufferedImage truckicon = ImageIO.read(getClass().getResource("Truck.png"));

答案 1 :(得分:1)

我讨厌不知道为什么会有效。但我记得经过Graphics totorial并看到他们在构造函数中执行此操作,他们似乎在绘制第二个图像

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

这是我所做的修复。我希望我能给出更多解释,但请随时查看我链接的教程

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

另外,我打算建议@ AndrewThompson的答案,但因为我不知道你使用两个BufferedImages的原因,我不想暗示它。您可能一直在尝试做教程正在做的事情。