围棋
package ch11;
/**
* Created by Jiqing on 2016/12/4.
*/
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.imageio.*;
import java.io.*;
public class Gobang
{
// 下面三个位图分别代表棋盘、黑子、白子
BufferedImage table;
BufferedImage black;
BufferedImage white;
// 当鼠标移动时候的选择框
BufferedImage selected;
// 定义棋盘的大小
private static int BOARD_SIZE = 15;
// 定义棋盘宽、高多少个像素
private final int TABLE_WIDTH = 535;
private final int TABLE_HETGHT = 536;
// 定义棋盘坐标的像素值和棋盘数组之间的比率。
private final int RATE = TABLE_WIDTH / BOARD_SIZE;
// 定义棋盘坐标的像素值和棋盘数组之间的偏移距。
private final int X_OFFSET = 5;
private final int Y_OFFSET = 6;
// 定义一个二维数组来充当棋盘
private String[][] board = new String[BOARD_SIZE][BOARD_SIZE];
// 五子棋游戏的窗口
JFrame f = new JFrame("五子棋游戏");
// 五子棋游戏棋盘对应的Canvas组件
ChessBoard chessBoard = new ChessBoard();
// 当前选中点的坐标
private int selectedX = -1;
private int selectedY = -1;
public void init()throws Exception
{
table = ImageIO.read(new File("image/board.jpg"));
black = ImageIO.read(new File("image/black.gif"));
white = ImageIO.read(new File("image/white.gif"));
selected = ImageIO.read(new File("image/selected.gif"));
// 把每个元素赋为"╋","╋"代表没有棋子
for (int i = 0 ; i < BOARD_SIZE ; i++)
{
for ( int j = 0 ; j < BOARD_SIZE ; j++)
{
board[i][j] = "╋";
}
}
chessBoard.setPreferredSize(new Dimension(
TABLE_WIDTH , TABLE_HETGHT));
chessBoard.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent e)
{
// 将用户鼠标事件的坐标转换成棋子数组的坐标。
int xPos = (int)((e.getX() - X_OFFSET) / RATE);
int yPos = (int)((e.getY() - Y_OFFSET ) / RATE);
board[xPos][yPos] = "●";
/*
电脑随机生成两个整数,作为电脑下棋的坐标,赋给board数组。
还涉及:
1.如果下棋的点已经有棋子,不能重复下棋。
2.每次下棋后,需要扫描谁赢了
*/
chessBoard.repaint();
}
// 当鼠标退出棋盘区后,复位选中点坐标
public void mouseExited(MouseEvent e)
{
selectedX = -1;
selectedY = -1;
chessBoard.repaint();
}
});
chessBoard.addMouseMotionListener(new MouseMotionAdapter()
{
// 当鼠标移动时,改变选中点的坐标
public void mouseMoved(MouseEvent e)
{
selectedX = (e.getX() - X_OFFSET) / RATE;
selectedY = (e.getY() - Y_OFFSET) / RATE;
chessBoard.repaint();
}
});
f.add(chessBoard);
f.pack();
f.setVisible(true);
}
public static void main(String[] args)throws Exception
{
Gobang gb = new Gobang();
gb.init();
}
class ChessBoard extends JPanel
{
// 重写JPanel的paint方法,实现绘画
public void paint(Graphics g)
{
// 将绘制五子棋棋盘
g.drawImage(table , 0 , 0 , null);
// 绘制选中点的红框
if (selectedX >= 0 && selectedY >= 0)
g.drawImage(selected , selectedX * RATE + X_OFFSET ,
selectedY * RATE + Y_OFFSET, null);
// 遍历数组,绘制棋子。
for (int i = 0 ; i < BOARD_SIZE ; i++)
{
for ( int j = 0 ; j < BOARD_SIZE ; j++)
{
// 绘制黑棋
if (board[i][j].equals("●"))
{
g.drawImage(black , i * RATE + X_OFFSET
, j * RATE + Y_OFFSET, null);
}
// 绘制白棋
if (board[i][j].equals("○"))
{
g.drawImage(white, i * RATE + X_OFFSET
, j * RATE + Y_OFFSET, null);
}
}
}
}
}
}
手绘
package ch11;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
public class HandDraw
{
// 画图区的宽度
private final int AREA_WIDTH = 500;
// 画图区的高度
private final int AREA_HEIGHT = 400;
// 下面的preX、preY保存了上一次鼠标拖动事件的鼠标坐标
private int preX = -1;
private int preY = -1;
// 定义一个右键菜单用于设置画笔颜色
PopupMenu pop = new PopupMenu();
MenuItem redItem = new MenuItem("红色");
MenuItem greenItem = new MenuItem("绿色");
MenuItem blueItem = new MenuItem("蓝色");
// 定义一个BufferedImage对象
BufferedImage image = new BufferedImage(AREA_WIDTH
, AREA_HEIGHT , BufferedImage.TYPE_INT_RGB);
// 获取image对象的Graphics
Graphics g = image.getGraphics();
private Frame f = new Frame("简单手绘程序");
private DrawCanvas drawArea = new DrawCanvas();
// 用于保存画笔颜色
private Color foreColor = new Color(255, 0 ,0);
public void init()
{
// 定义右键菜单的事件监听器。
ActionListener menuListener = e ->
{
if (e.getActionCommand().equals("绿色"))
{
foreColor = new Color(0 , 255 , 0);
}
if (e.getActionCommand().equals("红色"))
{
foreColor = new Color(255 , 0 , 0);
}
if (e.getActionCommand().equals("蓝色"))
{
foreColor = new Color(0 , 0 , 255);
}
};
// 为三个菜单添加事件监听器
redItem.addActionListener(menuListener);
greenItem.addActionListener(menuListener);
blueItem.addActionListener(menuListener);
// 将菜单项组合成右键菜单
pop.add(redItem);
pop.add(greenItem);
pop.add(blueItem);
// 将右键菜单添加到drawArea对象中
drawArea.add(pop);
// 将image对象的背景色填充成白色
g.fillRect(0 , 0 ,AREA_WIDTH , AREA_HEIGHT);
drawArea.setPreferredSize(new Dimension(AREA_WIDTH , AREA_HEIGHT));
// 监听鼠标移动动作
drawArea.addMouseMotionListener(new MouseMotionAdapter()
{
// 实现按下鼠标键并拖动的事件处理器
public void mouseDragged(MouseEvent e)
{
// 如果preX和preY大于0
if (preX > 0 && preY > 0)
{
// 设置当前颜色
g.setColor(foreColor);
// 绘制从上一次鼠标拖动事件点到本次鼠标拖动事件点的线段
g.drawLine(preX , preY , e.getX() , e.getY());
}
// 将当前鼠标事件点的X、Y坐标保存起来
preX = e.getX();
preY = e.getY();
// 重绘drawArea对象
drawArea.repaint();
}
});
// 监听鼠标事件
drawArea.addMouseListener(new MouseAdapter()
{
// 实现鼠标松开的事件处理器
public void mouseReleased(MouseEvent e)
{
// 弹出右键菜单
if (e.isPopupTrigger())
{
pop.show(drawArea , e.getX() , e.getY());
}
// 松开鼠标键时,把上一次鼠标拖动事件的X、Y坐标设为-1。
preX = -1;
preY = -1;
}
});
f.add(drawArea);
f.pack();
f.setVisible(true);
}
public static void main(String[] args)
{
new HandDraw().init();
}
class DrawCanvas extends Canvas
{
// 重写Canvas的paint方法,实现绘画
public void paint(Graphics g)
{
// 将image绘制到该组件上
g.drawImage(image , 0 , 0 , null);
}
}
}
弹球游戏
package ch11;
/**
* Created by Jiqing on 2016/12/4.
*/
import java.util.Random;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class PinBall
{
// 桌面的宽度
private final int TABLE_WIDTH = 300;
// 桌面的高度
private final int TABLE_HEIGHT = 400;
// 球拍的垂直位置
private final int RACKET_Y = 340;
// 下面定义球拍的高度和宽度
private final int RACKET_HEIGHT = 20;
private final int RACKET_WIDTH = 60;
// 小球的大小
private final int BALL_SIZE = 16;
private Frame f = new Frame("弹球游戏");
Random rand = new Random();
// 小球纵向的运行速度
private int ySpeed = 10;
// 返回一个-0.5~0.5的比率,用于控制小球的运行方向。
private double xyRate = rand.nextDouble() - 0.5;
// 小球横向的运行速度
private int xSpeed = (int)(ySpeed * xyRate * 2);
// ballX和ballY代表小球的坐标
private int ballX = rand.nextInt(200) + 20;
private int ballY = rand.nextInt(10) + 20;
// racketX代表球拍的水平位置
private int racketX = rand.nextInt(200);
private MyCanvas tableArea = new MyCanvas();
Timer timer;
// 游戏是否结束的旗标
private boolean isLose = false;
public void init()
{
// 设置桌面区域的最佳大小
tableArea.setPreferredSize(
new Dimension(TABLE_WIDTH , TABLE_HEIGHT));
f.add(tableArea);
// 定义键盘监听器
KeyAdapter keyProcessor = new KeyAdapter()
{
public void keyPressed(KeyEvent ke)
{
// 按下向左、向右键时,球拍水平坐标分别减少、增加
if (ke.getKeyCode() == KeyEvent.VK_LEFT)
{
if (racketX > 0)
racketX -= 10;
}
if (ke.getKeyCode() == KeyEvent.VK_RIGHT)
{
if (racketX < TABLE_WIDTH - RACKET_WIDTH)
racketX += 10;
}
}
};
// 为窗口和tableArea对象分别添加键盘监听器
f.addKeyListener(keyProcessor);
tableArea.addKeyListener(keyProcessor);
// 定义每0.1秒执行一次的事件监听器。
ActionListener taskPerformer = evt ->
{
// 如果小球碰到左边边框
if (ballX <= 0 || ballX >= TABLE_WIDTH - BALL_SIZE)
{
xSpeed = -xSpeed;
}
// 如果小球高度超出了球拍位置,且横向不在球拍范围之内,游戏结束。
if (ballY >= RACKET_Y - BALL_SIZE &&
(ballX < racketX || ballX > racketX + RACKET_WIDTH))
{
timer.stop();
// 设置游戏是否结束的旗标为true。
isLose = true;
tableArea.repaint();
}
// 如果小球位于球拍之内,且到达球拍位置,小球反弹
else if (ballY <= 0 ||
(ballY >= RACKET_Y - BALL_SIZE
&& ballX > racketX && ballX <= racketX + RACKET_WIDTH))
{
ySpeed = -ySpeed;
}
// 小球坐标增加
ballY += ySpeed;
ballX += xSpeed;
tableArea.repaint();
};
timer = new Timer(100, taskPerformer);
timer.start();
f.pack();
f.setVisible(true);
}
public static void main(String[] args)
{
new PinBall().init();
}
class MyCanvas extends Canvas
{
// 重写Canvas的paint方法,实现绘画
public void paint(Graphics g)
{
// 如果游戏已经结束
if (isLose)
{
g.setColor(new Color(255, 0, 0));
g.setFont(new Font("Times" , Font.BOLD, 30));
g.drawString("游戏已结束!" , 50 ,200);
}
// 如果游戏还未结束
else
{
// 设置颜色,并绘制小球
g.setColor(new Color(240, 240, 80));
g.fillOval(ballX , ballY , BALL_SIZE, BALL_SIZE);
// 设置颜色,并绘制球拍
g.setColor(new Color(80, 80, 200));
g.fillRect(racketX , RACKET_Y
, RACKET_WIDTH , RACKET_HEIGHT);
}
}
}
}
剪贴板
package ch11;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.datatransfer.*;
/**
* Created by Jiqing on 2016/12/4.
*/
public class SimpleClipboard {
// 剪贴板
private Frame f = new Frame("简单的剪贴板程序");
private Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
// 定义用于复制的文本框
private TextArea jtaCopyTo = new TextArea(5,20);
private TextArea jtaPaste = new TextArea(5,20);
private Button btCopy = new Button("复制");
private Button btPaste = new Button("粘贴");
public void init() {
Panel p = new Panel();
p.add(btCopy);
p.add(btPaste);
btCopy.addActionListener(event ->{
StringSelection contents = new StringSelection(jtaCopyTo.getText());
clipboard.setContents(contents,null);
});
btPaste.addActionListener(event ->{
if (clipboard.isDataFlavorAvailable(DataFlavor.stringFlavor)) {
try {
String content = (String)clipboard.getData(DataFlavor.stringFlavor);
jtaPaste.append(content);
} catch (Exception e) {
e.printStackTrace();
}
}
});
Box box = new Box(BoxLayout.X_AXIS);
box.add(jtaCopyTo);
box.add(jtaPaste);
f.add(p,BorderLayout.SOUTH);
f.add(box,BorderLayout.CENTER);
f.pack();
f.setVisible(true);
}
public static void main(String[] args) {
new SimpleClipboard().init();
}
}
监听
package ch11;
import java.awt.*;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
/**
* Created by Jiqing on 2016/12/3.
*/
public class WindowListenerTest {
private Frame f = new Frame("测试");
private TextArea ta = new TextArea(6,40);
public void init() {
// 为窗口添加窗口事件监听器
f.addWindowListener(new MyListener());
f.add(ta);
f.pack();
f.setVisible(true);
}
// 实现一个窗口监听器类
class MyListener implements WindowListener {
public void windowActivated(WindowEvent e) {
ta.append("窗口被激活!\n");
}
public void windowClosed(WindowEvent e) {
ta.append("窗口被成功关闭!\n");
}
public void windowClosing(WindowEvent e) {
ta.append("用户关闭窗口!\n");
System.exit(0);
}
public void windowDeactivated(WindowEvent e) {
ta.append("窗口失去焦点!\n");
}
public void windowDeiconified(WindowEvent e) {
ta.append("窗口被恢复!\n");
}
public void windowIconified(WindowEvent e) {
ta.append("窗口被最小化!\n");
}
public void windowOpened(WindowEvent e) {
ta.append("窗口初次被打开!\n");
}
}
public static void main(String[] args) {
new WindowListenerTest().init();
}
}
适配器
package ch11;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
/**
* Created by Jiqing on 2016/12/4.
*/
public class WindowAdapterTest {
private Frame f = new Frame("测试");
private TextArea ta = new TextArea(6,40);
public void init() {
f.addWindowListener(new MyListener());
f.add(ta);
f.pack();
f.setVisible(true);
}
class MyListener extends WindowAdapter{
public void windowClosing(WindowEvent e) {
System.out.println("用户关闭窗口!\n");
System.exit(0);
}
}
public static void main(String[] args) {
new WindowAdapterTest().init();
}
}