Java 通过TictaToe按钮创建线条
当有三个Java 通过TictaToe按钮创建线条,java,swing,jframe,Java,Swing,Jframe,当有三个按钮s具有相同的符号时,是否可以在tictactoe中的按钮s之间划线,例如: 如果是这样,怎么做呢?如果您使用的是JFrame,那么就很容易了。试试这个: if(shape1==shape2 && shape2==shape3){ public void paint(Graphics g) { super.paint(g); Graphics2D g2 = (Graphics2D) g; Line2D l
按钮
s具有相同的符号时,是否可以在tictactoe中的按钮
s之间划线,例如:
如果是这样,怎么做呢?如果您使用的是JFrame,那么就很容易了。试试这个:
if(shape1==shape2 && shape2==shape3){
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2 = (Graphics2D) g;
Line2D lin = new Line2D.Float(100, 100, 250, 260);
g2.draw(lin);
}
}
当然,坐标取决于形状的位置。一种方法是使用一些
布尔逻辑和自定义JButton
类以及一些自定义绘图。例如,在下面的示例中,有一个leftRight
标志。如果存在水平tic tac toe,则将升起leftRight
标志,导致在tic tac toe中的每个按钮中绘制线
请原谅我的tic-tac-toe编程逻辑,它不是最终的游戏产品。它只给出了一个例子,顶排的tic-tac-toe的O's。但我相信你能想出另一种抽搐的逻辑。例如,对于对角线,您可以有一个leftDiag
标志,如果升起,它将绘制绘图线(0,0,getWidth(),getHeight())
玩它。享受吧
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.border.LineBorder;
public class TTTButtons {
ImageIcon oooIcon;
MyButton[][] buttons = new MyButton[3][3];
JPanel gamePanel;
public TTTButtons() {
oooIcon = new ImageIcon(getClass().getResource("/resources/ooooo.png"));
gamePanel = createGamePanel();
JFrame frame = new JFrame("TicTacToe");
frame.add(gamePanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
private JPanel createGamePanel() {
JPanel panel = new JPanel(new GridLayout(3, 3));
for (int i = 0; i < buttons.length; i++) {
for (int j = 0; j < buttons[i].length; j++) {
buttons[i][j] = new MyButton(oooIcon);
panel.add(buttons[i][j]);
}
}
return panel;
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new TTTButtons();
}
});
}
public class MyButton extends JButton {
Image oooImage;
boolean leftRight = false;
boolean clicked = false;
boolean ooo = false;
boolean tictactoe = false;
public MyButton(ImageIcon oooImage) {
this.oooImage = oooImage.getImage();
addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
clicked = true;
ooo = true;
if (buttons[0][0].clicked && buttons[0][1].clicked && buttons[0][2].clicked) {
for (int i = 0; i < 3; i++) {
buttons[0][i].leftRight = true;
buttons[0][i].tictactoe = true;
buttons[0][i].repaint();
}
return;
}
repaint();
}
});
setBorder(new LineBorder(Color.BLACK, 1));
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (clicked) {
if (ooo) {
g.drawImage(oooImage, 0, 0, getWidth(), getHeight(), this);
}
} else {
g.setColor(Color.GRAY);
g.fillRect(0, 0, getWidth(), getHeight());
}
if (tictactoe) {
if (leftRight) {
g.drawLine(0, (int) (getHeight() / 2), getWidth(), (int) (getHeight() / 2));
}
}
}
@Override
public Dimension getPreferredSize() {
return oooImage == null ? new Dimension(100, 100)
: new Dimension(oooImage.getWidth(this), oooImage.getHeight(this));
}
}
}
导入java.awt.Color;
导入java.awt.Dimension;
导入java.awt.Graphics;
导入java.awt.GridLayout;
导入java.awt.Image;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入javax.swing.ImageIcon;
导入javax.swing.JButton;
导入javax.swing.JFrame;
导入javax.swing.JPanel;
导入javax.swing.SwingUtilities;
导入javax.swing.border.LineBorder;
公共类TTT按钮{
图像图标oooIcon;
MyButton[][]按钮=新的MyButton[3][3];
JPanel游戏面板;
公共TTTButtons(){
oooIcon=newImageIcon(getClass().getResource(“/resources/oooooo.png”);
gamePanel=createGamePanel();
JFrame=新JFrame(“Tictatoe”);
frame.add(游戏面板);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(空);
frame.setVisible(true);
}
私有JPanel createGamePanel(){
JPanel面板=新JPanel(新网格布局(3,3));
对于(int i=0;i
是的,这是有可能的。你是想尝试任何东西还是只想要游戏代码?我只是想知道这是否可以实现,以及我需要使用哪些内置类。但是一个例子更受欢迎。在JavaSon中没有什么是不可能的!!是的,我使用的是JFrame.hi,在paint关键字上,我得到一个错误oid是变量paint的无效类型,我需要为此导入一些东西吗?尝试这些导入:import javax.swing.*;导入java.awt.*;导入java.awt.geom.*;它应该处理这些进口产品。我不明白为什么不是。我会远离像JFrame这样的顶级容器,因为它们已经承担了绘制组件的负担。相反,使用JPanel
或JComponent
并覆盖paintComponent
,这样您就知道正在创建的线条,是图像还是图形/绘画类中的线条?顺便说一句,我得到了一个空指针错误,不管怎样解决了这个问题。这是我正在绘制的线条。查看MyButton
类中的paintComponent
方法。有一个提升的tictactoe
标志将绘制线条。是的,请确保使用正确的图像路径。在我的例子中,我的resources
是src