gpt4 book ai didi

java - DrawImage 离开屏幕后消失

转载 作者:行者123 更新时间:2023-11-30 04:01:10 27 4
gpt4 key购买 nike

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

import javax.swing.*;

导入 java.awt.;导入 java.awt.event.;

公共(public)类Pokemoneur扩展了JFrame实现了AdjustmentListener, Action 监听器、鼠标监听器、可运行{

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

最佳答案

你问:

but when the window goes off screen, the images that have been drawn have disappeared, and I have been wondering if there is a way to avoid that?

这意味着您的代码中某处存在错误,而您实际上并未向我们展示代码。

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

关于java - DrawImage 离开屏幕后消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21965837/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com