⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 map.java

📁 一个全自动的反弹球
💻 JAVA
字号:
package com.cz.map;

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

public class Map extends JFrame {
	int WIDTH = 300;
	int HEIGHT = 400;
	Map() {
		this.setSize(WIDTH , HEIGHT);
		
		WoPanel wp = new WoPanel(WIDTH , HEIGHT);
		
		Thread t = new Thread(wp);
		t.start();
		
		this.addMouseListener(wp);
		this.addMouseMotionListener(wp);
		
		this.add(wp);
	}
	public static void main(String[] args) {
		Map w = new Map(); 
		w.setVisible(true);
		
	}
}
class WoPanel extends JPanel implements Runnable , MouseListener , MouseMotionListener {
	private int WIDTH;
	private int HEIGHT;
	int x = 0;
	int y = 0;
	int bx = 200;
	int by = 200;
	int x1 = 0;
	int y1 = 0;
	int x2 = 0;
	int y2 = 0;
	int n = 0;
	boolean xy[][];
	Color c = Color.RED;
	
	WoPanel(int WIDTH , int HEIGHT) {
		this.WIDTH = WIDTH;
		this.HEIGHT = HEIGHT;
		xy = new boolean[WIDTH][HEIGHT];
		
		n = (int)(Math.random()*4);
		
		for(int i = 0; i < WIDTH; i++) {
			for(int j = 0; j < HEIGHT; j++) {
				xy[i][j] = true;
				if(i == 0 || j == 0 || i == WIDTH - 10 || j == HEIGHT - 10) {
					xy[i][j] = false;
				}
			}
		}
		
		x = bx + 5;
		y = by + 5;
	}	
	public void paint(Graphics g) {
		super.paint(g);
		this.setBackground(Color.BLACK);
		g.setColor(c);
		g.fillOval(bx , by , 10 , 10);
		
		g.setColor(Color.GREEN);
		for(int i = 0; i < WIDTH; i++) {
			for(int j = 0; j < HEIGHT; j++) {
				if(!xy[i][j]) {
					g.fillOval(i , j , 1 , 1);
				}
			}
		}		
		if(x1 - x2 > 0) {
			if(y1 - y2 >0) {
				// 左上
				g.fillRect(x2 , y2 , x1 - x2 , y1 - y2);
			} else {
				// 左下
				g.fillRect(x2 , y1 , x1 - x2 , y2 - y1);
			}			
		} else {
			if(y1 - y2 > 0) {
				// 右上
				g.fillRect(x1 , y2 , x2 - x1 , y1 - y2);
			} else {
				// 右下
				g.fillRect(x1 , y1 , x2 - x1 , y2 - y1);
			}
		}
	}
	public void run() {
		while(true) {
			move();
			try {
				Thread.sleep(5);
			}
			catch(Exception e) {
				
			}			
		}
	}	
	public void move() {
		check();
		// 右下
		if(n == 0) {
			x++;
			y++;
			c = Color.YELLOW;
		}
		// 右上
		if(n == 1) {
			x++;
			y--;
			c = Color.GREEN;
		}
		// 左上
		if(n == 2) {
			x--;
			y--;
			c = Color.BLUE;
		}
		// 左下
		if(n == 3) {
			x--;
			y++;
			c = Color.RED;
		}
		
		bx = x - 5;
		by = y - 5;
		
		repaint();
	}
	public void check() {
	    if(n == 0) {
			if(!xy[x + 1][y + 1]) {
				if(!xy[x + 1][y - 1]) {
					if(xy[x - 1][y + 1]) {
						n = 3;
					} else {
						n = 2;
					}
				} else {
					if(xy[x - 1][y + 1]){
						n = 2;
					} else {
						n = 1;
					}
				}					
			}
		}
	    
	    if(n == 1) {
	    	if(!xy[x + 1][y - 1]) {
				if(!xy[x + 1][y + 1]) {
					if(xy[x - 1][y - 1]) {
						n = 2;
					} else {
						n = 3;
					}
				} else {
					if(xy[x - 1][y - 1]){
						n = 3;
					} else {
						n = 0;
					}
				}					
			}
	    }
	    
	    if(n == 2) {
	    	if(!xy[x - 1][y - 1]) {
				if(!xy[x - 1][y + 1]) {
					if(xy[x + 1][y - 1]) {
						n = 1;
					} else {
						n = 0;
					}
				} else {
					if(xy[x + 1][y - 1]){
						n = 0;
					} else {
						n = 3;
					}
				}
			}
	    }
	    
	    if(n == 3) {
	    	if(!xy[x - 1][y + 1]) {
				if(!xy[x - 1][y - 1]) {
					if(xy[x + 1][y + 1]) {
						n = 0;
					} else {
						n = 1;
					}
				} else {
					if(xy[x + 1][y + 1]) {
						n = 1;
					} else {
						n = 2;
					}
				}					
			}
	    }	    
	}	
	public void mouseClicked(MouseEvent e) {
		
	}	
	public void mousePressed(MouseEvent e) {
		x1 = e.getX();
		y1 = e.getY();
	}	
	public void mouseReleased(MouseEvent e) {
		x2 = e.getX();
		y2 = e.getY();
		int tmp = 0;
		if(x1 > x2) {
			tmp = x1;
			x1 = x2;
			x2 = tmp;
		}
		if(y1 > y2) {
			tmp = y1;
			y1 = y2;
			y2 = tmp;
		}
		for(int i = x1; i < x2; i++) {
			for(int j = y1; j < y2; j++) {
				xy[i][j] = false;
			}
		}
	}	
	public void mouseEntered(MouseEvent e) {
		
	}	
	public void mouseExited(MouseEvent e) {
		
	}	
	public void mouseDragged(MouseEvent e) {
		x2 = e.getX();
		y2 = e.getY();
		repaint();
	}	
	public void mouseMoved(MouseEvent e) {
		
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -