gpt4 book ai didi

java - JApplet - 当 boolean 值为 true 时不会绘制图像

转载 作者:行者123 更新时间:2023-12-01 22:44:33 27 4
gpt4 key购买 nike

我有 4 个图 block :蓝色、红色、绿色和黄色。当鼠标移到图 block 上时,我希望它亮起(只需将图像更改为浅色图像)。相反,什么也没有发生。

我已经做了一些简单的调试,并且知道图像已正确导入并且 mouseOverTile[0] 变为 true。

import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Arrays;

import javax.swing.ImageIcon;
import javax.swing.JApplet;


public class MainGame extends JApplet implements MouseListener, Runnable
{
private static final long serialVersionUID = 1L;
Thread animator = null;
Image blue, blueLit, red, redLit, green, greenLit, yellow, yellowLit;
boolean takingTurn;
boolean[] mouseOverTile = new boolean[4];

public void init()
{
setSize(500, 500);

blue = new ImageIcon("Images/Blue.png").getImage();
blueLit = new ImageIcon("Images/Blue_Lit.png").getImage();
red = new ImageIcon("Images/Red.png").getImage();
redLit = new ImageIcon("Images/Red_Lit.png").getImage();
green = new ImageIcon("Images/Green.png").getImage();
greenLit = new ImageIcon("Images/Green_Lit.png").getImage();
yellow = new ImageIcon("Images/Yellow.png").getImage();
yellowLit = new ImageIcon("Images/Yellow_Lit.png").getImage();
Arrays.fill(mouseOverTile, false);
takingTurn = true;
addMouseListener(this);
}

public void start()
{
if(animator == null)
{
animator = new Thread(this);
}
}

public void stop()
{
animator = null;
}

@Override
public void run()
{
while(Thread.currentThread() == animator)
{
try
{
Thread.sleep(1);
repaint();
}
catch(Exception e)
{ }
}
}

public void paint(Graphics g)
{
if(!mouseOverTile[0])
{
g.drawImage(blue, 50, 50, this);
} else g.drawImage(blueLit, 50, 50, this);
if(!mouseOverTile[1])
{
g.drawImage(red, 260, 50, this);
} else g.drawImage(redLit, 260, 50, this);
if(!mouseOverTile[2])
{
g.drawImage(green, 50, 260, this);
} else g.drawImage(greenLit, 50, 260, this);
if(!mouseOverTile[3])
{
g.drawImage(yellow, 260, 260, this);
} else g.drawImage(yellowLit, 260, 260, this);
}


@Override
public void mouseClicked(MouseEvent e)
{
System.out.println("Mouse X: " + e.getX());
System.out.println("Mouse Y: " + e.getY());
}

@Override
public void mouseEntered(MouseEvent e)
{
if(takingTurn)
{
if(e.getX() > 50 && e.getX() < 250)
{
mouseOverTile[0] = true;
System.out.println(mouseOverTile[0]);
}
}
}

@Override
public void mouseExited(MouseEvent e)
{
if(takingTurn)
{
if(e.getX() < 50 || e.getX() > 250)
{
mouseOverTile[0] = false;
System.out.println(mouseOverTile[0]);
}
}
}

@Override
public void mousePressed(MouseEvent arg0) {
// TODO Auto-generated method stub

}


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

}

}

最佳答案

您尚未更新 View 。
当您进行一些更改时,调用repaint()
例如,在 mouseEntered(...) 内的代码中:

@Override
public void mouseEntered(MouseEvent e)
{
if(takingTurn)
{
if(e.getX() > 50 && e.getX() < 250)
{
mouseOverTile[0] = true;
System.out.println(mouseOverTile[0]);
repaint();
}
}
}

关于java - JApplet - 当 boolean 值为 true 时不会绘制图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25559052/

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