文章目录
- 一、坦克大战游戏演示(略)
- 二、java绘图坐标体系
- 三、java绘图技术
- 3.1 快速入门
- 3.2 绘图原理
- Graphics类
- 四、java事件处理机制
- 五、绘制坦克
- 5.1 坦克抽象类、己方坦克、敌方坦克
- 5.2 自定义面板
- 5.3 绘图界面
一、坦克大战游戏演示(略)
二、java绘图坐标体系
三、java绘图技术
3.1 快速入门
package com.gyh.draw;
import javax.swing.*;
import java.awt.*;
public class DrawCircle extends JFrame {
private MyPanel mp;
public static void main(String[] args) {
new DrawCircle();
}
public DrawCircle() {
this.mp = new MyPanel();
this.add(this.mp);
this.setSize(400, 300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
}
class MyPanel extends JPanel {
@Override
public void paint(Graphics g) {
super.paint(g);
g.drawOval(10, 10, 100, 100);
}
}
3.2 绘图原理
Graphics类
package com.gyh.draw;
import javax.swing.*;
import java.awt.*;
public class DrawCircle extends JFrame {
private MyPanel mp;
public static void main(String[] args) {
new DrawCircle();
}
public DrawCircle() {
this.mp = new MyPanel();
this.add(this.mp);
this.setSize(400, 300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
}
class MyPanel extends JPanel {
@Override
public void paint(Graphics g) {
super.paint(g);
g.drawLine(10, 10, 100, 100);
g.drawRect(10, 10, 100, 100);
g.drawOval(10, 10, 10, 10);
g.setColor(Color.BLUE);
g.fillRect(10, 10, 100, 100);
g.fillOval(10, 10, 100, 100);
Image image = Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/1.PNG"));
g.drawImage(image, 10, 10, 97, 127, this);
g.setColor(Color.GREEN);
g.setFont(new Font("隶书", Font.BOLD, 50));
g.drawString("北京你好", 100, 100);
}
}
四、java事件处理机制
package com.gyh.draw;
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class TestEvent extends JFrame {
private MyPanel2 mp;
public static void main(String[] args) {
TestEvent TestEvent = new TestEvent();
}
public TestEvent() {
mp = new MyPanel2();
this.add(mp);
this.setSize(400, 300);
this.addKeyListener(mp);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
}
class MyPanel2 extends JPanel implements KeyListener {
private int x = 10;
private int y = 10;
@Override
public void paint(Graphics g) {
super.paint(g);
g.fillOval(x, y, 20, 20);
}
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_DOWN) {
y++;
} else if (e.getKeyCode() == KeyEvent.VK_UP) {
y--;
} else if (e.getKeyCode() == KeyEvent.VK_LEFT) {
x--;
} else if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
x++;
}
this.repaint();
}
@Override
public void keyReleased(KeyEvent e) {
}
}
五、绘制坦克
5.1 坦克抽象类、己方坦克、敌方坦克
package com.gyh.draw;
import java.awt.*;
public abstract class Tank {
private int x;
private int y;
private int width = 40;
private int height = 60;
private Color c;
private Direction d = Direction.UP;
enum Direction {
UP, DOWN, LEFT, RIGHT;
}
public Tank(int x, int y, int width, int height, Color c) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.c = c;
}
public Tank(int x, int y, Color c) {
this.x = x;
this.y = y;
this.c = c;
}
private void drawTankLeftOrRight(Graphics g) {
int topWidth = height, topHeight = width / 4;
int topX = x - topWidth / 2, topY = y - 2 * topHeight;
g.fill3DRect(topX, topY, topWidth, topHeight, false);
g.fill3DRect(topX + topWidth / 6, topY + topHeight, topWidth * 2 / 3, 2 * topHeight, false);
g.fillOval(topX + topWidth / 3, topY + topHeight, 2 * topHeight, 2 * topHeight);
if (d == Direction.LEFT) {
g.drawLine(topX, topY + topHeight * 2, topX + topWidth / 3, topY + topHeight * 2);
} else {
g.drawLine(topX + topWidth * 2 / 3, topY + topHeight * 2, topX + topWidth, topY + topHeight * 2);
}
g.fill3DRect(topX, topY + 3 * topHeight, topWidth, topHeight, false);
}
private void drawTankUpOrDown(Graphics g) {
int leftWidth = width / 4, leftHeight = height;
int leftX = x - 2 * leftWidth, leftY = y - height / 2;
g.fill3DRect(leftX, leftY, leftWidth, leftHeight, false);
g.fill3DRect(leftX + leftWidth, leftY + leftHeight / 6, leftWidth * 2, leftHeight * 2 / 3, false);
g.fillOval(leftX + leftWidth, leftY + leftHeight / 3, leftWidth * 2, leftWidth * 2);
if (d == Direction.UP) {
g.drawLine(leftX + leftWidth * 2, leftY + leftHeight / 3, leftX + leftWidth * 2, leftY);
} else {
g.drawLine(leftX + leftWidth * 2, leftY + leftHeight * 2 / 3, leftX + leftWidth * 2, leftY + leftHeight);
}
g.fill3DRect(leftX + leftWidth * 3, leftY, leftWidth, leftHeight, false);
}
public void drawTank(Graphics g) {
g.setColor(c);
if (d == Direction.UP || d == Direction.DOWN) {
drawTankUpOrDown(g);
} else {
drawTankLeftOrRight(g);
}
}
public Direction getD() {
return d;
}
public void setD(Direction d) {
this.d = d;
}
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 int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
}
package com.gyh.draw;
import java.awt.*;
public class Hero extends Tank {
public Hero(int x, int y, int width, int height) {
super(x, y, width, height, Color.yellow);
}
public Hero(int x, int y) {
super(x, y, Color.yellow);
}
}
package com.gyh.draw;
import java.awt.*;
public class Enemy extends Tank {
public Enemy(int x, int y, int width, int height) {
super(x, y, width, height, Color.cyan);
}
public Enemy(int x, int y) {
super(x, y, Color.cyan);
}
}
5.2 自定义面板
package com.gyh.draw;
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.Vector;
public class MyPanel1 extends JPanel implements KeyListener {
private static final int BACKGROUND_WIDTH = 1000;
private static final int BACKGROUND_HEIGHT = 750;
private static final int WIDTH = 40;
private static final int HEIGHT = 60;
Hero hero;
Vector<Enemy> enemys;
int enemyTankNum = 3;
public MyPanel1() {
hero = new Hero(100, 100, WIDTH, HEIGHT);
enemys = new Vector<>();
for (int i = 0; i < enemyTankNum; i++) {
Enemy e = new Enemy(200+i*100, 200);
e.setD(Tank.Direction.DOWN);
enemys.add(e);
}
}
@Override
public void paint(Graphics g) {
super.paint(g);
g.fillRect(0, 0, BACKGROUND_WIDTH, BACKGROUND_HEIGHT);
hero.drawTank(g);
for (Enemy e : enemys) {
e.drawTank(g);
}
}
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
int speed = 4;
int dis = 0;
if (e.getKeyCode() == KeyEvent.VK_DOWN) {
hero.setD(Tank.Direction.DOWN);
dis = (dis = hero.getY() + speed) + HEIGHT / 2 > BACKGROUND_HEIGHT ? BACKGROUND_HEIGHT - HEIGHT / 2 : dis;
hero.setY(dis);
} else if (e.getKeyCode() == KeyEvent.VK_UP) {
hero.setD(Tank.Direction.UP);
dis = (dis = hero.getY() - speed) - HEIGHT / 2 < 0 ? HEIGHT / 2 : dis;
hero.setY(dis);
} else if (e.getKeyCode() == KeyEvent.VK_LEFT) {
hero.setD(Tank.Direction.LEFT);
dis = (dis = hero.getX() - speed) - HEIGHT / 2 < 0 ? HEIGHT / 2 : dis;
hero.setX(dis);
} else if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
hero.setD(Tank.Direction.RIGHT);
dis = (dis = hero.getX() + speed) + HEIGHT / 2 > BACKGROUND_WIDTH ? BACKGROUND_WIDTH - HEIGHT / 2 : dis;
hero.setX(dis);
}
repaint();
}
@Override
public void keyReleased(KeyEvent e) {
}
}
5.3 绘图界面
package com.gyh.draw;
import javax.swing.*;
public class GyhTankGame01 extends JFrame {
MyPanel1 mp;
public static void main(String[] args) {
new GyhTankGame01();
}
public GyhTankGame01() {
mp = new MyPanel1();
this.add(mp);
this.setSize(1200, 800);
this.addKeyListener(mp);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
}