关闭屏幕时DrawImage消失了

时间:2014-02-23 08:16:32

标签: java swing

最近我一直在构建一个在JFrame上绘制图像的应用程序,一切都很棒,但是当窗口离开屏幕时,已经绘制的图像已经消失,我一直想知道是否有办法避免那?感谢

import javax.swing.*;

import java.awt。; import java.awt.event。;

公共类Pokemoneur扩展JFrame实现了AdjustmentListener,         ActionListener,MouseListener,Runnable {

private JButton a = new JButton("Reset");
private int nbi = 7;
private JPanel frame;
private int n;

private int x2, y2;
private static int temp2;
private Object src;
private Thread t;
private JButton[] v = new JButton[nbi];
private ImageIcon[] imagei = new ImageIcon[7];
private JPanel canevas = new JPanel();
private JTextField nombre = new JTextField(7);
private JScrollBar js = new JScrollBar(0, 0, 0, 0, 100);
private JPanel panboutton = new JPanel(new GridLayout(1, 8));
private JPanel panjs = new JPanel(new BorderLayout(5, 5));
private JPanel frame2 = new JPanel(new GridLayout(2, 1, 5, 5));
private Graphics g;

public Pokemoneur() {

    startGUI();
    list();
    this.setVisible(true);
}

public void startGUI() {
    int max = 1000;
    frame = (JPanel) this.getContentPane();
    for (int i = 0; i < imagei.length; i++) {

    }

    frame.add(canevas);

    frame.add(frame2, "North");
    frame2.add(panboutton);
    frame2.add(panjs);
    js.setValue(6);
    nombre.setText("" + js.getValue());
    for (int i = 0; i < nbi; i++) {
        imagei[i] = new ImageIcon(getClass()
                .getResource("pok" + i + ".png"));
        v[i] = new JButton(imagei[i]);

        panboutton.add(v[i]);
    }

    panjs.add(js);
    js.setMaximum(max);
    panjs.add(nombre, "East");

    frame.setPreferredSize(new Dimension(500, 500));

    frame.setBorder(BorderFactory.createTitledBorder("Marc André 2014"));
    panjs.setBorder(BorderFactory.createTitledBorder("Numbers of Pokemons"));
    canevas.setBorder(BorderFactory.createTitledBorder("Party"));

    frame.add(a, "South");

    g = canevas.getGraphics();
    this.pack();
    this.setLocationRelativeTo(null);
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);

}

public void run() {

    for (int i = 0; i < v.length; i++) {
        if (src == v[i]) {

            for (int j = 0; j < js.getValue(); j++) {
                int x = (int) (Math.random() * canevas.getWidth());
                int y = (int) (Math.random() * canevas.getHeight());
                int n = 0;
                do {
                    n = (int) (Math.random() * nbi);
                } while (n == i);
                if (x > 80)
                    x -= 80;
                if (y > 80)
                    y -= 80;

                g.drawImage(imagei[n].getImage(), x, y, null);

            }
            x2 = (int) (Math.random() * canevas.getWidth());
            y2 = (int) (Math.random() * canevas.getHeight());
            if (x2 > 80)
                x2 -= 80;
            if (y2 > 80)
                y2 -= 80;

            g.drawImage(imagei[i].getImage(), x2, y2, null);

            temp2 = i;
            do {
                n = (int) (Math.random() * nbi);
            } while (n == temp2);

            g.drawImage(imagei[n].getImage(), x2 + 13, y2, null);
        }
    }
}

public void list() {
    js.addAdjustmentListener(this);
    for (int i = 0; i < nbi; i++) {
        v[i].addActionListener(this);
    }
    a.addActionListener(this);
    canevas.addMouseListener(this);
}

public void adjustmentValueChanged(AdjustmentEvent x) {
    nombre.setText("" + js.getValue());
}

public void actionPerformed(ActionEvent ae) {
    src = ae.getSource();
    if (src == a) {
        try {
            g.setColor(canevas.getBackground());
            g.fillRect(0, 0, canevas.getWidth(), canevas.getHeight());
            t.join();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    } else {
        g = canevas.getGraphics();
        g.setColor(canevas.getBackground());
        g.fillRect(0, 0, canevas.getWidth(), canevas.getHeight());
        t = new Thread(this);
        t.start();
    }
}

@Override
public void mouseClicked(MouseEvent e) {
    // TODO Auto-generated method stub

}

@Override
public void mousePressed(MouseEvent e) {
    int x = e.getX();
    int y = e.getY();
    System.out.println(x2 + "," + y2 + "\n" + x + " " + y);
    // if((x>=x2 && x<=x2+80)&&(x>=x2 && y<=y2+80)){
    if ((x >= x2 && x <= x2 + 80) && (y >= y2 && y <= y2 + 80)) {
        System.out.println("True");
        g.setColor(canevas.getBackground());
        g.fillRect(0, 0, canevas.getWidth(), canevas.getHeight());
        System.out.println("in");
        g.drawImage(imagei[temp2].getImage(), x2, y2, null);

        System.out.println(temp2);
        System.out.println("end");
    } else {
        System.out.println("false");
    }

}

@Override
public void mouseReleased(MouseEvent e) {
    // TODO Auto-generated method stub

}

@Override
public void mouseEntered(MouseEvent e) {
    // TODO Auto-generated method stub

}

@Override
public void mouseExited(MouseEvent e) {
    // TODO Auto-generated method stub

}
public static void main(String[] args) {
    new Pokemoneur();
}

} 问题是,当用户在屏幕上绘制图像时移动Jframe时,图像不会停留...

之前:http://i.stack.imgur.com/KHIvm.png 之后:http://i.stack.imgur.com/koZSI.png

1 个答案:

答案 0 :(得分:2)

你问:

  

但是当窗口离开屏幕时,已经绘制的图像已经消失,我一直在想是否有办法避免这种情况?

这意味着您的代码中存在一个错误,代码中您实际上并没有向我们展示。

也许您正在使用通过在组件上调用getGraphics()获得的Graphics对象进行绘制,但我所能做的就是在此时进行疯狂的A $$ ed猜测。如果你这样做,不要。在组件上调用getGraphics()将获得一个Graphics对象,该对象在重复绘制时不会持久存在,并且会导致图像同样不会持久存在。而是在JFrame中显示的JPanel的paintComponent(Graphics g)方法中绘制,如Performing Custom Painting with Swing tutorials中所述。但最重要的是,请稍微努力一下,提出问题,这样我们就不必猜测未见过的代码。

相关问题