首先,手動(dòng)畫一個(gè)小烏龜,如下:
創(chuàng)新互聯(lián)公司專注于二七網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗(yàn)。 熱誠(chéng)為您提供二七營(yíng)銷型網(wǎng)站建設(shè),二七網(wǎng)站制作、二七網(wǎng)頁(yè)設(shè)計(jì)、二七網(wǎng)站官網(wǎng)定制、重慶小程序開發(fā)服務(wù),打造二七網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供二七網(wǎng)站排名全網(wǎng)營(yíng)銷落地服務(wù)。
然后,按照J(rèn)ava繪圖基本步驟一步步來(lái)。
swing 編程步驟:
1. 繼承JFrame
2. 定義組件
3.創(chuàng)建組件(構(gòu)造函數(shù))
4.添加組件
5.對(duì)窗體設(shè)置
6.顯示窗體
最終效果如下:
代碼如下:
/**?
*?功能:畫一個(gè)烏龜?
*/??
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{??
//定義一個(gè)烏龜??
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);??
}??
}??
//定義一個(gè)烏龜類??
class?Tortoise?{??
//表示烏龜?shù)臋M坐標(biāo)??
int?x?=?0;??
//表示烏龜?shù)目v坐標(biāo)??
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;??
}??
}
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; //定義一個(gè)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("兔子跑起來(lái)了!"); } }); 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("兔子跑起來(lái)了!"); }else{ speed = 0; stateOfRabbit.setText("兔子在睡覺!"); } } } catch (InterruptedException e) { e.printStackTrace(); } if (x = MAX_LENGTH) { stateOfRabbit.setText("兔子到終點(diǎn)了!"); if (TestTortoiseRabbit.this.winner == null) { TestTortoiseRabbit.this.winner = this; } else { JOptionPane.showMessageDialog(TestTortoiseRabbit.this, TestTortoiseRabbit.this.winner.getText()+ "勝利!"); } stop = true; } } } } }
上代碼:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.LayoutManager;
import java.awt.Paint;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import java.awt.event.*;
public class YuanYiDong extends JFrame{
private static int BANJIN=0;
private static int X=0;
private static int Y=0;
JTextField rTxt=new JTextField(5);
JTextField xField=new JTextField(5);
JTextField yField=new JTextField(5);
JButton paintBt=new JButton("畫");
JLabel huaban=new huaban();
JPanel jPanel=new JPanel();
JLabel banjingLabel,xLabel,yLabel;
public YuanYiDong(){
banjingLabel=new JLabel("半徑");
xLabel=new JLabel("X坐標(biāo)");
yLabel=new JLabel("Y坐標(biāo)");
this.setTitle("圓的移動(dòng)");
this.setLocation(300,100);
this.setSize(500, 400);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
this.add(rTxt);
jPanel.setLayout(new FlowLayout());
add(huaban,BorderLayout.CENTER);
jPanel.add(banjingLabel);
jPanel.add(rTxt);
jPanel.add(xLabel);
jPanel.add(xField);
jPanel.add(yLabel);
jPanel.add(yField);
jPanel.add(paintBt);
add(jPanel,BorderLayout.NORTH);
paintBt.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
BANJIN=Integer.parseInt(rTxt.getText());
X=Integer.parseInt(xField.getText());
Y=Integer.parseInt(yField.getText());
huaban.repaint();
}
});
}
private void drawCirlce(Graphics g) {
g.setColor(Color.blue);
g.fillOval(X, Y, BANJIN,BANJIN);
}
public static void main(String[] args) {
YuanYiDong frame = new YuanYiDong();
}
public class huaban extends JLabel{
public huaban(){}
public void paint(Graphics g) {
Image image = createImage(getWidth(), getHeight());
drawCirlce(image.getGraphics());
g.drawImage(image, 0, 0, null);
}
}
}
給分吧!
幫你改了一下。
你要畫在panel上,然后frame.add(panel)就能顯示。
是不是和applet搞混了,applet復(fù)寫一些方法就能顯示,但現(xiàn)在你編的是java gui
import java.awt.*;
import java.awt.Event.*;
import javax.swing.*; //import javax.swing.Timer;
import java.awt.BasicStroke;
//import java.util.Date;
//import java.text.*;
//import java.util.*;
public class TestGui {
public void paint(Graphics g) {
Graphics2D a2d = (Graphics2D) g;
int x = 120, y = 90, width = 150, height = 150;
a2d.setColor(Color.red);
a2d.setStroke(new BasicStroke(3.0f)); // 設(shè)置線條寬度,3.0即線的寬度
a2d.drawOval(x, y, width, height);
}
public static void main(String[] args) {
JFrame frame = new JFrame();
// frame.add(new paint(),BorderLayout.CENTER);
frame.setSize(500, 500);
frame.setLocation(200, 200);
frame.setVisible(true);
Panel p = new Panel();
frame.add(p);
// frame.paint(null);
// TODO code application logic here
}
}
class Panel extends JPanel {
// 重新覆蓋paint方法
public void paint(Graphics g) {
super.paint(g);
Graphics2D a2d = (Graphics2D) g;
int x = 120, y = 90, width = 150, height = 150;
a2d.setColor(Color.red);
a2d.setStroke(new BasicStroke(3.0f)); // 設(shè)置線條寬度,3.0即線的寬度
a2d.drawOval(x, y, width, height);
}
}