zoukankan      html  css  js  c++  java
  • 改进版——使用了双缓冲技术

    import java.awt.*;
    import java.awt.event.*;
    public class DrawTurtle
    {
    private int x, y;

    public static void main(String[] args)
    {
    new DrawTurtle();
    }

    public DrawTurtle()
    {
    x = 100;
    y = 10;
    Frame frame = new Frame("DrawTurtle");
    DrawLittleTurtle turtle = new DrawLittleTurtle();
    frame.add(turtle);
    frame.setSize(500, 500);
    frame.setVisible(true);
    frame.addWindowListener(new WindowAdapter()
    {
    public void windowClosing(WindowEvent e)
    {
    System.exit(0);
    }
    });
    turtle.requestFocus();
    turtle.addKeyListener(new KeyAdapter()
    {
    public void keyPressed(KeyEvent e)
    {
    if (e.getKeyCode() == KeyEvent.VK_UP)
    {
    y -= 10;
    }
    if (e.getKeyCode() == KeyEvent.VK_LEFT)
    {
    x -= 10;
    }
    if (e.getKeyCode() == KeyEvent.VK_RIGHT)
    {
    x += 10;
    }
    if (e.getKeyCode() == KeyEvent.VK_DOWN)
    {
    y += 10;
    }
    turtle.repaint();
    }
    });
    }

    class DrawLittleTurtle extends Canvas
    {
    private Image image;

    public void paint(Graphics g)
    {
    drawBufferedImage();
    g.drawImage(image, 0, 0, this);
    }

    private void drawBufferedImage()
    {
    image = createImage(this.getWidth(), this.getHeight());
    Graphics g = image.getGraphics();
    g.setColor(Color.YELLOW); // 乌龟四个腿
    g.fillOval(x + 0, y + 40, 30, 30);
    g.fillOval(x + 90, y + 40, 30, 30);
    g.fillOval(x + 0, y + 110, 30, 30);
    g.fillOval(x + 90, y + 110, 30, 30);
    g.fillOval(x + 50, y + 130, 20, 50); // 乌龟尾巴
    g.fillOval(x + 40, y + 0, 40, 70); // 乌龟头
    g.setColor(Color.BLACK);
    g.fillOval(x + 50, y + 15, 5, 5);
    g.fillOval(x + 65, y + 15, 5, 5);
    g.setColor(Color.GREEN); // 乌龟壳
    g.fillOval(x + 10, y + 30, 100, 120);
    g.setColor(Color.BLACK);
    g.drawLine(x + 24, y + 50, x + 40, y + 67);
    g.drawLine(x + 97, y + 50, x + 80, y + 67);
    g.drawLine(x + 24, y + 130, x + 40, y + 113);
    g.drawLine(x + 97, y + 130, x + 80, y + 113);
    g.drawLine(x + 40, y + 67, x + 80, y + 67);
    g.drawLine(x + 40, y + 113, x + 80, y + 113);
    g.drawLine(x + 10, y + 90, x + 110, y + 90);
    g.drawLine(x + 60, y + 30, x + 60, y + 150);
    }

    }
    }
    ————————————————

  • 相关阅读:
    as3的InteractivePNG例子
    HttpWebRequest模拟POST提交防止中文乱码
    net发布的dll方法和类显示注释信息(字段说明信息)[图解]
    IP地址、手机归属和身份证查询接口
    一些好用的开源控件
    c# 操作IIS应用程序池
    c# 获取电脑硬件信息通用查询类[测试通过]
    C# 操作线程的通用类[测试通过]
    几款浏览器JavaScript调试工具
    Microsoft SQL Server 2005 提供了一些工具来监控数据库
  • 原文地址:https://www.cnblogs.com/hyhy904/p/11491408.html
Copyright © 2011-2022 走看看