日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

会爬行的小乌龟

發布時間:2024/1/18 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 会爬行的小乌龟 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一?代碼

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{public void paint(Graphics g){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);}} }

二?運行

三?改進版——使用了雙緩沖技術

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);}} }

?

總結

以上是生活随笔為你收集整理的会爬行的小乌龟的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。