gpt4 book ai didi

java - Java 弹跳球闪烁

转载 作者:行者123 更新时间:2023-12-01 12:19:16 26 4
gpt4 key购买 nike

嗨,我的后台程序弹跳球有问题,问题是球闪烁,我不知道如何停止它:

Graficos :

public class Graficos extends JFrame implements ActionListener {

private JButton play, pause, exit;
private Timer reloj;
private Pelota pelota;
private JPanel panel2, panel1;

public Graficos(){
super();
reloj = new Timer(200, this);
this.setLayout(new BorderLayout());
pelota = new Pelota();
play = new JButton("Play");
play.setFont(new Font("ARIAL", Font.BOLD, 20));
play.setBackground(Color.GREEN);
play.setSize(20, 20);
pause = new JButton("Pause");
pause.setFont(new Font("ARIAL", Font.BOLD, 20));
pause.setBackground(Color.ORANGE);
pause.setSize(20, 20);
exit = new JButton("Exit");
exit.setFont(new Font("ARIAL", Font.BOLD, 20));
exit.setBackground(Color.RED);
exit.setSize(20, 20);
panel1 = new JPanel();
panel2 = new JPanel();
panel2.setLayout(new GridLayout(1, 3));

this.add(panel1, BorderLayout.CENTER);
panel1.add(pelota, BorderLayout.CENTER);
this.add(panel2, BorderLayout.SOUTH);
panel2.add(play, BorderLayout.EAST);
panel2.add(pause, BorderLayout.CENTER);
panel2.add(exit, BorderLayout.WEST);

this.setSize(440, 460);
this.setVisible(true);

reloj.start();

play.addActionListener(this);
pause.addActionListener(this);
exit.addActionListener(this);
}

public void paint(Graphics g){
super.paint(g);
pelota.paintComponent(g);
panel1.paintComponents(g);
}

public static void main(String args[]){
Graficos x = new Graficos();
}

@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub

if(e.getSource().equals(play))reloj.start();
else{
if(e.getSource().equals(pause))reloj.stop();
else{
if(e.getSource().equals(exit)){
reloj.stop();
System.exit(0);
}
else if(e.getSource().equals(reloj)){
pelota.muevete();
this.repaint();
}
}
}
}
}

Pelota :

public class Pelota extends JPanel {

private int x, y, dx=5, dy=5, ancho, alto;
private ImageIcon foto, fondo;

public Pelota(){

x = 5;
y = 5;
fondo = new ImageIcon(getClass().getResource("genious.jpg"));
foto = new ImageIcon(getClass().getResource("ball.png"));
ancho = foto.getIconWidth();
alto = foto.getIconHeight();
this.setVisible(true);
this.setSize(fondo.getIconWidth(), fondo.getIconHeight());
}

/*public void pintate(Graphics g){
super.paint(g);
g.drawImage(fondo.getImage(),0,0,null);
g.drawImage(foto.getImage(),x,y,null);
}
*/

public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(fondo.getImage(),0,0,this);
g.drawImage(foto.getImage(),x,y,this);
}

public void muevete(){
x += dx;
y += dy;
if(x+ancho >=fondo.getIconWidth()+30){
dx *= -1;
}
if(y+alto >= fondo.getIconHeight()+40){
dy *= -1;
}
if(x<=-40){
dx *= -1;
}
if(y<=-10){
dy *= -1;
}
//repaint();
}

public int getX() {
return x;
}

public void setX(int x) {
this.x = x;
}

public int getY() {
return y;
}

public void setY(int y) {
this.y = y;
}

public int getDx() {
return dx;
}

public void setDx(int dx) {
this.dx = dx;
}

public int getDy() {
return dy;
}

public void setDy(int dy) {
this.dy = dy;
}

public int getAncho() {
return ancho;
}

public void setAncho(int ancho) {
this.ancho = ancho;
}

public int getAlto() {
return alto;
}

public void setAlto(int alto) {
this.alto = alto;
}

public ImageIcon getFoto() {
return foto;
}

public void setFoto(ImageIcon foto) {
this.foto = foto;
}

public ImageIcon getFondo() {
return fondo;
}

public void setFondo(ImageIcon fondo) {
this.fondo = fondo;
}
}

最佳答案

当你编写这个程序时,你到底在想什么?

public void paint(Graphics g){
super.paint(g);
pelota.paintComponent(g);
panel1.paintComponents(g);
}

它彻底打破了油漆链。删除它以消除闪烁。

那么代码就会存在分层问题和透明度问题。我的猜测是面板需要以相反的顺序添加。例如。而不是:

pelotapanel1JFrame

..应该是..

panel1pelotaJFrame

最上面的面板需要是透明的。

作为更一般的建议:

  1. 为了更快地获得更好的帮助,请发布 MCVE (最小完整可验证示例)。
  2. 获取示例图像的一种方法是热链接到 this Q&A 中看到的图像。 .

关于java - Java 弹跳球闪烁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26807643/

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