最近我一直在構建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 後:
請張貼代碼,而問題的屏幕截圖。我們無法調試我們無法看到的內容。 –
你的問題已被擱置。但這不是永久性的。我建議你改進你的問題,包括添加相關的代碼和detal。「 –
我在代碼中添加了更多信息,以及我的問題是什麼作爲圖像... – user3342795