重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
首先,手动画一个小乌龟,如下:
创新互联专注于铁山企业网站建设,成都响应式网站建设公司,商城网站制作。铁山网站建设公司,为铁山等地区提供建站服务。全流程按需定制网站,专业设计,全程项目跟踪,创新互联专业和态度为您提供的服务
然后,按照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;
}
}
帮你改了一下。
你要画在panel上,然后frame.add(panel)就能显示。
是不是和applet搞混了,applet复写一些方法就能显示,但现在你编的是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)); // 设置线条宽度,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)); // 设置线条宽度,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的设置给覆盖了;要是先添加画图再添加
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);
}
}
运行结果如下:
感觉挺有趣的,试着写了个~
public static void main(String[] arg) {
new wugui().run();
new tuzi().run();
}
static class wugui {
final int sudu = 4;// 乌龟的速度是每秒4米
public static boolean hasEnd = false;// 是否已经跑到终点
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("呜呜,差一点点就赢了~");
} else {
System.out.println("胜利是属于有准备的人的,你的自大害了你!-------乌龟赢了");
}
}
}.start();
}
}
static class tuzi {
final int sudu = 5;// 兔子的速度是每秒5米
public static boolean hasEnd = false;// 是否已经跑到终点
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("小兔子休息够了,又开始跑了,决一胜负吧!");
hasXiuXi = true;
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
hasEnd = true;
if (wugui.hasEnd) {
System.out.println("呜呜,早知道就不休息了~");
} else {
System.out.println("哇哈哈,你个战5渣也想赢我~~做梦去吧!!-------兔子赢了");
}
}
}.start();
}
}