📄 graphicsanimation.java
字号:
import java.awt.*;
import java.applet.*;
public class GraphicsAnimation extends Applet implements Runnable
{
int windowWidth,windowHeight;
Image offScreenImage;
Graphics offScreen;
Thread threadAnimation=null;
int x,y;
int xSpeed,ySpeed;
public void init()
{
initForm();
usePageParams();
initParameters();
}
private final String labelParam = "label";
private final String backgroundParam = "background";
private final String foregroundParam = "foreground";
/**
* Reads parameters from the applet's HTML host and sets applet
* properties.
*/
private void usePageParams()
{
final String defaultLabel = "Default label";
final String defaultBackground = "C0C0C0";
final String defaultForeground = "000000";
String labelValue;
String backgroundValue;
String foregroundValue;
/**
* Read the <PARAM NAME="label" VALUE="some string">,
* <PARAM NAME="background" VALUE="rrggbb">,
* and <PARAM NAME="foreground" VALUE="rrggbb"> tags from
* the applet's HTML host.
*/
labelValue = getParameter(labelParam);
backgroundValue = getParameter(backgroundParam);
foregroundValue = getParameter(foregroundParam);
if ((labelValue == null) || (backgroundValue == null) ||
(foregroundValue == null))
{
/**
* There was something wrong with the HTML host tags.
* Generate default values.
*/
labelValue = defaultLabel;
backgroundValue = defaultBackground;
foregroundValue = defaultForeground;
}
/**
* Set the applet's string label, background color, and
* foreground colors.
*/
label1.setText(labelValue);
label1.setBackground(stringToColor(backgroundValue));
label1.setForeground(stringToColor(foregroundValue));
this.setBackground(stringToColor(backgroundValue));
this.setForeground(stringToColor(foregroundValue));
}
/**
* Converts a string formatted as "rrggbb" to an awt.Color object
*/
private Color stringToColor(String paramValue)
{
int red;
int green;
int blue;
red = (Integer.decode("0x" + paramValue.substring(0,2))).intValue();
green = (Integer.decode("0x" + paramValue.substring(2,4))).intValue();
blue = (Integer.decode("0x" + paramValue.substring(4,6))).intValue();
return new Color(red,green,blue);
}
public String[][] getParameterInfo()
{
String[][] info =
{
{ labelParam, "String", "Label string to be displayed" },
{ backgroundParam, "String", "Background color, format \"rrggbb\"" },
{ foregroundParam, "String", "Foreground color, format \"rrggbb\"" },
};
return info;
}
Label label1 = new Label();
void initForm()
{
this.setBackground(Color.lightGray);
this.setForeground(Color.black);
label1.setText("label1");
this.setLayout(new BorderLayout());
this.add("North",label1);
}
public void initParameters()
{
windowWidth=Integer.parseInt(getParameter("width"));
windowHeight=Integer.parseInt(getParameter("height"));
offScreenImage=createImage(windowWidth,windowHeight);
offScreen=offScreenImage.getGraphics();
draw_groupBox_border(offScreen);
drawBase(offScreen);
drawPillar(offScreen,25,31,15,windowHeight-60-60-2);
drawPillar(offScreen,windowWidth-25-16,31,15,windowHeight-60-40-2);
x=40;
y=60;
xSpeed=9;
ySpeed=1;
}
public void paint(Graphics g)
{
offScreen.setColor(Color.black);
offScreen.fillRect(40,31,windowWidth-80,windowHeight-70-32);
drawSphere(offScreen,x,y,16);
int h=windowHeight-50-40-1;
drawGlider(offScreen,x,h,50,20);
drawPillar(offScreen,25,h,15,20);
g.drawImage(offScreenImage,0,0,this);
}
public void draw_groupBox_border(Graphics g)
{
BorderRectangle br=new BorderRectangle(24,30,windowWidth-49,windowHeight-60);
br.setBKColor(Color.black);
br.draw(g);
}
public void drawBase(Graphics g)
{
int h=windowHeight-50;
for(int i=0;i<13;i++)drawBrick(g,25+i*50,h,50,20);
Brick brick_left=new Brick(25,h-20,25,20);
brick_left.cutLeft(g);
Brick brick_right=new Brick(650,h-20,25,20);
brick_right.cutLeft(g);
for(int i=0;i<12;i++)
drawBrick(g,50+i*50,h-20,50,20);
g.setColor(Color.lightGray);
g.drawLine(25,h-21,windowWidth-25,h-21);
}
public void drawBrick(Graphics g,int x,int y,int w,int h)
{
Brick brick=new Brick(x,y,w,h);
brick.draw(g);
}
public void drawGlider(Graphics g,int x,int y,int w,int h)
{
Brick glider=new Brick(x-12,y,w,h);
glider.setForeColor(new Color(32,128,255));
glider.draw(g);
}
public void drawPillar(Graphics g,int x,int y,int w,int h)
{
VirtualCylinder vc=new VirtualCylinder(x,y,w,h);
vc.draw(g);
}
public void drawSphere(Graphics g,int x,int y, int radius)
{
VirtualSphere vs=new VirtualSphere(x,y,radius);
vs.draw(g);
}
public void update(Graphics g)
{
paint(g);
}
public void run()
{
while(true)
{
try
{
x+=xSpeed;
if(x>windowWidth-40-16)
{
xSpeed=-xSpeed;
x=windowWidth-40-16;
}
else if(x<40)
{
xSpeed=-xSpeed;
x=40;
}
y+=ySpeed;
ySpeed++;
if(y>windowHeight-90-16)
{
y=2*(windowHeight-90-16)-y;
ySpeed=-ySpeed+4;
}
else if(y<31)
{
y=31;
}
repaint(40,31,windowWidth-80,windowHeight-70-32);
Thread.sleep(50);
}
catch(InterruptedException e)
{
stop();
}
}
}
public void start()
{
if(threadAnimation==null)
{
threadAnimation=new Thread(this);
threadAnimation.start();
}
}
public void stop()
{
if(threadAnimation!=null)
{
threadAnimation.stop();
threadAnimation=null;
}
}
public void destroy(){}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -