十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
首先,手动画一个小乌龟,如下:
成都创新互联公司长期为上1000家客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为牟定企业提供专业的成都网站建设、成都网站制作,牟定网站改版等技术服务。拥有10年丰富建站经验和众多成功案例,为您定制开发。
然后,按照Java绘图基本步骤一步步来。
swing 编程步骤:
1. 继承JFrame
2. 定义组件
3.创建组件(构造函数)
4.添加组件
5.对窗体设置
6.显示窗体
最终效果如下:
代码如下:
/**
* 功能:画一个乌龟
*/
package com.test1;
import java.awt.*;
import javax.swing.*;
public class MyTortoise extends JFrame{
MyPanel2 mp = null;
//构造函数
public MyTortoise(){
mp = new MyPanel2();
this.add(mp);
this.setTitle("小乌龟,丑丑哒");
this.setSize(400,300);
this.setVisible(true);
this.setLocation(300,200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
MyTortoise mtg = new MyTortoise();
}
}
//我的面板。只有JPanel有画图方法,JFrame没有,故必须在JFrame中添加JPanel
class MyPanel2 extends JPanel{
//定义一个乌龟
Tortoise t = null;
//构造函数
public MyPanel2(){
t = new Tortoise(100,100);
}
//画乌龟
public void drawTortoise(int x, int y, Graphics g){
//1.画脸
g.setColor(Color.green);
g.fillOval(x+60, y, 30, 15);
//2.画左眼
g.setColor(Color.black);
g.fillOval(x+65, y+3, 5, 5);
//3.画右眼
g.fillOval(x+78, y+3, 5, 5);
//4.画脖子
g.setColor(Color.green);
g.fillOval(x+70, y, 10, 42);
//5.画乌龟壳
g.setColor(Color.red);
g.fillOval(x+40, y+40, 70, 100);
//6.画左上脚
g.setColor(Color.green);
g.fillOval(x+15, y+60, 30, 10);
//7.画右上脚
g.fillOval(x+105, y+60, 30, 10);
//8.画左下脚
g.fillOval(x+15, y+110, 30, 10);
//9.画右下脚
g.fillOval(x+105, y+110, 30, 10);
//10.画尾巴
g.setColor(Color.black);
g.drawLine(x+70,y+140,x+130,y+210);
g.drawOval(x+95, y+150, 30, 30);
}
//覆盖JPanel的paint方法
//Graphics 是绘图的重要类。你可以把他理解成一只画笔
public void paint(Graphics g){
//1.调用父类函数完成初始化任务
//这句话不能少
super.paint(g);
//2.画乌龟,调用方法即可
this.drawTortoise(50, 50, g);
}
}
//定义一个乌龟类
class Tortoise {
//表示乌龟的横坐标
int x = 0;
//表示乌龟的纵坐标
int y = 0;
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public Tortoise(int x, int y){
this.x = x;
this.y = y;
}
}
jose 不动 ,maria forward(40) turn(-90)
这是java 中的方法传参问题 ,在java中参数类型是引用类型,传的是这个引用参数的引用的副本,在dosth()中,这个引用turtle指向了maria的地址,改变的都是maria值
import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Random; import javax.swing.Icon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; //定义一个JFrame窗体作为显示载体 public class TestTortoiseRabbit extends JFrame { private static final long serialVersionUID = 987654321L; static final int MAX_LENGTH = 700; private JButton begin = new JButton("开始"); private JButton exit = new JButton("退出"); private Rabbit rabbit = new Rabbit("兔子"); private Tortoise tortoise = new Tortoise("乌龟"); private JLabel stateOfRabbit = new JLabel(); private JLabel winner; public TestTortoiseRabbit() { super("龟兔赛跑"); setLayout(null); setBounds(200, 150, 800, 400); setDefaultCloseOperation(EXIT_ON_CLOSE); add(rabbit); rabbit.setBounds(20, 100, 50, 20); add(tortoise); tortoise.setBounds(20, 150, 50 ,20); add(stateOfRabbit); stateOfRabbit.setBounds(300, 50, 100 ,20); add(begin);begin.setBounds(20, 200, 80, 30); add(exit);exit.setBounds(100, 200, 80, 30); setVisible(true); begin.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { new Thread(rabbit).start(); new Thread(tortoise).start(); begin.setVisible(false); stateOfRabbit.setText("兔子跑起来了!"); } }); exit.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.exit(0); } }); } public static void main(String[] args) { new TestTortoiseRabbit(); } class Tortoise extends JLabel implements Runnable { private static final long serialVersionUID = 12345678L; public Tortoise(String name) { super(name); } public Tortoise(Icon icon) { super(icon); } boolean stop; int speed = 2; int x, y; public void run() { x = getLocation().x; y = getLocation().y; while (!stop) { x += speed; setLocation(x, y); try { Thread.sleep(50); } catch (InterruptedException e) { e.printStackTrace(); } if (x = MAX_LENGTH) { if (TestTortoiseRabbit.this.winner == null) { TestTortoiseRabbit.this.winner = this; } else { JOptionPane.showMessageDialog(TestTortoiseRabbit.this, TestTortoiseRabbit.this.winner.getText()+ "胜利!"); } stop = true; } } } } class Rabbit extends JLabel implements Runnable { private static final long serialVersionUID = 123456789L; public Rabbit(String name) { super(name); } public Rabbit(Icon icon) { super(icon); } boolean stop; int speed = 5; int x, y; Random r = new Random(); public void run() { x = getLocation().x; y = getLocation().y; while (!stop) { x += speed; setLocation(x, y); try { Thread.sleep(50); if(r.nextInt(100) 90){ if(speed == 0){ speed = 3; stateOfRabbit.setText("兔子跑起来了!"); }else{ speed = 0; stateOfRabbit.setText("兔子在睡觉!"); } } } catch (InterruptedException e) { e.printStackTrace(); } if (x = MAX_LENGTH) { stateOfRabbit.setText("兔子到终点了!"); if (TestTortoiseRabbit.this.winner == null) { TestTortoiseRabbit.this.winner = this; } else { JOptionPane.showMessageDialog(TestTortoiseRabbit.this, TestTortoiseRabbit.this.winner.getText()+ "胜利!"); } stop = true; } } } } }