开发者

How to properly draw images instead of just the main window? [closed]

开发者 https://www.devze.com 2023-02-27 09:34 出处:网络
Closed. This question needs debugging details. It is not currently accepting answers. 开发者_如何学编程
Closed. This question needs debugging details. It is not currently accepting answers. 开发者_如何学编程

Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.

Closed 4 years ago.

Improve this question

If I run it from netbeans, sometimes it draws the images as intended.. but sometimes I get only the main window, and the "start" button after I move my mouse.

public class my_gui extends JFrame {
    public my_gui() {
        setTitle("Broscute 1.0 :p");
        setSize(954, 320);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setResizable(false);
        setIconImage(Toolkit.getDefaultToolkit().getImage("src/img/test.png"));
        setVisible(true);
        initUI();
    }
    public final void initUI() { //ui here
        setLayout(null);
        setLocationRelativeTo(null);
        JPanel panel = new JPanel();
        panel.setLayout(null);
        panel.setBounds(0, 0, 954, 320);
        getContentPane().add(panel);
        JButton button = new JButton("Start!");
        button.setBounds(0, 0, 954, 40);
        final ImagePanel[] label = new ImagePanel[4];
        int i, j;
        for(i=40, j=0;i<=220;i+=60, j++){
            label[j] = new ImagePanel(0, i);
            label[j].setBounds(0, 0, 954, 320);
            panel.add(label[j]);
        }
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                label[3].X += 100;

            }
        });
        panel.add(button);
    }
}
class ImagePanel extends JComponent{
    public int X, Y;
    float v = 10;
    private BufferedImage image;
    ImagePanel(){}
    public ImagePanel(int x, int y) {
        X = x; Y = y;
       try {                
          image = ImageIO.read(new File("src/img/broasca.png"));
       } catch (IOException ex) {
            // handle exception...
       }
    }
    @Override
    public void paintComponent(Graphics g) {
        super.paintChildren(g);  //a friend told me I should put it here
        g.drawImage(image, X, Y, this); // see javadoc for more info on the parameters
        repaint(); //I think this should go here
    }
}

Running it outside the IDE, it wasn't able to find the images.

What am I doing wrong here?


Don't show the dialog until all the initialization is done, ie move the setVisible to the last method in the constructor.

for better reuse, don't have the JFrame derived class call setVisible(true) at all, let the client do it.

The problem is, once you show the window, any changes you make to the window MUST be done on the GUI thread, or else you will get spurious problems like you are seeing.


The first thing you normally do in paintComponent is clear the off-screen bitmap via super.paintComponent() - I bet thats your problem.

0

精彩评论

暂无评论...
验证码 换一张
取 消