50,686
社区成员
发帖
与我相关
我的任务
分享
package wzq.panel;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import wzq.chess.Chess;
public class ChessPanel extends JPanel implements MouseListener {
/**
*
*/
private static final long serialVersionUID = 1L;
int x,y;
int X,Y;
public int getX() {
return X;
}
public void setX(int x) {
X = x;
}
public int getY() {
return Y;
}
public void setY(int y) {
Y = y;
}
int num[][] = new int[15][15];
public int CovertXY(int x){
if(x<40){
x =1;
return x * 40 -20;
}else if(x %40 >20){
x = x/40;
return x*40 +20;
}
x = x /40;
return x * 40 - 20;
}
public Chess chess = new Chess();
public ChessPanel(){
addMouseListener(this);
}
public static void main(String[] args) {
JFrame frame = new JFrame("棋盘");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ChessPanel cp =new ChessPanel();
cp.setBackground(Color.ORANGE);
frame.getContentPane().add(cp, "Center");
frame.setBounds(1000, 200, 620, 650);
frame.setVisible(true);
}
public void paintComponent(Graphics g){
super.paintComponent(g);
for(int i=0;i<chess.getChess_row();i++){
for(int j=0;j<chess.getChess_cloumn();j++){
g.setColor(Color.black);
g.drawRect(i*40, j*40, chess.chess_grid, chess.chess_grid);
}
}
if(num[x][y] == 0){
g.setColor(Color.black);
g.fillOval(getX(), getY(), 40, 40);
}
}
@Override
public void mouseClicked(MouseEvent e) {}
@Override
public void mousePressed(MouseEvent e) {
X= CovertXY(e.getX());
Y= CovertXY(e.getY());
System.out.println();
System.out.println(e.getX());
System.out.println(e.getY());
System.out.println(X);
System.out.println(Y);
repaint();
}
@Override
public void mouseReleased(MouseEvent e) {}
@Override
public void mouseEntered(MouseEvent e) {}
@Override
public void mouseExited(MouseEvent e) { }
}