gpt4 book ai didi

java - 编码风险(游戏): Making buttons shape of territories

转载 作者:行者123 更新时间:2023-11-30 05:54:03 25 4
gpt4 key购买 nike

我正在为学校的一个项目用 Java 编写棋盘游戏 Risk。我需要让每个地区成为一个单独的按钮。我将每个区域分成具有透明背景的不同图片。我知道 2D java 按钮是面向矩形的,所以我可能不得不使用 alpha 值,因为我已经为每个区域的背景提供了透明背景。

如果我这样做,会出现两个问题:

  1. 我不知道按钮的透明区域是否被按下。
  2. 按钮由于其固有的矩形形状而重叠,我只需要包含被单击的非透明像素的按钮即可启动。

或者是否有更有效的方法来编写游戏代码?欢迎任何帮助,因为这是非常需要的。我在网上找到了java版的 table 游,但是看不懂。 (它被标记为统治,但实际上是风险。 Link to the map || The link to the game I found online

最佳答案

这是我制作的东西,您可以继续使用它来仅在没有透明度的区域上感知运动。

IMAGEBUTTON.java 就像 JButton 一样工作,除了你给它一个 imageIcon 并且它感知 alpha 提供的类

如果你有任何来自 png 的阴影,请使用 setMinimumDetectionAlpha()。

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import javax.swing.ImageIcon;

@SuppressWarnings("serial")
public class ImageButton extends javax.swing.JButton implements MouseListener{

private MouseListener mouseListener = null;
private BufferedImage bufferedImage;
private int minAlpha = 0x0;
private boolean userMouseListener = false;

public ImageButton(ImageIcon ic){
super();
setPreferredSize(new Dimension(ic.getIconWidth(), ic.getIconHeight()));
setImage(ic);
addMouseListener(this);
}
protected BufferedImage imageIconToBufferedImage(ImageIcon a){
BufferedImage bi = new BufferedImage(a.getIconWidth(), a.getIconHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = (Graphics2D) bi.getGraphics();
g2d.drawImage(a.getImage(), 0,0,null);
g2d.dispose();
return bi;
}
public void setImage(ImageIcon ic){
bufferedImage = imageIconToBufferedImage(ic);
repaint();
}
protected void paintComponent(Graphics g){
Graphics2D g2d = (Graphics2D) g;
RenderingHints _rh = g2d.getRenderingHints();
g2d.setRenderingHint(
RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.drawImage(bufferedImage, 0, 0, null);
g2d.setRenderingHints(_rh);
}
protected void setMinimumDetectionAlpha(int alpha){
minAlpha = alpha == 0 ? 1 : alpha;
minAlpha = minAlpha % 0x000000FF << 0x00000010;
}
protected void processMouseEvent(MouseEvent e) {
if(checkInBounds(e.getX(), e.getY())){
switch(e.getID()){
case MouseEvent.MOUSE_CLICKED: mouseListener.mouseClicked(e); break;
case MouseEvent.MOUSE_ENTERED: mouseListener.mouseEntered(e); break;
case MouseEvent.MOUSE_EXITED: mouseListener.mouseExited(e); break;
case MouseEvent.MOUSE_PRESSED: mouseListener.mousePressed(e); break;
case MouseEvent.MOUSE_RELEASED: mouseListener.mouseReleased(e); break;
}
e.consume();
}
}
public void addMouseListener(MouseListener a){
if(!userMouseListener)
super.addMouseListener(a);
else
mouseListener = a;
userMouseListener = true;
}
protected boolean checkInBounds(int x, int y){
try{
if((bufferedImage.getRGB(x, y) & 0xFF000000)>>>4 >= (minAlpha > 0 ? minAlpha : 0x08800000))
return true;
else
throw new Exception();
}catch(Exception e){
return false;
}
}
@Override
public void mouseClicked(MouseEvent e) { processMouseEvent(e); }
@Override
public void mouseEntered(MouseEvent e) { processMouseEvent(e); }
@Override
public void mouseExited(MouseEvent e) { processMouseEvent(e); }
@Override
public void mousePressed(MouseEvent e) { processMouseEvent(e); }
@Override
public void mouseReleased(MouseEvent e) { processMouseEvent(e); }
}

你可以像这样继续使用它

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.ImageIcon;
import javax.swing.JFrame;


public class Test {

/**
* @param args
*/
public static void main(String[] args) {
JFrame jmb = new JFrame();
ImageButton ib = new ImageButton(new ImageIcon("res/buttonImage.png"));
ib.setMinimumDetectionAlpha(0);
ib.addMouseListener(new MouseListener(){

@Override
public void mouseClicked(MouseEvent e) {
System.out.println("eventFired");

}

@Override
public void mouseEntered(MouseEvent e) {
System.out.println("eventFired");

}

@Override
public void mouseExited(MouseEvent e) {
System.out.println("eventFired");

}

@Override
public void mousePressed(MouseEvent e) {
System.out.println("eventFired");

}

@Override
public void mouseReleased(MouseEvent e) {
System.out.println("eventFired");

}

});
jmb.add(ib);
jmb.pack();
jmb.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jmb.show();
}

}

使用的图像是这样的

buttonImage.png

希望对你有帮助:D

关于java - 编码风险(游戏): Making buttons shape of territories,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9726219/

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