首先,手動畫一個小烏龜,如下:
成都創(chuàng)新互聯(lián)公司長期為上1000家客戶提供的網(wǎng)站建設(shè)服務(wù),團隊從業(yè)經(jīng)驗10年,關(guān)注不同地域、不同群體,并針對不同對象提供差異化的產(chǎn)品和服務(wù);打造開放共贏平臺,與合作伙伴共同營造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為牟定企業(yè)提供專業(yè)的成都網(wǎng)站建設(shè)、成都網(wǎng)站制作,牟定網(wǎng)站改版等技術(shù)服務(wù)。擁有10年豐富建站經(jīng)驗和眾多成功案例,為您定制開發(fā)。
然后,按照Java繪圖基本步驟一步步來。
swing 編程步驟:
1. 繼承JFrame
2. 定義組件
3.創(chuàng)建組件(構(gòu)造函數(shù))
4.添加組件
5.對窗體設(shè)置
6.顯示窗體
最終效果如下:
代碼如下:
/**?
*?功能:畫一個烏龜?
*/??
package?com.test1;??
import?java.awt.*;??
import?javax.swing.*;??
public?class?MyTortoise??extends?JFrame{??
MyPanel2?mp?=?null;??
//構(gòu)造函數(shù)??
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;??
//構(gòu)造函數(shù)??
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.調(diào)用父類函數(shù)完成初始化任務(wù)??
//這句話不能少??
super.paint(g);??
//2.畫烏龜,調(diào)用方法即可??
this.drawTortoise(50,?50,?g);??
}??
}??
//定義一個烏龜類??
class?Tortoise?{??
//表示烏龜?shù)臋M坐標??
int?x?=?0;??
//表示烏龜?shù)目v坐標??
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中參數(shù)類型是引用類型,傳的是這個引用參數(shù)的引用的副本,在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; } } } } }