重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
import java.awt.*;
成都创新互联2013年至今,是专业互联网技术服务公司,拥有项目成都网站制作、网站设计、外贸网站建设网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元大余做网站,已为上家服务,为大余各地企业和个人服务,联系电话:18980820575
import javax.swing.*;
public class Chessboard extends JApplet {
int baseXPosition, baseYPosition;
int currentXPosition, currentYPosition;
public void init() {
baseXPosition = 40; // 棋盘的开始x位置
baseYPosition = 40; // 棋盘的开始y位置
setBackground(Color.black); // 设置背景颜色黑色
}
public void paint(Graphics g) { // 绘制棋盘
currentXPosition = baseXPosition; // currentXPosition当前的x位置
currentYPosition = baseYPosition; // currentYPosition当前的y位置
for (int row = 0; row 8; row++) {
currentXPosition = baseXPosition + row * 40;
for (int column = 0; column 8; column++) {
if ((column + row) % 2 == 0)
g.setColor(Color.white); // 设置棋盘格子的颜色
else
g.setColor(Color.red); // 设置棋盘格子的颜色
currentYPosition = baseXPosition + column * 40;
g.drawRect(currentXPosition,currentYPosition,40,40);//;代码4 //在当前位置绘制棋盘的格子;每个格子的大小是40*40像
g.fillRect(currentXPosition,currentYPosition,40,40);
}
}
}
}
/*首先,
应用程序必须为 Canvas 类创建子类,以获得有用的功能(如创建自定义组件)。必须重写 paint 方法,以便在 canvas 上执行自定义图形。
以下是代码
*/
import java.awt.Canvas;
import java.awt.Font;
import javax.swing.*;
public class P {
public static void main(String[] args){
JFrame jf=new JFrame();
jf.setVisible(true);
jf.setLocation(200,200);
jf.setSize(300, 300);
jf.add(new a());
}
}
class a extends Canvas{
public void paint(java.awt.Graphics g){
int j=0,i=0;
while(i6){//for循环也可以
g.drawLine(0, 20*i, 100, 20*i);//横线,y坐标不变
g.drawLine(20*i, 0, 20*i, 100);
i++;
}
g.drawString("楚河汉界", 20, 130);
i=0;
while(i6){
g.drawLine(0,160+20*i, 100, 160+20*i);
g.drawLine(20*i, 160, 20*i, 260);
i++;}
}
}
虽然画出来了,但我想简化两个循环,如下:
class a extends Canvas{
public void paint(java.awt.Graphics g){
int j=0,i=0;
do{//用for循环也可以
if(j==0){
g.drawLine(0, 20*i, 100, 20*i);//横线
g.drawLine(20*i, 0, 20*i, 100);
}
else{
g.drawLine(0,160+20*i, 100, 160+20*i);
g.drawLine(20*i, 160, 20*i, 260);
}
if(i==5j==0){j=1;i=-1;g.drawString("楚河汉界", 20, 130);}//因为判断第一次之后,i会被加一次,而这次是没有画线的,会导致少了一条线,所以i=-1
i++;
}while(i6);
}
}
import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.beans.*;
import java.util.*;
/**
* 面板上的光标改变成为手型
* @author hardneedl
*/
final class ChessPaint extends JFrame{
private static final Dimension SIZE = new Dimension(600,400);
public Dimension getMinimumSize() {return SIZE;}
public Dimension getMaximumSize() {return SIZE;}
public Dimension getPreferredSize() {return SIZE;}
public String getTitle() {return "ChessPaint";}
/**
* 棋盘
* 实现属性变化监听
*/
private class Chessboard extends Observable implements PropertyChangeListener{
private int columns, rows, cellWidth;
private PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(this);
private Chessboard(int columns, int rows, int cellWidth) {
this.columns = columns;
this.rows = rows;
this.cellWidth = cellWidth;
}
private int getColumns() {
return columns;
}
private void setColumns(int columns) {
propertyChangeSupport.firePropertyChange("columns", this.columns, columns);
this.columns = columns;
}
private int getRows() {
return rows;
}
private void setRows(int rows) {
propertyChangeSupport.firePropertyChange("columns", this.rows, rows);
this.rows = rows;
}
private int getCellWidth() {
return cellWidth;
}
private void setCellWidth(int cellWidth) {
this.cellWidth = cellWidth;
}
public void propertyChange(PropertyChangeEvent evt) {
switch (evt.getPropertyName()) {
case "columns":
case "rows":
case "cellWidth":
if (!evt.getOldValue().equals(evt.getNewValue())) {
setChanged();
notifyObservers(this);
}
break;
}
}
}
/**
* 画笔
*/
private interface Brush {
void paint(Graphics g);
Component getCompoment();
}
abstract private class ChessboardBrush implements Brush, Observer {
private Chessboard chessboard;
private ChessboardBrush(Chessboard chessboard) {
this.chessboard = chessboard;
}
public void paint(Graphics g) {
if (chessboard == null) return;
Graphics2D g2 = (Graphics2D) g.create();
//背景白色
g2.setColor(Color.WHITE);
g2.fillRect(0,0, getCompoment().getWidth(), getCompoment().getHeight());
//整体偏移坐标系
g2.translate(100,100);
g2.setColor(Color.BLACK);
//绘制行线
for (int r = 0; r = chessboard.getRows(); r ++)
g2.drawLine(0, r * chessboard.getCellWidth(), chessboard.getColumns() * chessboard.getCellWidth(), r * chessboard.getCellWidth());
//绘制竖线
for (int c = 0; c = chessboard.getColumns(); c++)
g2.drawLine(c * chessboard.getCellWidth(), 0, chessboard.getCellWidth() * c , chessboard.getRows() * chessboard.getCellWidth());
g2.dispose();
}
public void update(Observable o, Object arg) {
if (arg instanceof Chessboard)
chessboard = (Chessboard)arg;
}
}
/*画布*/
private class Canvas extends JComponent{
private Brush getBrush() {
return brush;
}
private void setBrush(Brush brush) {
this.brush = brush;
}
private Brush brush;
private Canvas() {
super();
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if(isVisible() brush != null)
brush.paint(g);
}
public Border getBorder() {
return BorderFactory.createLineBorder(Color.BLUE,2);
}
}
private Canvas canvas;
private ChessboardBrush brush;
private Chessboard chessboard;
private ChessPaint() {
super();
init();
addListeners();
doLay();
}
private void init(){
chessboard = new Chessboard(19,19,30);
canvas = new Canvas();
brush = new ChessboardBrush(chessboard) {
public Component getCompoment() {
return canvas;
}
};
canvas.setBrush(brush);
chessboard.addObserver(brush);
}
private void addListeners(){
}
private void doLay(){
Container container = getContentPane();
container.add(canvas, BorderLayout.CENTER);
pack();
setVisible(true);
}
public static void main(String... args) {
System.setProperty("swing.defaultlaf","com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
SwingUtilities.invokeLater(ChessPaint::new);
}
}
代码如下:
// App.java
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import javax.swing.JFrame;
public class App extends JFrame {
public App() {
this.setTitle("Chess");
this.setSize(618, 647);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
@Override
public void paint(Graphics g) {
super.paint(g);
Rectangle rect = getRootPane().getBounds();
int blockWidth = rect.width / 8;
int blockHeight = rect.height / 8;
for (int i = 0; i 8; i++) {
for (int j = 0; j 8; j++) {
if (i % 2 == 0) {
g.setColor(j % 2 == 0 ? Color.RED : Color.BLACK);
} else {
g.setColor(j % 2 == 0 ? Color.BLACK : Color.RED);
}
g.fillRect(rect.x + j * blockWidth, rect.y + i * blockHeight, blockWidth, blockHeight);
}
}
}
public static void main(String[] args) {
new App().setVisible(true);
}
}
运行结果:
其实我也有用JAVA做五子棋呢~,棋盘都是用画的,我把代码发下,你自己试下,也不定合你一意.事件代码我都去啦,因为是简单的麻烦事.~!
import java.awt.*;
import javax.swing.*;
@SuppressWarnings("serial")
public class ChessBoard extends JPanel{
/*
* 制作棋盘的宽高;
*/
public static final int BOARD_WIDTH=515;
/*
* 计算棋盘表格坐标(单元格宽高相等)
*/
public static int [] location=new int[22];
static{
for(int i=0,WIDTH=30;ilocation.length;i++,WIDTH+=22){
location[i]=WIDTH;
}
}
public ChessBoard(int x,int y){
super(null);
this.setBounds(x, y, BOARD_WIDTH, BOARD_WIDTH);
this.setBackground(new Color(255, 164, 85));
}
/**
* 重写方法,绘制棋盘表格图;
*/
public void paintComponent(Graphics g){
super.paintComponent(g);
char ch='A';
g.setFont(new Font("宋体",Font.BOLD,12));
//画横线
for(int i=0,width=30+22*21;ilocation.length;i++,ch++){
g.setColor(Color.black);
g.drawLine(30,location[i],width,location[i]);
g.setColor(Color.blue);
g.drawString(""+ch,5,location[i]+3);
}
//画竖线
for(int i=0,width=30+22*21;ilocation.length;i++){
g.setColor(Color.black);
g.drawLine(location[i],30,location[i],width);
g.setColor(Color.blue);
g.drawString(""+(i+1),location[i]-3,13);
}
}
}