首先,手動畫一個(gè)小烏龜,如下:
創(chuàng)新互聯(lián)專注于鐵山企業(yè)網(wǎng)站建設(shè),成都響應(yīng)式網(wǎng)站建設(shè)公司,商城網(wǎng)站制作。鐵山網(wǎng)站建設(shè)公司,為鐵山等地區(qū)提供建站服務(wù)。全流程按需定制網(wǎng)站,專業(yè)設(shè)計(jì),全程項(xiàng)目跟蹤,創(chuàng)新互聯(lián)專業(yè)和態(tài)度為您提供的服務(wù)
然后,按照J(rèn)ava繪圖基本步驟一步步來。
swing 編程步驟:
1. 繼承JFrame
2. 定義組件
3.創(chuàng)建組件(構(gòu)造函數(shù))
4.添加組件
5.對窗體設(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;??
}??
}
幫你改了一下。
你要畫在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);
}
}
java在JFrame上畫東西,主要是使用paint方法,代碼如下:
import?java.awt.Color;
import?java.awt.Graphics;
import?javax.swing.JFrame;
import?javax.swing.JPanel;
public?class?Draw?extends?JFrame{
JPanel??jPanel=new?JPanel();
public?Draw()?{
jPanel.setBackground(Color.red);
add(jPanel);?
Drawation?drawaction=new?Drawation();//添加畫圖,把上面jpanel的設(shè)置給覆蓋了;要是先添加畫圖再添加
add(drawaction);????????????????????//jpanel則把畫圖覆蓋了
}
public?static?void?main(String[]?args){
Draw?draw=new?Draw();
draw.setTitle("abc");
draw.setSize(300,300);
draw.setVisible(true);
draw.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
class?Drawation?extends?JPanel{
public?void?paintComponent(Graphics?g){
super.paintComponents(g);
g.drawString("agagh",?50,?45);
}
}
運(yùn)行結(jié)果如下:
感覺挺有趣的,試著寫了個(gè)~
public static void main(String[] arg) {
new wugui().run();
new tuzi().run();
}
static class wugui {
final int sudu = 4;// 烏龜?shù)乃俣仁敲棵?米
public static boolean hasEnd = false;// 是否已經(jīng)跑到終點(diǎn)
public void run() {
new Thread() {
public void run() {
int distance = 0;
while (distance 100) {
try {
Thread.sleep(1000);
distance += sudu;
System.out.println("小烏龜跑了" + distance + "米");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
hasEnd = true;
if (tuzi.hasEnd) {
System.out.println("嗚嗚,差一點(diǎn)點(diǎn)就贏了~");
} else {
System.out.println("勝利是屬于有準(zhǔn)備的人的,你的自大害了你!-------烏龜贏了");
}
}
}.start();
}
}
static class tuzi {
final int sudu = 5;// 兔子的速度是每秒5米
public static boolean hasEnd = false;// 是否已經(jīng)跑到終點(diǎn)
public void run() {
new Thread() {
@Override
public void run() {
int distance = 0;// 跑了多少米
boolean hasXiuXi = false;// 是否休息過
while (distance 100) {
try {
Thread.sleep(1000);
distance += sudu;
System.out.println("小兔子跑了" + distance + "米");
if (distance 50 !hasXiuXi) {
System.out.println("小兔子累了,決定休息一會兒~");
Thread.sleep((long) (10000 * Math.random()));
System.out.println("小兔子休息夠了,又開始跑了,決一勝負(fù)吧!");
hasXiuXi = true;
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
hasEnd = true;
if (wugui.hasEnd) {
System.out.println("嗚嗚,早知道就不休息了~");
} else {
System.out.println("哇哈哈,你個(gè)戰(zhàn)5渣也想贏我~~做夢去吧??!-------兔子贏了");
}
}
}.start();
}
}