我有一个带有六边形的板,我想每次按六角形来更改颜色,但是当我按六角形时,我希望它们更改颜色和我按的以前的六边形。问题出在我的setSelected
类中,因为它绘制了我已经选择的所有内容,但我不知道如何解决。
这是代码。
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
class OmegaPanel extends JPanel {
private int[][] board;
private final Color[] colors = {Color.GRAY, Color.WHITE, Color.BLACK};
Hexagon[][] comb;
private int turn = 1;
private int colour = 1;
// private AI ai;
public OmegaPanel() {
setBackground(Color.white);
setFocusable(true);
String[] opts = {"Black", "White"};
colour = JOptionPane.showOptionDialog(null, "Which colour would you like to play as?", "Choose Colour", JOptionPane.YES_NO_OPTION, JOptionPane.PLAIN_MESSAGE, null, opts, opts[0]);
addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
if (comb[i][j] != null) {
if (comb[i][j].contains(e.getX(), e.getY())) {
comb[i][j].setSelected();
playAt(i,j);
break;
}
repaint();
}
}
}
}
});
comb = new Hexagon[9][9];
int size = 5;
int row = 0;
int x1 = 100, x2 = 82, x3 = 64, x4 = 46, x5 = 28, x6 = 10, x7 = -8, x8 = -26, x9 = -44, y1 = 36;
int x = 0, y = 0;
for (int i = 0; i < 61; i++) {
int col = 0;
if (i < size) {
x = x1 + i * 36;
y = y1;
col = 4 + i;
row = 0;
} else if (i < size + 6) {
x = x2 + (i - 5) * 36;
y = y1 + 32;
col = 3 + (i - 5);
row = 1;
} else if (i < size + 13) {
x = x3 + (i - 11) * 36;
y = y1 + 2 * 32;
col = 2 + (i - 11);
row = 2;
} else if (i < size + 21) {
x = x4 + (i - 18) * 36;
y = y1 + 3 * 32;
col = 1 + (i - 18);
row = 3;
} else if (i < size + 30) {
x = x5 + (i - 26) * 36;
y = y1 + 4 * 32;
col = i - 26;
row = 4;
} else if (i < size + 38 && col < 8) {
x = x6 + (i - 34) * 36;
y = y1 + 5 * 32;
col = i - 35;
row = 5;
} else if (i < size + 45 && col < 7) {
x = x7 + (i - 41) * 36;
y = y1 + 6 * 32;
col = i - 43;
row = 6;
} else if (i < size + 51 && col < 6) {
x = x8 + (i - 47) * 36;
y = y1 + 7 * 32;
col = i - 50;
row = 7;
} else if (i < size + 56 && col < 5) {
x = x9 + (i - 52) * 36;
y = y1 + 8 * 32;
col = i - 56;
row = 8;
}
comb[row][col] = new Hexagon(x, y);
}
requestFocus();
}
@Override
public void paintComponent(Graphics gg) {
super.paintComponent(gg);
Graphics2D g = (Graphics2D) gg;
g.setStroke(new BasicStroke(3));
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
if (comb[i][j] != null) {
if (turn == 1) {
comb[i][j].draw1(g);
} else if (turn == 2) {
comb[i][j].draw2(g);
}
}
}
}
}
private int playAt(int x ,int y) {
if (turn==2) {
turn--;
repaint();
} else {
turn++;
repaint();
}
return turn;
}
}
import java.awt.*;
class Hexagon extends Polygon {
final Color baseColor = Color.green;
final Color selectedColor1 = Color.white;
final Color selectedColor2 = Color.black;
private boolean hasBeenSelected;
Hexagon(int x, int y) {
for (int i = 0; i < 6; i++)
addPoint((int) (x + 20 * Math.sin(i * Math.PI / 3)),
(int) (y + 20 * Math.cos(i * Math.PI / 3)));
getBounds();
}
void setSelected() {
hasBeenSelected = true;
}
void draw1(Graphics2D g) {
g.setColor(hasBeenSelected ? selectedColor1 : baseColor);
g.fillPolygon(this);
g.setColor(Color.black);
g.drawPolygon(this);
}
void draw2(Graphics2D g) {
g.setColor(hasBeenSelected ? selectedColor2 : baseColor);
g.fillPolygon(this);
g.setColor(Color.black);
g.drawPolygon(this);
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Main {
public static void main(String[] args) {
JFrame f = new JFrame("Omega Game");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
f.setContentPane(new OmegaPanel());
f.setLocationRelativeTo(null);
f.setSize(1200, 800);
}
}