📄 xgame.java
字号:
import java.awt.*;
import java.awt.geom.*;
import java.applet.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.image.*;
public class Xgame extends Applet implements KeyListener,Runnable
{
Area ballArea;
Graphics2D g2d;//Set graphics context for Java 2D
// Animation condition. True = animate, False = static.
int bgx=0, bgy = 0;
boolean reverse_al3x = false, reverse_al3y = false;
int al3xmax = 640, al3ymax = 240;
boolean reverse_al1x = false, reverse_al1y = false;
int al1xmax = 640, al1ymax = 240;
boolean reverse_al2x = false, reverse_al2y = false;
int al2xmax = 640, al2ymax = 240;
boolean reverse_al4x = false, reverse_al4y = false;
int al4xmax = 640, al4ymax = 240;
int xmax = 640, ymax = 480;
int x, y;
int al1x=200, al1y=100, al2x= 400, al2y=100, al3x=600, al3y=100,al4x=200,al4y=240;
int peoplex;
int peopley;
int PEOPLE_SIZE;
int peopleHeight, peopleWidth;
boolean reverse_x = false, reverse_y = false;
boolean planebool=false, laidOut=false;
boolean left;
boolean right;
boolean up;
boolean down;
Thread animThread;
Dimension offDimension; // Defines an offscreen Dimension
Image offImage; // Defines an offscreen Image
Graphics offGraphics; // Defines an offscreen Graphics
Image bg;
Image alien1;
Image alien2;
Image alien3;
Image alien4;
Image people;
public void init()
{
bg = getImage(getCodeBase(),"99.gif");
alien1 = getImage( getCodeBase(),"i25.gif");
alien2 = getImage( getCodeBase(),"u37.gif");
alien3 = getImage( getCodeBase(),"b47.gif");
alien4 = getImage( getCodeBase(),"m241.gif");
people = getImage(getCodeBase(), "1850796573.gif");
peoplex=150;
peopley=400;
peopleWidth = people.getWidth(this);
peopleHeight = people.getHeight(this);
addKeyListener(this);
}
public void keyPressed(KeyEvent evt) {
int key = evt.getKeyCode();
if (key == KeyEvent.VK_LEFT) {
if (peoplex <= 640)
peoplex -= 8;
if(peoplex <3)
peoplex = 3;
repaint();
}
else if (key == KeyEvent.VK_RIGHT) {
if (peoplex <= 640)
peoplex += 8;
if (peoplex > getSize().width - 3 - PEOPLE_SIZE)
peoplex = getSize().width - 3 - PEOPLE_SIZE;
repaint();
}
}
public void keyReleased(KeyEvent evt){}
public void keyTyped(KeyEvent evt){}
public void start() {
// Make sure the thread hasn’t already been created
if (animThread == null) {
animThread = new Thread(this, "anim");
animThread.start();
}
}
public void run()
{
// As long as the thread is created, keep redrawing the canvas and
// then pausing for 100 miliseconds
Thread myThread = Thread.currentThread();
while (animThread == myThread)
{
repaint();
try {
Thread.sleep(10);
} catch (InterruptedException e) {}
if (x > xmax) reverse_x = true;
if (y > ymax) reverse_y = true;
if (x == 0) reverse_x = false;
if (y == 0) reverse_y = false;
if (reverse_x) x-=1;
else x+=1;
if (reverse_y) y-=1;
else y+=1;
}
}
public void update(Graphics g) {
Dimension d = getSize();
// Create the offscreen graphics context
if ((offGraphics == null)
|| (d.width != offDimension.width)
|| (d.height != offDimension.height)) {
offDimension = d;
offImage = createImage(d.width, d.height);
offGraphics = offImage.getGraphics();
}
// Erase the previous image
offGraphics.setColor(getBackground());
offGraphics.fillRect(0, 0, d.width, d.height);
offGraphics.setColor(Color.BLUE);
paintFrame(offGraphics); // Paint the frame into the image
g.drawImage(offImage, 0, 0, null); // Paint the image onto the screen
}
public void paint(Graphics g)
{
if (offImage != null)
{
g.drawImage(offImage, 0, 0, null);
}
}
public void paintFrame(Graphics g)
{
Graphics2D g2d = (Graphics2D)g;
g2d.drawImage(bg, bgx, bgy,this);
g2d.drawImage(alien1, al1x, al1y,this);
g2d.drawImage(alien2, al2x, al2y,this);
g2d.drawImage(alien3, al3x, al3y,this);
g2d.drawImage(alien4, al4x, al4y,this);
g2d.drawImage(people, peoplex, peopley, this);
g2d = (Graphics2D) g;
createBall (g2d, x, y);
g2d.fill (ballArea);
if (al2x > al2xmax) reverse_al2x = true;
if (al2y > al2ymax) reverse_al2y = true;
if (al2x == 0) reverse_al2x = false;
if (al2y == 0) reverse_al2y = false;
if (reverse_al2x) al2x-=10;
else al2x+=10;
if (reverse_al2y) al2y-=10;
else al2y+=10;
if (al1y > al1ymax) reverse_al1y = true;
if (al1y == 0) reverse_al1y = false;
if (reverse_al1y) al1y-=20;
else al1y+=20;
if (al3x > al3xmax) reverse_al3x = true;
if (al3x == 0) reverse_al3x = false;
if (reverse_al3x) al3x-=20;
else al3x+=20;
if (al4x > al4xmax) reverse_al4x = true;
if (al4x == 0) reverse_al4x = false;
if (reverse_al4x) al4x-=10;
else al4x+=10;
}
public void stop() {
// The thread has been killed, stop the thread and set it to null
animThread = null;
offImage = null;
offGraphics = null;
}
public void destroy() {
}
public void createBall (Graphics2D g2d, int x_coord, int y_coord)
{
Ellipse2D.Double ball = new Ellipse2D.Double (x_coord, y_coord, 60, 60);
ballArea = new Area (ball);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -