如何将鼠标监听器添加到使用 graph.drawimage() 插入的图像?
How to add a mouselistener to images which are inserted using graph.drawimage()?
public class buttonActionStart implements ActionListener { // overwritten ActionListener
public void actionPerformed (ActionEvent e){
JFrame window2 = new JFrame("Main Game"); //create empty frame
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
window2.setSize(screenSize.width,screenSize.height);
window2.add(new JPanel(){
public void paintComponent(Graphics graph){
super.paintComponent(graph);
Board board = new Board();
graph.drawString(board.sqaures[1].getName(),440,560);
graph.drawString(board.sqaures[2].getName(),335,560);
graph.drawString(board.sqaures[3].getName(),225,560);
graph.drawString(board.sqaures[4].getName(),145,560);
graph.drawString(board.sqaures[5].getName(),30,560);
graph.drawString(board.sqaures[6].getName(),25,460);
graph.drawString(board.sqaures[7].getName(),25,360);
graph.drawString(board.sqaures[8].getName(),35,260);
graph.drawString(board.sqaures[9].getName(),25,160);
graph.drawString(board.sqaures[10].getName(),25,60);
graph.drawString(board.sqaures[11].getName(),140,60);
graph.drawString(board.sqaures[12].getName(),240,60);
graph.drawString(board.sqaures[13].getName(),340,60);
graph.drawString(board.sqaures[14].getName(),440,60);
graph.drawString(board.sqaures[15].getName(),530,60);
graph.drawString(board.sqaures[16].getName(),550,160);
graph.drawString(board.sqaures[17].getName(),545,260);
graph.drawString(board.sqaures[18].getName(),540,360);
graph.drawString(board.sqaures[19].getName(),550,460);
for (int i = 0; i < 6; i++)
graph.drawRect(10 + (i*100),10,100,100);
for (int j = 0; j < 4; j++)
graph.drawRect(10,110 + (j*100),100,100);
for (int k = 0; k < 4; k++)
graph.drawRect(510,110 + (k*100),100,100);
for (int l = 0; l < 6; l++)
graph.drawRect(10 + (l*100),510,100,100);
InputStream resource = UI.class.getResourceAsStream("player1.png");
Image player1Image = null;
try{
player1Image = ImageIO.read(resource);
}catch (IOException e){
e.printStackTrace();
}
resource = UI.class.getResourceAsStream("player2.png");
Image player2Image = null;
try{
player2Image = ImageIO.read(resource);
}catch (IOException e){
e.printStackTrace();
}
resource = UI.class.getResourceAsStream("player3.png");
Image player3Image = null;
try{
player3Image = ImageIO.read(resource);
}catch (IOException e){
e.printStackTrace();
}
resource = UI.class.getResourceAsStream("player4.png");
Image player4Image = null;
try{
player4Image = ImageIO.read(resource);
}catch (IOException e){
e.printStackTrace();
}
graph.drawImage(player1Image, 511, 511, null);
graph.drawImage(player2Image, 585, 510, null);
graph.drawImage(player3Image, 511, 585, null);
graph.drawImage(player4Image, 585, 585, null);
}
});
window2.setVisible(true);
}
}
我正在开发一种棋盘游戏,这四个图像将作为玩家图标。我希望玩家单击手动分配给他们的图像,然后单击屏幕上的其他位置,图像将 "updated" 到该位置。
"square"和board"将是我自定义的class。
我得到了另一个 window,它将有一个 "start" 按钮并触发上面的图形绘制在一个新的 window 上。
我确实发现很多人建议使用 JLabel 添加图像并将 JLabel 添加到 JFrame,但我发现图像会覆盖我的图形。
两个选项:
- 按照建议操作并使用 JLabel。说 "the image will cover my graphics",您的意思可能是图像的透明部分不会显示背景。如果是,请调用
JLabel.setOpaque(false)
.
- 将 MouseListener 添加到您为其实现
paintComponent()
方法(可能是 JFrame 实现)的组件。每当用户单击组件时,您都会收到鼠标事件。在侦听器实现中,您必须手动检查坐标是否落入其中一张图像的区域。
一般来说,对于游戏我更喜欢选项 2:使用空白 canvas 并将游戏场景绘制到 canvas 中。然后,用户交互由通用游戏逻辑对象管理,该对象根据坐标/按下的键等
委托给 'player'、'NPC' 等游戏对象
public class buttonActionStart implements ActionListener { // overwritten ActionListener
public void actionPerformed (ActionEvent e){
JFrame window2 = new JFrame("Main Game"); //create empty frame
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
window2.setSize(screenSize.width,screenSize.height);
window2.add(new JPanel(){
public void paintComponent(Graphics graph){
super.paintComponent(graph);
Board board = new Board();
graph.drawString(board.sqaures[1].getName(),440,560);
graph.drawString(board.sqaures[2].getName(),335,560);
graph.drawString(board.sqaures[3].getName(),225,560);
graph.drawString(board.sqaures[4].getName(),145,560);
graph.drawString(board.sqaures[5].getName(),30,560);
graph.drawString(board.sqaures[6].getName(),25,460);
graph.drawString(board.sqaures[7].getName(),25,360);
graph.drawString(board.sqaures[8].getName(),35,260);
graph.drawString(board.sqaures[9].getName(),25,160);
graph.drawString(board.sqaures[10].getName(),25,60);
graph.drawString(board.sqaures[11].getName(),140,60);
graph.drawString(board.sqaures[12].getName(),240,60);
graph.drawString(board.sqaures[13].getName(),340,60);
graph.drawString(board.sqaures[14].getName(),440,60);
graph.drawString(board.sqaures[15].getName(),530,60);
graph.drawString(board.sqaures[16].getName(),550,160);
graph.drawString(board.sqaures[17].getName(),545,260);
graph.drawString(board.sqaures[18].getName(),540,360);
graph.drawString(board.sqaures[19].getName(),550,460);
for (int i = 0; i < 6; i++)
graph.drawRect(10 + (i*100),10,100,100);
for (int j = 0; j < 4; j++)
graph.drawRect(10,110 + (j*100),100,100);
for (int k = 0; k < 4; k++)
graph.drawRect(510,110 + (k*100),100,100);
for (int l = 0; l < 6; l++)
graph.drawRect(10 + (l*100),510,100,100);
InputStream resource = UI.class.getResourceAsStream("player1.png");
Image player1Image = null;
try{
player1Image = ImageIO.read(resource);
}catch (IOException e){
e.printStackTrace();
}
resource = UI.class.getResourceAsStream("player2.png");
Image player2Image = null;
try{
player2Image = ImageIO.read(resource);
}catch (IOException e){
e.printStackTrace();
}
resource = UI.class.getResourceAsStream("player3.png");
Image player3Image = null;
try{
player3Image = ImageIO.read(resource);
}catch (IOException e){
e.printStackTrace();
}
resource = UI.class.getResourceAsStream("player4.png");
Image player4Image = null;
try{
player4Image = ImageIO.read(resource);
}catch (IOException e){
e.printStackTrace();
}
graph.drawImage(player1Image, 511, 511, null);
graph.drawImage(player2Image, 585, 510, null);
graph.drawImage(player3Image, 511, 585, null);
graph.drawImage(player4Image, 585, 585, null);
}
});
window2.setVisible(true);
}
}
我正在开发一种棋盘游戏,这四个图像将作为玩家图标。我希望玩家单击手动分配给他们的图像,然后单击屏幕上的其他位置,图像将 "updated" 到该位置。
"square"和board"将是我自定义的class。
我得到了另一个 window,它将有一个 "start" 按钮并触发上面的图形绘制在一个新的 window 上。
我确实发现很多人建议使用 JLabel 添加图像并将 JLabel 添加到 JFrame,但我发现图像会覆盖我的图形。
两个选项:
- 按照建议操作并使用 JLabel。说 "the image will cover my graphics",您的意思可能是图像的透明部分不会显示背景。如果是,请调用
JLabel.setOpaque(false)
. - 将 MouseListener 添加到您为其实现
paintComponent()
方法(可能是 JFrame 实现)的组件。每当用户单击组件时,您都会收到鼠标事件。在侦听器实现中,您必须手动检查坐标是否落入其中一张图像的区域。
一般来说,对于游戏我更喜欢选项 2:使用空白 canvas 并将游戏场景绘制到 canvas 中。然后,用户交互由通用游戏逻辑对象管理,该对象根据坐标/按下的键等
委托给 'player'、'NPC' 等游戏对象