Day11-java绘制图形
java绘制图形
-
绘制一个坦克1.0
package com.lsq.example.绘图;
public class Tanke {
private int x;
private int y;
public Tanke(int x, int y) {
this.x = x;
this.y = y;
}
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;
}
}package com.lsq.example.绘图;
import com.sun.javafx.scene.control.skin.TableHeaderRow;
import javax.swing.*;
import java.awt.*;
//绘图区(面板)
public class Panel extends JPanel {
Wenji wenji = null;
public Panel() {
wenji = new Wenji(100, 100);
}
@Override
public void paint(Graphics g) {
super.paint(g);
g.fillRect(0, 0, 1000, 750);//填充矩形,默认黑色
drawTanke(wenji.getX(),wenji.getY(),g,0,0);
}
//单独写一个画坦克的方法
public void drawTanke(int x,int y,Graphics g,int drect,int type){
switch (type){
case 0://我方坦克
g.setColor(Color.cyan);
break;
case 1://地方坦克
g.setColor(Color.orange);
break;
}
//设置坦克的方向
switch (drect){
case 0:
g.fill3DRect(x,y,10,60,false);
g.fill3DRect(x+30,y,10,60,false);
g.fill3DRect(x+10,y+10,20,40,false);
g.fillOval(x+10,x+20,20,20);
g.drawLine(x+20,y+30,x+20,y);
break;
default:
System.out.println("暂时不做处理");
break;
}
}
}package com.lsq.example.绘图;
//要画的坦克
public class Wenji extends Tanke{
public Wenji(int x, int y) {
super(x, y);
}
}package com.lsq.example.绘图;
import javax.swing.*;
//窗口
public class TankeGame extends JFrame {
Panel panel=null;
public static void main(String[] args) {
TankeGame tankeGame = new TankeGame();
}
public TankeGame(){
panel=new Panel();
this.add(panel);//把游戏的绘图区添加到到窗口
this.setSize(1000,750);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
} -
绘制一个上下左右移动的坦克2.0
package com.lsq.example.坦克;
public class Tanke {
public int getSpeed() {
return speed;
}
public void setSpeed(int speed) {
this.speed = speed;
}
private int x;
private int y;
private int drect;
private int speed=1;
public void moveUp(){
y-=speed;
}
public void moveDown(){
y+=speed;
}
public void moveLeft(){
x-=speed;
}
public void moveRight(){
x+=speed;
}
public Tanke(int x, int y) {
this.x = x;
this.y = y;
}
public int getDrect() {
return drect;
}
public void setDrect(int drect) {
this.drect = drect;
}
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;
}
}package com.lsq.example.坦克;
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
//绘图区(面板)
public class Panel extends JPanel implements KeyListener{
Wenji wenji = null;
public Panel() {
wenji = new Wenji(100, 100);
wenji.setSpeed(5);
}
@Override
public void paint(Graphics g) {
super.paint(g);
g.fillRect(0, 0, 1000, 750);//填充矩形,默认黑色
drawTanke(wenji.getX(),wenji.getY(),g,wenji.getDrect(),1);
}
//单独写一个画坦克的方法
public void drawTanke(int x,int y,Graphics g,int drect,int type){
switch (type){
case 0://我方坦克
g.setColor(Color.cyan);
break;
case 1://地方坦克
g.setColor(Color.orange);
break;
}
//设置坦克的方向(0:向上,1:向右,2:向下,3:向左)
switch (drect){
case 0:
g.fill3DRect(x,y,10,60,false);
g.fill3DRect(x+30,y,10,60,false);
g.fill3DRect(x+10,y+10,20,40,false);
g.fillOval(x+10,y+20,20,20);
g.drawLine(x+20,y+30,x+20,y);
break;
case 1:
g.fill3DRect(x,y,60,10,false);
g.fill3DRect(x,y+30,60,10,false);
g.fill3DRect(x+10,y+10,40,20,false);
g.fillOval( x+20,y+10,20,20);
g.drawLine(x+30,y+20,x+60,y+20);
break;
case 2:
g.fill3DRect(x,y,10,60,false);
g.fill3DRect(x+30,y,10,60,false);
g.fill3DRect(x+10,y+10,20,40,false);
g.fillOval(x+10,y+20,20,20);
g.drawLine(x+20,y+30,x+20,y+60);
break;
case 3:
g.fill3DRect(x,y,60,10,false);
g.fill3DRect(x,y+30,60,10,false);
g.fill3DRect(x+10,y+10,40,20,false);
g.fillOval(x+20,y+10,20,20);
g.drawLine(x+30,y+20,x,y+20);
break;
default:
System.out.println("暂时不做处理");
break;
}
}
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode()==KeyEvent.VK_W){
wenji.setDrect(0);
wenji.moveUp();
}else if (e.getKeyCode()==KeyEvent.VK_D){
wenji.setDrect(1);
wenji.moveRight();
}else if (e.getKeyCode()==KeyEvent.VK_S){
wenji.setDrect(2);
wenji.moveDown();
}else if (e.getKeyCode()==KeyEvent.VK_A){
wenji.setDrect(3);
wenji.moveLeft();
}
this.repaint();
}
@Override
public void keyReleased(KeyEvent e) {
}
}package com.lsq.example.坦克;
//要画的坦克
public class Wenji extends Tanke {
public Wenji(int x, int y) {
super(x, y);
}
}package com.lsq.example.坦克;
import javax.swing.*;
//窗口
public class TankeGame02 extends JFrame {
Panel panel=null;
public static void main(String[] args) {
TankeGame02 tankeGame = new TankeGame02();
}
public TankeGame02(){
panel=new Panel();
this.add(panel);//把游戏的绘图区添加到到窗口
this.addKeyListener(panel);
this.setSize(1000,750);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
} -
绘制我方和敌方坦克
package com.lsq.example.坦克;
public class Tanke {
public int getSpeed() {
return speed;
}
public void setSpeed(int speed) {
this.speed = speed;
}
private int x;
private int y;
private int drect;
private int speed=1;
public void moveUp(){
y-=speed;
}
public void moveDown(){
y+=speed;
}
public void moveLeft(){
x-=speed;
}
public void moveRight(){
x+=speed;
}
public Tanke(int x, int y) {
this.x = x;
this.y = y;
}
public int getDrect() {
return drect;
}
public void setDrect(int drect) {
this.drect = drect;
}
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;
}
}package com.lsq.example.坦克;
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Vector;
//绘图区(面板)
public class Panel extends JPanel implements KeyListener{
Wenji wenji = null;
Vector<EnemyTanke> enemyTankes=new Vector<>();
int enemyTankeSize=3;
public Panel() {
wenji = new Wenji(100, 100);
wenji.setSpeed(5);
for (int i = 0; i < enemyTankeSize; i++) {
EnemyTanke enemyTanke = new EnemyTanke(100 * (i + 1), 0);
enemyTanke.setDrect(2);
enemyTankes.add(enemyTanke);
}
}
@Override
public void paint(Graphics g) {
super.paint(g);
g.fillRect(0, 0, 1000, 750);//填充矩形,默认黑色
drawTanke(wenji.getX(),wenji.getY(),g,wenji.getDrect(),1);
for (int i = 0; i < enemyTankeSize; i++) {
EnemyTanke enemyTanke = enemyTankes.get(i);
drawTanke(enemyTanke.getX(),enemyTanke.getY(),g,enemyTanke.getDrect(),0);
}
}
//单独写一个画坦克的方法
public void drawTanke(int x,int y,Graphics g,int drect,int type){
switch (type){
case 0://我方坦克
g.setColor(Color.cyan);
break;
case 1://地方坦克
g.setColor(Color.orange);
break;
}
//设置坦克的方向(0:向上,1:向右,2:向下,3:向左)
switch (drect){
case 0:
g.fill3DRect(x,y,10,60,false);
g.fill3DRect(x+30,y,10,60,false);
g.fill3DRect(x+10,y+10,20,40,false);
g.fillOval(x+10,y+20,20,20);
g.drawLine(x+20,y+30,x+20,y);
break;
case 1:
g.fill3DRect(x,y,60,10,false);
g.fill3DRect(x,y+30,60,10,false);
g.fill3DRect(x+10,y+10,40,20,false);
g.fillOval( x+20,y+10,20,20);
g.drawLine(x+30,y+20,x+60,y+20);
break;
case 2:
g.fill3DRect(x,y,10,60,false);
g.fill3DRect(x+30,y,10,60,false);
g.fill3DRect(x+10,y+10,20,40,false);
g.fillOval(x+10,y+20,20,20);
g.drawLine(x+20,y+30,x+20,y+60);
break;
case 3:
g.fill3DRect(x,y,60,10,false);
g.fill3DRect(x,y+30,60,10,false);
g.fill3DRect(x+10,y+10,40,20,false);
g.fillOval(x+20,y+10,20,20);
g.drawLine(x+30,y+20,x,y+20);
break;
default:
System.out.println("暂时不做处理");
break;
}
}
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode()==KeyEvent.VK_W){
wenji.setDrect(0);
wenji.moveUp();
}else if (e.getKeyCode()==KeyEvent.VK_D){
wenji.setDrect(1);
wenji.moveRight();
}else if (e.getKeyCode()==KeyEvent.VK_S){
wenji.setDrect(2);
wenji.moveDown();
}else if (e.getKeyCode()==KeyEvent.VK_A){
wenji.setDrect(3);
wenji.moveLeft();
}
this.repaint();
}
@Override
public void keyReleased(KeyEvent e) {
}
}package com.lsq.example.坦克;
//要画的坦克
public class Wenji extends Tanke {
public Wenji(int x, int y) {
super(x, y);
}
}package com.lsq.example.坦克;
//敌人的坦克
public class EnemyTanke extends Tanke{
public EnemyTanke(int x, int y) {
super(x, y);
}
}package com.lsq.example.坦克;
import javax.swing.*;
//窗口
public class TankeGame02 extends JFrame {
Panel panel=null;
public static void main(String[] args) {
TankeGame02 tankeGame = new TankeGame02();
}
public TankeGame02(){
panel=new Panel();
this.add(panel);//把游戏的绘图区添加到到窗口
this.addKeyListener(panel);
this.setSize(1000,750);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
} -
绘制一个会动的小球
package com.lsq.example.绘图;
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.logging.XMLFormatter;
public class BollMove extends JFrame{
Panel_ myPanel=null;
public static void main(String[] args) {
BollMove bollMove = new BollMove();
}
public BollMove(){
myPanel=new Panel_();
this.add(myPanel);
this.setSize(400,500);
this.addKeyListener(myPanel);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
class Panel_ extends JPanel implements KeyListener{
private int x=10;
private int y=10;
@Override
public void paint(Graphics g) {
super.paint(g);
g.setColor(Color.cyan);
g.fillOval(x,y,20,20);
}
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
System.out.println((char) e.getKeyCode()+"键被按下...");
if (e.getKeyCode()==KeyEvent.VK_DOWN){
y+=5;
}else if(e.getKeyCode()==KeyEvent.VK_UP){
y-=5;
}else if (e.getKeyCode()==KeyEvent.VK_LEFT){
x-=5;
}else if (e.getKeyCode()==KeyEvent.VK_RIGHT){
x+=5;
}
this.repaint();
}
@Override
public void keyReleased(KeyEvent e) {
}
}