📄 animapanel.java
字号:
package labyrinth;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Random;
import javax.swing.JPanel;
class AnimaPanel extends JPanel implements MouseListener, Runnable {
Car car;
Image path,balk,entrance,exit;
Location[][] locations;
int carpx = 0;
int carpy = 1;
static Image photos[];
// Thread anima;
Graphics og;
AnimaPanel(Car car, Location[][] locs) {
photos = new Image[4];
active = true;
this.car = car;
Toolkit tk = Toolkit.getDefaultToolkit();
locations = locs;
path = tk.createImage("./res/path.GIF");
balk = tk.createImage("./res/balk.gif");
exit = tk.createImage("./res/exit.gif");
entrance = tk.createImage("./res/entrance.gif");
LoadImage.waitImage(this, path);
LoadImage.waitImage(this, balk);
LoadImage.waitImage(this, entrance);
LoadImage.waitImage(this, exit);
add(car);
carpx = car.cury * 40;
carpy = car.curx * 40;
addMouseListener(this);
getPhotos();
}
// 只须要重绘要变化的两个单元
int carplx;
int carply = 40;
public final void setOffscreen() {
offscreen = createImage(320, 240);
og = offscreen.getGraphics();
og.drawImage(photos[0], 0, 0, this);
}
public final void ready() {
carpx = car.cury * 40;
carpy = car.curx * 40;
carplx = car.lasty * 40;
carply = car.lastx * 40;
}
final boolean animation() {
if (carplx < carpx) {
car.direction = 1;
carplx += 4;
repaint(carplx, carply, 80, 40);
return false;
}
if (carplx > carpx) {
car.direction = 3;
carplx -= 4;
repaint(carpx, carpy, 80, 40);
return false;
}
if (carply < carpy) {
car.direction = 2;
carply += 4;
repaint(carplx, carply, 40, 80);
return false;
}
if (carpy < carply) {
car.direction = 4;
carply -= 4;
repaint(carpx, carpy, 40, 80);
return false;
}
return true;
}
public void paint(Graphics g) {
// paintComponent(g);
// super.paint(g);
if (!ToolPanel.win) {
for (int i = 0; i < locations.length; i++) {
for (int j = 0; j < locations[0].length; j++) {
g.drawImage(path, j * 40, i * 40, this);
if (locations[i][j].balk) {
g.drawImage(balk, j * 40, i * 40, this);
}
}
}
g.drawImage(entrance, 0, 40, this);
g.drawImage(exit, 9 * 40, 8 * 40, this);
car.setLocation(carplx, carply);
paintChildren(g);
} else {
g.setColor(getBackground());
g.fillRect(0, 0, getWidth(), getHeight());
g.fillRect(100, 100, 100, 100);
g.drawImage(offscreen, 40, 50, this);
g.setFont(font);
g.setColor(Color.DARK_GRAY);
g.drawString("Teammate : ",100,320);
g.setColor(Color.RED);
switch (indexp) {
case 0:
g.drawString("lishic",250,320);
break;
case 1:
g.drawString("yane",250,320);
break;
case 2:
g.drawString("duanlinlin",250,320);
break;
case 3:
g.drawString("unicoco",250,320);
break;
default:
break;
}
}
g.setColor(Color.GRAY);
g.fillRect(402, 0, 286, 472);
g.setColor(Color.BLACK);
for (int x = 402; x < 286 + 402; x += 10) {
g.drawLine(402, 286 + 402 - x, x, 0);
}
// System.out.println(getWidth());
g.setFont(font);
g.setColor(Color.BLUE);
g.drawString("Coder : Unicoco", 480, 130);
g.drawString("Software Artist : Yane", 410, 210);
}
Font font = new Font("tohoma", Font.BOLD, 25);
boolean active;
// public void run() {
// }
public final boolean start(boolean active) {
this.active = active;
while (active && ToolPanel.active) {
active = !animation();
try {
Thread.sleep(50);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
if (!ToolPanel.active) {
return false;
}
}
if (!ToolPanel.active) {
return false;
}
return true;
}
public void mouseClicked(MouseEvent arg0) {
}
public void mouseEntered(MouseEvent arg0) {
}
public void mouseExited(MouseEvent arg0) {
}
public void mousePressed(MouseEvent e) {
if (!ToolPanel.started) {
int pixelX = e.getX() / 40;
int pixelY = e.getY() / 40;
if (pixelX > 0 && pixelY > 0 && pixelX < 9 && pixelY < 9) {
if (locations[pixelY][pixelX].balk) {
locations[pixelY][pixelX].balk = false;
} else {
locations[pixelY][pixelX].balk = true;
}
// System.out.println(pixelX+" "+pixelY);
repaint(pixelX * 40, pixelY * 40, 40, 40);
}
}
}
public void mouseReleased(MouseEvent arg0) {
}
public void reset() {
carplx = 0;
carply = 40;
carpx = 1;
carpy = 0;
}
int indexp = 0;
Image offscreen;
public void run() {
while (ToolPanel.win) {
showPhotos();
}
}
final void mode1() {
//System.out.println("mode1");
//Graphics og = offscreen.getGraphics();
int x = 0, y = 0, w = 320, h = 240;
boolean condi = true;
for (; w >= 0;) {
og.drawImage(photos[(indexp + 1) % 4], 0, 0, this);
og.setClip(x, y, w, h);
og.drawImage(photos[indexp], 0, 0, this);
repaint();
try {
Thread.sleep(20);
} catch (Exception e) {
// TODO: handle exception
}
x += 2;
y += condi ? 1 : 2;
condi = !condi;
w -= 4;
h -= 3;
}
indexp = (indexp + 1) % 4;
repaint();
og.setClip(0,0,320, 240);
}
final void mode2() {
//System.out.println("mode2");
//Graphics og = offscreen.getGraphics();
int section = 0;
// int step=40;
for (; section <= 40;) {
og.drawImage(photos[indexp], 0, 0, this);
for (int x = 0; x <= 320; x += 40) {
og.setClip(x, 0, section, 240);
og.drawImage(photos[(indexp + 1) % 4], 0, 0, this);
}
repaint();
try {
Thread.sleep(50);
} catch (Exception e) {
// TODO: handle exception
}
section++;
}
indexp = (indexp + 1) % 4;
repaint();
og.setClip(0,0,320, 240);
//og.dispose();
}
boolean[][] occupyed = new boolean[10][10];
final void mode3() {
//System.out.println("mode3");
//Graphics og = offscreen.getGraphics();
og.drawImage(photos[indexp], 0, 0, this);
int w = 32, h = 24;
int incompleted = occupyed.length * occupyed[0].length;
while (incompleted > 0) {
int x = (int) (Math.random() * 305) / w;
int y = (int) (Math.random() * 229) / h;
if (valide(x, y, occupyed)) {
og.setClip(x * w, y * h, w, h);
og.drawImage(photos[(indexp + 1) % 4], 0, 0, this);
repaint();
incompleted--;
try {
Thread.sleep(10);
} catch (Exception e) {
// TODO: handle exception
}
} else {
continue;
}
}
indexp = (indexp + 1) % 4;
repaint();
//og.dispose();
og.setClip(0,0,320, 240);
for (int i = 0; i < occupyed.length; i++) {
for (int j = 0; j < occupyed[0].length; j++) {
occupyed[i][j]=false;
}
}
}
boolean valide(int x, int y, boolean[][] occupyed) {
if (occupyed[x][y]) {
return false;
}
occupyed[x][y] = true;
return true;
}
final void mode4() {
//System.out.println("mode4");
//Graphics og = offscreen.getGraphics();
int nx = 320, ny = 240;
for (; nx >= 0; nx -= 4) {
og.setClip(0, 0, nx, ny);
og.drawImage(photos[indexp], 0, 0, this);
og.setClip(nx, 0, 320 - nx, ny);
og.drawImage(photos[(indexp + 1) % 4], 0, 0, this);
repaint();
try {
Thread.sleep(40);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
indexp = (indexp + 1) % 4;
repaint();
og.setClip(0,0,320, 240);
//og.dispose();
}
public final void getPhotos() {
Toolkit tk = Toolkit.getDefaultToolkit();
photos[3] = tk.createImage("./res/unicoco.jpg");
photos[1] = tk.createImage("./res/yane.jpg");
photos[0] = tk.createImage("./res/lsc.jpg");
photos[2] = tk.createImage("./res/dll.jpg");
for (int i = 0; i < photos.length; i++) {
LoadImage.waitImage(this, photos[i]);
}
}
public void showPhotos() {
setOffscreen();
while (ToolPanel.win) {
int mode=(int)(Math.random()*4);
switch (mode) {
case 0:
mode1();
break;
case 1:
mode2();
break;
case 2:
mode3();
break;
case 3:
mode4();
break;
default:
break;
}
try {
Thread.sleep(2000);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
return;
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -