2014-02-23 254 views
1

最近我一直在構建JFrame上繪製圖像的應用了,一切都很好,但是當窗口熄滅屏幕,已經繪製的圖片已經消失了,我一直在想,如果有是避免這種情況的一種方式?謝謝的DrawImage時關閉屏幕

import javax.swing.*; 

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

公共類Pokemoneur延伸的JFrame實現的AdjustmentListener, 的ActionListener,的MouseListener,可運行{

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 後:

+1

請張貼代碼,而問題的屏幕截圖。我們無法調試我們無法看到的內容。 –

+0

你的問題已被擱置。但這不是永久性的。我建議你改進你的問題,包括添加相關的代碼和detal。「 –

+0

我在代碼中添加了更多信息,以及我的問題是什麼作爲圖像... – user3342795

回答

2

你問:

但是當窗口熄滅屏幕時,已經繪製的圖像已經消失,我一直在想是否有辦法避免這種情況?

這意味着您的代碼中存在一個錯誤,代碼中您實際上並未顯示我們。

也許你正在使用通過調用組件上的getGraphics()獲得的Graphics對象進行繪製,但我所能做的就是在此處進行一次瘋狂的猜測。如果你這樣做,不要。在組件上調用getGraphics()將爲您提供一個Graphics對象,該對象不會在重複的繪製中持久化,並且會導致同樣不會持久的圖像。如Performing Custom Painting with Swing tutorials中詳細描述的那樣,在JFrame中顯示的JPanel的paintComponent(Graphics g)方法中繪製。但最重要的是,請花點功夫問你的問題,這樣我們就不必猜測未看到的代碼。