java画王八的代码,java绘画图的代码
java画乌龟
首先,手动画一个小乌龟,如下:
创新互联公司专注于二七网站建设服务及定制,我们拥有丰富的企业做网站经验。 热诚为您提供二七营销型网站建设,二七网站制作、二七网页设计、二七网站官网定制、重庆小程序开发服务,打造二七网络公司原创品牌,更为您提供二七网站排名全网营销落地服务。
然后,按照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;
}
}
用JAVA编程:编写GUI程序,模拟龟兔赛跑游戏
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; } } } } }
java绘图,求代码
上代码:
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坐标");
yLabel=new JLabel("Y坐标");
this.setTitle("圆的移动");
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);
}
}
}
给分吧!
用java 在窗体中画一个简单图形。
帮你改了一下。
你要画在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画王八的代码,java绘画图的代码
网站URL:http://scyanting.com/article/dsgsihj.html