- 五子棋
package com.lovo;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Stroke;
public class Board {
private int[][] b = new int[15][15];
private boolean blackTurn = true; // 是否轮到黑方走棋
/**
* 绘制棋盘
* @param g 画笔
*/
public void draw(Graphics g) {
g.setColor(Color.BLACK);
Graphics2D g2d = (Graphics2D) g;
Stroke oldStroke = g2d.getStroke(); // 记录画笔原来的粗细(保存现场)
g2d.setStroke(new BasicStroke(5)); // 修改画笔的粗细
g.drawRect(50, 50, 700, 700); // 绘制棋盘边框
g2d.setStroke(oldStroke); // 将画笔还原为原来的粗细(恢复现场)
// 绘制横纵线条
for (int i = 0; i < 13; i++) {
g.drawLine(50, 100 + 50 * i, 750, 100 + 50 * i);
g.drawLine(100 + 50 * i, 50, 100 + 50 * i, 750);
}
g.fillOval(395, 395, 10, 10); // 绘制天元
// 绘制四个星
g.fillOval(195, 195, 10, 10);
g.fillOval(595, 595, 10, 10);
g.fillOval(195, 595, 10, 10);
g.fillOval(595, 195, 10, 10);
// 画棋子
for(int i = 0; i < b.length; i++) { // 行控制纵坐标
for(int j = 0; j < b[i].length; j++) { // 列控制横坐标
if(b[i][j] != 0) {
g.setColor(b[i][j] == 1? Color.BLACK : Color.WHITE);
g.fillOval(25 + 50 * j, 25 + 50 * i, 50, 50);
}
}
}
}
/**
* 随机下棋
*/
public void makeAMove() {
int row = (int) (Math.random() * 15);
int col = (int) (Math.random() * 15);
if(b[row][col] == 0) {
b[row][col] = blackTurn ? 1 : 2; // 用数字1表示黑棋用; 数字2表示白棋
blackTurn = !blackTurn; // 交换走棋方
}
}
}
package com.lovo;
public class GameRunner {
public static void main(String[] args) {
new MyFrame().setVisible(true);
}
}
package com.lovo;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import javax.swing.JFrame;
import javax.swing.Timer;
@SuppressWarnings("serial")
public class MyFrame extends JFrame {
private Board board = new Board(); // 创建棋盘类的对象
private Image offImage = new BufferedImage(800, 800, BufferedImage.TYPE_INT_RGB);
public MyFrame() {
this.setTitle("五子棋"); // 设置窗口标题
this.setSize(800, 800); // 设置窗口宽度和高度
this.setResizable(false); // 设置窗口大小不可改变
this.setLocationRelativeTo(null); // 设置窗口居中
this.getContentPane().setBackground(new Color(150, 100, 50)); // 设置窗口背景色
this.setDefaultCloseOperation(EXIT_ON_CLOSE); // 关闭窗口时结束应用程序
Timer timer = new Timer(1000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
board.makeAMove();
repaint(); // 通知操作系统通知你的窗口调用paint方法重新绘制界面
}
});
timer.start();
}
@Override
public void paint(Graphics g) { // 重写此方法的目的是在窗口上绘制自己想要的东西
Graphics newG = offImage.getGraphics();
super.paint(newG);
board.draw(newG);
g.drawImage(offImage, 0, 0, 800, 800, null);
}
}
- 多态
package com.lovo;
import java.awt.Color;
import java.awt.Graphics;
// 如果一个类有抽象方法,这个类必须被声明为抽象类
// 抽象类不能实例化(不能创建对象),抽象类是专门用来被继承的
public abstract class Shape {
protected int x, y;
protected Color color;
/**
* 计算周长
* @return 图形的周长
*/
public abstract double getCircumference(); // 抽象方法没有实现
/**
* 计算面积
* @return 图形的面积
*/
public abstract double getArea();
/**
* 绘图
* @param g 画笔
*/
public abstract void draw(Graphics g);
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public Color getColor() {
return color;
}
public void setColor(Color color) {
this.color = color;
}
}
package com.lovo;
import java.awt.Graphics;
public class Rectangle extends Shape {
private int width, height;
public Rectangle(int width, int height) {
this.width = width;
this.height = height;
}
@Override
public double getCircumference() {
return 2 * (width + height);
}
@Override
public double getArea() {
return width * height;
}
@Override
public void draw(Graphics g) {
g.setColor(color);
g.drawRect(x, y, width, height);
}
}
package com.lovo;
import java.awt.Graphics;
public class Circle extends Shape {
private int radius;
public Circle(int radius) {
this.radius = radius;
}
@Override
public double getCircumference() {
return 2 * Math.PI * radius;
}
@Override
public double getArea() {
return Math.PI * radius * radius;
}
@Override
public void draw(Graphics g) {
g.setColor(color);
g.drawOval(x - radius, y - radius, 2 * radius, 2 * radius);
}
}
package com.lovo;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
@SuppressWarnings("serial")
public class MyFrame extends JFrame {
private Shape[] shapes = new Shape[5];
public MyFrame() {
this.setSize(800, 600);
this.setResizable(false);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
for(int i = 0; i < shapes.length; i++) {
int num = (int) (Math.random() * 2);
Shape currentShape = null;
switch(num) {
case 0:
int radius = (int) (Math.random() * 50 + 50);
currentShape = new Circle(radius);
break;
case 1:
int width = (int) (Math.random() * 300 + 100);
int height = (int) (Math.random() * 300 + 100);
currentShape = new Rectangle(width, height);
break;
}
int r = (int) (Math.random() * 256);
int g = (int) (Math.random() * 256);
int b = (int) (Math.random() * 256);
currentShape.setColor(new Color(r, g, b));
int x = (int) (Math.random() * 350 + 50);
int y = (int) (Math.random() * 350 + 50);
currentShape.setX(x);
currentShape.setY(y);
shapes[i] = currentShape;
}
}
@Override
public void paint(Graphics g) {
super.paint(g);
for(Shape sh : shapes) {
sh.draw(g);// 同样是Shape类型的引用,调用的是同样的draw方法,但却绘制出了不同的图形
// 这就是多态 (polymorphism)
}
}
public static void main(String[] args) {
new MyFrame().setVisible(true);
}
}
package com.lovo;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Stroke;
public class Board {
private int[][] b = new int[15][15];
private boolean blackTurn = true; // 是否轮到黑方走棋
/**
* 绘制棋盘
* @param g 画笔
*/
public void draw(Graphics g) {
g.setColor(Color.BLACK);
Graphics2D g2d = (Graphics2D) g;
Stroke oldStroke = g2d.getStroke(); // 记录画笔原来的粗细(保存现场)
g2d.setStroke(new BasicStroke(5)); // 修改画笔的粗细
g.drawRect(50, 50, 700, 700); // 绘制棋盘边框
g2d.setStroke(oldStroke); // 将画笔还原为原来的粗细(恢复现场)
// 绘制横纵线条
for (int i = 0; i < 13; i++) {
g.drawLine(50, 100 + 50 * i, 750, 100 + 50 * i);
g.drawLine(100 + 50 * i, 50, 100 + 50 * i, 750);
}
g.fillOval(395, 395, 10, 10); // 绘制天元
// 绘制四个星
g.fillOval(195, 195, 10, 10);
g.fillOval(595, 595, 10, 10);
g.fillOval(195, 595, 10, 10);
g.fillOval(595, 195, 10, 10);
// 画棋子
for(int i = 0; i < b.length; i++) { // 行控制纵坐标
for(int j = 0; j < b[i].length; j++) { // 列控制横坐标
if(b[i][j] != 0) {
g.setColor(b[i][j] == 1? Color.BLACK : Color.WHITE);
g.fillOval(25 + 50 * j, 25 + 50 * i, 50, 50);
}
}
}
}
/**
* 随机下棋
*/
public void makeAMove() {
int row = (int) (Math.random() * 15);
int col = (int) (Math.random() * 15);
if(b[row][col] == 0) {
b[row][col] = blackTurn ? 1 : 2; // 用数字1表示黑棋用; 数字2表示白棋
blackTurn = !blackTurn; // 交换走棋方
}
}
}
package com.lovo;
public class GameRunner {
public static void main(String[] args) {
new MyFrame().setVisible(true);
}
}
package com.lovo;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import javax.swing.JFrame;
import javax.swing.Timer;
@SuppressWarnings("serial")
public class MyFrame extends JFrame {
private Board board = new Board(); // 创建棋盘类的对象
private Image offImage = new BufferedImage(800, 800, BufferedImage.TYPE_INT_RGB);
public MyFrame() {
this.setTitle("五子棋"); // 设置窗口标题
this.setSize(800, 800); // 设置窗口宽度和高度
this.setResizable(false); // 设置窗口大小不可改变
this.setLocationRelativeTo(null); // 设置窗口居中
this.getContentPane().setBackground(new Color(150, 100, 50)); // 设置窗口背景色
this.setDefaultCloseOperation(EXIT_ON_CLOSE); // 关闭窗口时结束应用程序
Timer timer = new Timer(1000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
board.makeAMove();
repaint(); // 通知操作系统通知你的窗口调用paint方法重新绘制界面
}
});
timer.start();
}
@Override
public void paint(Graphics g) { // 重写此方法的目的是在窗口上绘制自己想要的东西
Graphics newG = offImage.getGraphics();
super.paint(newG);
board.draw(newG);
g.drawImage(offImage, 0, 0, 800, 800, null);
}
}
package com.lovo;
import java.awt.Color;
import java.awt.Graphics;
// 如果一个类有抽象方法,这个类必须被声明为抽象类
// 抽象类不能实例化(不能创建对象),抽象类是专门用来被继承的
public abstract class Shape {
protected int x, y;
protected Color color;
/**
* 计算周长
* @return 图形的周长
*/
public abstract double getCircumference(); // 抽象方法没有实现
/**
* 计算面积
* @return 图形的面积
*/
public abstract double getArea();
/**
* 绘图
* @param g 画笔
*/
public abstract void draw(Graphics g);
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public Color getColor() {
return color;
}
public void setColor(Color color) {
this.color = color;
}
}
package com.lovo;
import java.awt.Graphics;
public class Rectangle extends Shape {
private int width, height;
public Rectangle(int width, int height) {
this.width = width;
this.height = height;
}
@Override
public double getCircumference() {
return 2 * (width + height);
}
@Override
public double getArea() {
return width * height;
}
@Override
public void draw(Graphics g) {
g.setColor(color);
g.drawRect(x, y, width, height);
}
}
package com.lovo;
import java.awt.Graphics;
public class Circle extends Shape {
private int radius;
public Circle(int radius) {
this.radius = radius;
}
@Override
public double getCircumference() {
return 2 * Math.PI * radius;
}
@Override
public double getArea() {
return Math.PI * radius * radius;
}
@Override
public void draw(Graphics g) {
g.setColor(color);
g.drawOval(x - radius, y - radius, 2 * radius, 2 * radius);
}
}
package com.lovo;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
@SuppressWarnings("serial")
public class MyFrame extends JFrame {
private Shape[] shapes = new Shape[5];
public MyFrame() {
this.setSize(800, 600);
this.setResizable(false);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
for(int i = 0; i < shapes.length; i++) {
int num = (int) (Math.random() * 2);
Shape currentShape = null;
switch(num) {
case 0:
int radius = (int) (Math.random() * 50 + 50);
currentShape = new Circle(radius);
break;
case 1:
int width = (int) (Math.random() * 300 + 100);
int height = (int) (Math.random() * 300 + 100);
currentShape = new Rectangle(width, height);
break;
}
int r = (int) (Math.random() * 256);
int g = (int) (Math.random() * 256);
int b = (int) (Math.random() * 256);
currentShape.setColor(new Color(r, g, b));
int x = (int) (Math.random() * 350 + 50);
int y = (int) (Math.random() * 350 + 50);
currentShape.setX(x);
currentShape.setY(y);
shapes[i] = currentShape;
}
}
@Override
public void paint(Graphics g) {
super.paint(g);
for(Shape sh : shapes) {
sh.draw(g);// 同样是Shape类型的引用,调用的是同样的draw方法,但却绘制出了不同的图形
// 这就是多态 (polymorphism)
}
}
public static void main(String[] args) {
new MyFrame().setVisible(true);
}
}