这只乌龟画得真的很可爱。这两天边学边做把它改成能动的了。
用键盘的方向键控制。ESC键退出。
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;
class WuGui extends JFrame implements KeyListener
{
Container con=this.getContentPane();
MyPanel m=new MyPanel(true);
public WuGui()
{
this.setSize(Toolkit.getDefaultToolkit().getScreenSize());
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setUndecorated(true);
this.setAlwaysOnTop(true);
con.setBackground(new Color(0,128,255));
m.setOpaque(false);
con.add(m);
this.addKeyListener(this);
this.setVisible(true);
}
public static void main(String args[])
{
new WuGui();
}
public void keyTyped(KeyEvent e) {
}
public void keyPressed(KeyEvent e) {
}
public void keyReleased(KeyEvent e) {
m.setKey(e.getKeyCode());
}
}
class MyPanel extends JPanel
{
int x=50;
int y=50;
int key=40;
private AffineTransform t= new AffineTransform();
public MyPanel(boolean buffer)
{
super(buffer);
setRotate(180);
}
public void setKey(int key)
{
this.key=key;
}
public void paint(Graphics g)
{
super.paint(g);
Graphics2D g2=(Graphics2D)g;
g2.transform(t);
move();
drawWuGui(g2);
back();
try
{
Thread.currentThread().sleep(20);
}
catch(Exception e)
{
e.printStackTrace();
}
repaint();
}
public void drawWuGui(Graphics2D g2)
{
g2.setColor(Color.GREEN);
g2.fill(new Ellipse2D.Double(x+30,y+50,140,200));//body
g2.fill(new Ellipse2D.Double(x+70,y,60,100));//head
g2.fill(new Ellipse2D.Double(x+10,y+70,70,30));//leftUp
g2.fill(new Ellipse2D.Double(x+10,y+200,70,30));//leftDown
g2.fill(new Ellipse2D.Double(x+120,y+70,70,30)); //rightUp
g2.fill(new Ellipse2D.Double(x+120,y+200,70,30));//rightDown
g2.setColor(Color.BLACK);
g2.fill(new Ellipse2D.Double(x+70,y+20,10,10));//leftEye
g2.fill(new Ellipse2D.Double(x+120,y+20,10,10));//rightEye
}
public void setRotate(int jiao)
{
t.setToRotation(Math.toRadians(jiao),x+100,y+150);
}
public void move()
{
if(key==37)
{
setRotate(270);
x-=10;
}
if(key==38)
{
setRotate(360);
y-=10;
}
if(key==39)
{
setRotate(90);
x+=10;
}
if(key==40)
{
setRotate(180);
y+=10;
}
if(key==KeyEvent.VK_ESCAPE)
{
System.exit(0);
}
}
private void back()
{
if(x<=0 && key == 37)
{
key = 39;
}
if(x>=(this.getWidth()-240) && key == 39)
{
key = 37;
}
if(y<=0 && key == 38)
{
key = 40;
}
if(y>=(this.getHeight()-240) && key == 40)
{
key = 38;
}
}
}