Java贪吃蛇和图形编辑(极简画板)制作

1.贪吃蛇是在b站上学习的时候码的代码
B站

2.图形编辑是极简化的代码
程序中的代码中有详细的注释

贪吃蛇

package snake;

import javax.swing.*;

//游戏的主启动类
public class StartGame {
	public static void main(String arggs[]) {
		JFrame frame = new JFrame();
		
		frame.setBounds(10,10,900,720);
		frame.setResizable(false);//设置窗口大小不可变
		frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
	
		//正常游戏界面都应该在画上
		frame.add(new GamePanel());
		frame.setVisible(true);
	}
}
package snake;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Random;

//游戏的面板
public class GamePanel extends JPanel implements KeyListener,ActionListener{
	//定义蛇的数据结构
	int length;//蛇的长度
	int snakeX[] = new int[600];	//蛇的X坐标25*25
	int snakeY[] = new int[500];	//蛇的X坐标25*25
	String direction;
	
	//食物的坐标
	int foodX;
	int foodY;
	Random random = new Random();
	
	//成绩
	int score ;
	//游戏当前的状态:开始,停止
	boolean isStart = false;	//默认是不开始的
	//游戏失败状态
	boolean isFail = false; 
	//定时器
	Timer timer = new Timer(100, this);	//100ms执行一次
	
	//构造器
	public GamePanel() {
		init();
		//获得焦点和键盘事件
		this.setFocusable(true);//获得焦点事件
		this.addKeyListener(this);//获得键盘监听事件
		timer.start();//游戏开始,定时器启动
	}
	
	//初始化方法
	 public void init() {
		length = 3;
		snakeX[0] = 100; snakeY[0] = 100;	//脑袋坐标
		snakeX[1] = 75; snakeY[1] = 100;	//第一个身体坐标
		snakeX[2] = 50; snakeY[2] = 100;	//第二个身体坐标
		direction = "right"; 
		
		//把食物随机分布在界面上
		foodX = 25 + 25*random.nextInt(34);
		foodY = 75 + 25*random.nextInt(24);
		
		score = 0;
	 }
	
	//绘制面板,我们游戏中的所有东西,都是由这个画笔来画
	@Override
	protected void paintComponent(Graphics g) {
		super.paintComponent(g);//清屏
		//绘制静态面板
		this.setBackground(Color.white);
		Data.header.paintIcon(this, g, 25, 11);	//头部广告栏画上去
		g.fillRect(25, 75, 850, 600);			//默认的游戏界面
		
		//画积分
		g.setColor(Color.black);
		g.setFont(new Font("微软雅黑",Font.BOLD,18));
		g.drawString("长度:"+length, 750, 35);
		g.drawString("分数:"+score, 750, 50);
		
		//画食物
		Data.food.paintIcon(this, g, foodX, foodY);
		
		//把小蛇画上去
		if(direction.equals("right")) {
			Data.right.paintIcon(this, g, snakeX[0], snakeY[0]);	//蛇头的初始化向右
		}else if(direction.equals("left")) {
			Data.left.paintIcon(this, g, snakeX[0], snakeY[0]);	
		}else if(direction.equals("up")) {
			Data.up.paintIcon(this, g, snakeX[0], snakeY[0]);	
		}else if(direction.equals("down")) {
			Data.down.paintIcon(this, g, snakeX[0], snakeY[0]);	
		}
		
		for (int i = 1; i < length; i++) {
			Data.body.paintIcon(this, g, snakeX[i], snakeY[i]);		//第一个身体坐标
		}
		
		//游戏状态
		if(isStart == false) {
			g.setColor(Color.white);
			g.setFont(new Font("微软雅黑",Font.BOLD,40));
			g.drawString("按下空格开始游戏", 300, 300);
		}
		
		if(isFail) {
			g.setColor(Color.red);
			g.setFont(new Font("微软雅黑",Font.BOLD,40));
			g.drawString("按下空格重新开始", 300, 300);
		}
	}

	//键盘监听事件
	@Override
	public void keyPressed(KeyEvent e) {
		int keyCode = e.getKeyCode();
		
		if(keyCode == KeyEvent.VK_SPACE) {
			if(isFail) {
				//重新开始
				isFail = false;
				init();
			}else {
				isStart = !isStart;//取反
			}
			repaint();
		}
		
		//小蛇移动
		if(keyCode == KeyEvent.VK_UP) {
			direction = "up";
		}else if(keyCode == KeyEvent.VK_DOWN) {
			direction = "down";
		}else if(keyCode == KeyEvent.VK_LEFT) {
			direction = "left";
		}else if(keyCode == KeyEvent.VK_RIGHT) {
			direction = "right";
		}
	}
	
	//事件监听----需要通过固定时间来刷新(1s = 10次)
	@Override
	public void actionPerformed(ActionEvent e) {
		if(isStart && isFail == false) {//如果游戏时开始状态,则让小蛇动起来
			
			//吃食物
			if(snakeX[0] == foodX && snakeY[0] == foodY) {
				length++;	
				score = score + 10;
				//再次随机生成食物
				foodX = 25 + 25*random.nextInt(34);
				foodY = 75 + 25*random.nextInt(24);
			}
			
			//移动
			for(int i = length-1; i>0;i--) {//后一节移到前一节的位置
				snakeX[i] = snakeX[i-1];
				snakeY[i] = snakeY[i-1];			
			}
			
			//走向
			if(direction.equals("right")) {
				snakeX[0] = snakeX[0] + 25;
				if(snakeX[0]>850){snakeX[0] = 25;}//边界判断
			}else if(direction.equals("left")) {
				snakeX[0] = snakeX[0] - 25;
				if(snakeX[0]<25){snakeX[0] = 850;}//边界判断
			}else if(direction.equals("up")) {
				snakeY[0] = snakeY[0] - 25;
				if(snakeY[0]<75){snakeY[0] = 650;}//边界判断
			}else if(direction.equals("down")) {
				snakeY[0] = snakeY[0] + 25;
				if(snakeY[0]>650){snakeY[0] = 75;}//边界判断
			}
			
			//失败判断,撞到自己就算失败
			for(int i = 1; i<length;i++) {
				if(snakeX[0] == snakeX[i] && snakeY[0] == snakeY[i]) {
					isFail = true;
				}
			}
			
			repaint();//重画页面
		}
		timer.start();//定时器开始
	}
	
	public void keyTyped(KeyEvent e) {}
	public void keyReleased(KeyEvent e) {}

}
package snake;
import java.net.URL;
import javax.swing.*;

public class Data {

	//相对路径 tp.jpg
	//绝对路径
	public static URL headerUrl = Data.class.getResource("statics/header.png");
	public static ImageIcon header = new ImageIcon(headerUrl);
	
	public static URL upUrl = Data.class.getResource("statics/up.png");
	public static URL downUrl = Data.class.getResource("statics/down.png");
	public static URL leftUrl = Data.class.getResource("statics/left.png");
	public static URL rightUrl = Data.class.getResource("statics/right.png");
	public static ImageIcon up = new ImageIcon(upUrl);
	public static ImageIcon down = new ImageIcon(downUrl);
	public static ImageIcon left = new ImageIcon(leftUrl);
	public static ImageIcon right = new ImageIcon(rightUrl);
	
	public static URL bodyUrl = Data.class.getResource("statics/body.png");
	public static ImageIcon body = new ImageIcon(bodyUrl);

	public static URL foodUrl = Data.class.getResource("statics/food.png");
	public static ImageIcon food = new ImageIcon(foodUrl);
}

图形编辑(画板)

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class NewPaint extends JPanel{
	
	private NewShape[] shapeArray = new NewShape[10000];
	public static void main(String[] args) {
		NewPaint p = new NewPaint();
	}

	JFrame frame = new JFrame();
	int len = 0;
	//实例化一个ButtonListener对象,实现了多种接口
	PaintListener paintListener = new PaintListener();
	
	public NewPaint() {
		//实现一个窗体
		frame.setTitle("图形编辑软件");
		frame.setLocation(450, 150);
		frame.setSize(700, 600);
		this.setPreferredSize(new Dimension(600, 500));
		//设置布局为流式布局
		frame.setLayout(new FlowLayout());
		
		String[] command = { "直线", "画笔", "矩形","圆", "多边形", "文本","图片","清除"};
		for (int i = 0; i < command.length; i++) {
			JButton button = new JButton(command[i]);
			button.addActionListener(paintListener);
			frame.add(button);
		}
		
		Color[] color = { Color.BLACK, Color.BLUE, Color.YELLOW, Color.RED, Color.GREEN };
		for (int i = color.length - 1; i >= 0; i--) {
			JButton button = new JButton();
			button.setBackground(color[i]);
			button.setPreferredSize(new Dimension(20, 20));
			button.addActionListener(paintListener);
			frame.add(button);
		}
		
		//将JPanel对象添加进入frame窗体对象中,让他生效
		frame.add(this);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);	
		
		Graphics graphics1 = this.getGraphics();		
		this.addMouseListener(paintListener);			//添加鼠标监听器,用于画图
		this.addMouseMotionListener(paintListener);	//添加鼠标模式监听器,用于绘画曲线
		paintListener.setGraphics(graphics1);			//设置另外一个类的画笔
		paintListener.setJPanel(this);					//设置另外一个类的JPanel容器
		paintListener.setShapeArray(shapeArray);		//设置另外一个类的数组
	}
	
	public void paint(Graphics graphics) {
		super.paint(graphics);	
		len = paintListener.get_len();	//获取数组的长度
		
		for(int i=0;i<len;i++) {
			if(shapeArray[i]!=null) {
				shapeArray[i].repaint(graphics);
			}
			else {
				break;
			}
		}
	}
}
import java.awt.event.*;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import java.awt.*;
public class PaintListener extends JFrame implements ActionListener,MouseListener,MouseMotionListener {
		private int index=0;
		private NewShape shapeArray[];
		private JPanel panel;
		private Graphics graphics;
		private String order="";
		int x0=0,y0=0,x1=0,y1=0,x2=0,y2=0,x3=0,x4=0,y3=0,y4=0;
		int start_x=0,start_y=0;
		public void setJPanel(JPanel panel) {
			this.panel=panel;
		}
    
		public void setGraphics(Graphics graphics) {
			this.graphics = graphics;
		}
    
		public void setShapeArray(NewShape shapeArray[]) {
			this.shapeArray = shapeArray;
		}
    
		public int get_len() {
			return index;
		}
		
    
		public void actionPerformed(ActionEvent e) 
		{
			x1 = 200; y1 = 200;
			if (e.getActionCommand()=="") {
				JButton button = (JButton)e.getSource();
				graphics.setColor(button.getBackground());
			}
			else {
				order = e.getActionCommand();
				if("清除".equals(order)) {
					index = 0;
					panel.repaint();
					x4 = 0;
					y4 = 0;
				}
			}
		}
		
		//曲线
		public void mouseDragged(MouseEvent e) {
			if(order.equals("画笔")) {
				x1 = x0; y1 = y0; 
				x0 = e.getX();
				y0 = e.getY();
				graphics.drawLine(x1,y1,x0,y0);
				shapeArray[index++] = new NewShape(x1,y1,x0,y0,order);
			}
		}
		
		//点击鼠标
		public void mouseClicked(MouseEvent e) {
			if(order.equals("多边形")) {
				if(x4==0&&y4==0){
					x4 = e.getX(); 
					y4 = e.getY();
					start_x = x4; 
					start_y = y4;
				}
				else {
					x3 = x4; y3 = y4;
					x4 = e.getX();
					y4 = e.getY();
					graphics.drawLine(x3, y3, x4, y4);
					shapeArray[index++] = new NewShape(x3,y3,x4,y4,order);
				}
				if(e.getClickCount()==2){
				x4 =0; y4=0;
				graphics.drawLine(start_x, start_y, e.getX(), e.getY());
				shapeArray[index++] = new NewShape(start_x,start_y,e.getX(),e.getY(),order);
				}	
			}
			if(order.equals("文本")) {
				x1 = e.getX();
				y1 = e.getY();
				String text = "文本框";
				Font font = new Font("行楷", Font.BOLD+Font.ITALIC, 26);
				graphics.setFont(font);
				graphics.drawString(text, x1, y1);
				shapeArray[index++] = new NewShape(x1, y1, text);
			}	
			if(order.equals("图片")) {
				x1 = e.getX();
				y1 = e.getY();
				ImageIcon icon = new ImageIcon(
                    "G:\\eclipse\\workspace\\internetWork\\Work\\grapEdit\\picture1.jpg");
	    		Image img = icon.getImage();
	    		graphics.drawImage(img, x1, y1, null);
	    		shapeArray[index++] = new NewShape(x1, y1, img);
			}
		}

		 // 按下
		public void mousePressed(MouseEvent e) {
			if(order.equals("画笔")){
				x0 = e.getX();
				y0 = e.getY();
			}
			x1 = e.getX();
			y1 = e.getY();
		}
		
		// 松开
		public void mouseReleased(MouseEvent e) {
			x2 = e.getX();
			y2 = e.getY();
			if("直线".equals(order)){
				graphics.drawLine(x1, y1, x2, y2);
				shapeArray[index++] = new NewShape(x1,y1,x2,y2,order);
			}
			else if("矩形".equals(order)) {
				graphics.drawRect(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1-x2), Math.abs(y1-y2));
				shapeArray[index++] = new NewShape(x1,y1,x2,y2,order);
			}
			else if("圆".equals(order)) {
				graphics.drawOval(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1-x2), Math.abs(y1-y2));
				shapeArray[index++] = new NewShape(x1,y1,x2,y2,order);
			}
		}
		public void mouseEntered(MouseEvent e) {}
		public void mouseExited(MouseEvent e) {}
		public void mouseMoved(MouseEvent e) {}
	}

import java.awt.Graphics;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JPanel;

public class NewShape{
	//Shape类有name和两点坐标的属性
	private int x1,x2,y1,y2;
	private String name;
	private Image image;
	public NewShape(int x1,int y1,int x2,int y2,String name){
		this.x1 = x1;
		this.x2 = x2;
		this.y1 = y1;
		this.y2 = y2;
		this.name = name;
	}
	public NewShape(int x1,int y1, String name){
		this.x1 = x1;
		this.y1 = y1;
		this.name = name;
	}
	
	public NewShape(int x1,int y1, Image image){
		this.x1 = x1;
		this.y1 = y1;
		this.image = image;
	}
	
	
	public void repaint(Graphics g) {
		switch(name) {
		case "直线":
			g.drawLine(x1, y1, x2, y2);
			break;
		case "矩形":
			g.drawRect(Math.min(x1, x2), Math.min(y1 ,y2), Math.abs(x1-x2), Math.abs(y1-y2));
			break;
		case "圆形":
			g.drawOval(Math.min(x1, x2), Math.min(y1 ,y2), Math.abs(x1-x2), Math.abs(y1-y2));
			break;
		case "曲线":
			g.drawLine(x1, y1, x2, y2);
			break;
		case "多边形":
			g.drawLine(x1, y1, x2, y2);
			break;
		case "文本":
			g.drawString(name, x1, y1);
			break;
		case "图片":
			g.drawImage(image, x1, y1, null);
			break;
		}
	}
}

©️2020 CSDN 皮肤主题: 1024 设计师: 上身试试 返回首页
实付0元
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值