用JAVA代码画太阳花 太阳花编程代码

java编写花朵绽放的程序代码是什么

public class Flower {

创新互联公司-成都网站建设公司,专注网站设计、网站制作、网站营销推广,域名注册,网页空间,网站托管运营有关企业网站制作方案、改版、费用等问题,请联系创新互联公司

public static void main(String[] args) {

System.out.println("花朵绽放中...");

for (int i = 0; i 10; i++) {

System.out.println("芹液枯第" + (i + 1) + "朵埋瞎花开了!嫌洞");

}

}

}

用JAVA做个画笔,有画笔和橡皮功能就行。越简单越好

新建一个java文件,取名叫做PaintJava.java

代码如下:

import java.applet.*;

import java.awt.*;

import java.awt.event.*;

import java.util.*;

import javax.swing.*;

import java.awt.geom.*;

import java.io.*;

class Point implements Serializable

{

int x,y;

Color col;

int tool;

int boarder;

Point(int x, int y, Color col, int tool, int boarder)

{

this.x = x;

this.y = y;

this.col = col;

this.tool = tool;

this.boarder = boarder;

}

}

class paintboard extends Frame implements ActionListener,MouseMotionListener,MouseListener,ItemListener

{

int x = -1, y = -1;

int con = 1;//画笔大镇带小

int Econ = 5;//橡皮大小

int toolFlag = 0;//toolFlag:工具标记

//toolFlag工具对应表:

//(0--画笔);(1--橡皮);(2--清除);郑派

//(3--直线);(4--圆);(5--矩形);

Color c = new Color(0,0,0); //画笔颜色

BasicStroke size = new BasicStroke(con,BasicStroke.CAP_BUTT,BasicStroke.JOIN_BEVEL);//画笔粗细

Point cutflag = new Point(-1, -1, c, 6, con);//截断标志

Vector paintInfo = null;//点信息向量组

int n = 1;

FileInputStream picIn = null;

FileOutputStream picOut = null;

ObjectInputStream VIn = null;

ObjectOutputStream VOut = null;

// *工具御丛芦面板--画笔,直线,圆,矩形,多边形,橡皮,清除*/

Panel toolPanel;

Button eraser, drLine,drCircle,drRect;

Button clear ,pen;

Choice ColChoice,SizeChoice,EraserChoice;

Button colchooser;

Label 颜色,大小B,大小E;

//保存功能

Button openPic,savePic;

FileDialog openPicture,savePicture;

paintboard(String s)

{

super(s);

addMouseMotionListener(this);

addMouseListener(this);

paintInfo = new Vector();

/*各工具按钮及选择项*/

//颜色选择

ColChoice = new Choice();

ColChoice.add("black");

ColChoice.add("red");

ColChoice.add("blue");

ColChoice.add("green");

ColChoice.addItemListener(this);

//画笔大小选择

SizeChoice = new Choice();

SizeChoice.add("1");

SizeChoice.add("3");

SizeChoice.add("5");

SizeChoice.add("7");

SizeChoice.add("9");

SizeChoice.addItemListener(this);

//橡皮大小选择

EraserChoice = new Choice();

EraserChoice.add("5");

EraserChoice.add("9");

EraserChoice.add("13");

EraserChoice.add("17");

EraserChoice.addItemListener(this);

////////////////////////////////////////////////////

toolPanel = new Panel();

clear = new Button("清除");

eraser = new Button("橡皮");

pen = new Button("画笔");

drLine = new Button("画直线");

drCircle = new Button("画圆形");

drRect = new Button("画矩形");

openPic = new Button("打开图画");

savePic = new Button("保存图画");

colchooser = new Button("显示调色板");

//各组件事件监听

clear.addActionListener(this);

eraser.addActionListener(this);

pen.addActionListener(this);

drLine.addActionListener(this);

drCircle.addActionListener(this);

drRect.addActionListener(this);

openPic.addActionListener(this);

savePic.addActionListener(this);

colchooser.addActionListener(this);

颜色 = new Label("画笔颜色",Label.CENTER);

大小B = new Label("画笔大小",Label.CENTER);

大小E = new Label("橡皮大小",Label.CENTER);

//面板添加组件

toolPanel.add(openPic);

toolPanel.add(savePic);

toolPanel.add(pen);

toolPanel.add(drLine);

toolPanel.add(drCircle);

toolPanel.add(drRect);

toolPanel.add(颜色); toolPanel.add(ColChoice);

toolPanel.add(大小B); toolPanel.add(SizeChoice);

toolPanel.add(colchooser);

toolPanel.add(eraser);

toolPanel.add(大小E); toolPanel.add(EraserChoice);

toolPanel.add(clear);

//工具面板到APPLET面板

add(toolPanel,BorderLayout.NORTH);

setBounds(60,60,900,600); setVisible(true);

validate();

//dialog for save and load

openPicture = new FileDialog(this,"打开图画",FileDialog.LOAD);

openPicture.setVisible(false);

savePicture = new FileDialog(this,"保存图画",FileDialog.SAVE);

savePicture.setVisible(false);

openPicture.addWindowListener(new WindowAdapter()

{

public void windowClosing(WindowEvent e)

{ openPicture.setVisible(false); }

});

savePicture.addWindowListener(new WindowAdapter()

{

public void windowClosing(WindowEvent e)

{ savePicture.setVisible(false); }

});

addWindowListener(new WindowAdapter()

{

public void windowClosing(WindowEvent e)

{ System.exit(0);}

});

}

public void paint(Graphics g)

{

Graphics2D g2d = (Graphics2D)g;

Point p1,p2;

n = paintInfo.size();

if(toolFlag==2)

g.clearRect(0,0,getSize().width,getSize().height);//清除

for(int i=0; in ;i++){

p1 = (Point)paintInfo.elementAt(i);

p2 = (Point)paintInfo.elementAt(i+1);

size = new BasicStroke(p1.boarder,BasicStroke.CAP_BUTT,BasicStroke.JOIN_BEVEL);

g2d.setColor(p1.col);

g2d.setStroke(size);

if(p1.tool==p2.tool)

{

switch(p1.tool)

{

case 0://画笔

Line2D line1 = new Line2D.Double(p1.x, p1.y, p2.x, p2.y);

g2d.draw(line1);

break;

case 1://橡皮

g.clearRect(p1.x, p1.y, p1.boarder, p1.boarder);

break;

case 3://画直线

Line2D line2 = new Line2D.Double(p1.x, p1.y, p2.x, p2.y);

g2d.draw(line2);

break;

case 4://画圆

Ellipse2D ellipse = new Ellipse2D.Double(p1.x, p1.y, Math.abs(p2.x-p1.x) , Math.abs(p2.y-p1.y));

g2d.draw(ellipse);

break;

case 5://画矩形

Rectangle2D rect = new Rectangle2D.Double(p1.x, p1.y, Math.abs(p2.x-p1.x) , Math.abs(p2.y-p1.y));

g2d.draw(rect);

break;

case 6://截断,跳过

i=i+1;

break;

default :

}//end switch

}//end if

}//end for

}

public void itemStateChanged(ItemEvent e)

{

if(e.getSource()==ColChoice)//预选颜色

{

String name = ColChoice.getSelectedItem();

if(name=="black")

{c = new Color(0,0,0); }

else if(name=="red")

{c = new Color(255,0,0);}

else if(name=="green")

{c = new Color(0,255,0);}

else if(name=="blue")

{c = new Color(0,0,255);}

}

else if(e.getSource()==SizeChoice)//画笔大小

{

String selected = SizeChoice.getSelectedItem();

if(selected=="1")

{

con = 1;

size = new BasicStroke(con,BasicStroke.CAP_BUTT,BasicStroke.JOIN_BEVEL);

}

else if(selected=="3")

{

con = 3;

size = new BasicStroke(con,BasicStroke.CAP_BUTT,BasicStroke.JOIN_BEVEL);

}

else if(selected=="5")

{con = 5;

size = new BasicStroke(con,BasicStroke.CAP_BUTT,BasicStroke.JOIN_BEVEL);

}

else if(selected=="7")

{con = 7;

size = new BasicStroke(con,BasicStroke.CAP_BUTT,BasicStroke.JOIN_BEVEL);

}

else if(selected=="9")

{con = 9;

size = new BasicStroke(con,BasicStroke.CAP_BUTT,BasicStroke.JOIN_BEVEL);

}

}

else if(e.getSource()==EraserChoice)//橡皮大小

{

String Esize = EraserChoice.getSelectedItem();

if(Esize=="5")

{ Econ = 5*2; }

else if(Esize=="9")

{ Econ = 9*2; }

else if(Esize=="13")

{ Econ = 13*2; }

else if(Esize=="17")

{ Econ = 17*3; }

}

}

public void mouseDragged(MouseEvent e)

{

Point p1 ;

switch(toolFlag){

case 0://画笔

x = (int)e.getX();

y = (int)e.getY();

p1 = new Point(x, y, c, toolFlag, con);

paintInfo.addElement(p1);

repaint();

break;

case 1://橡皮

x = (int)e.getX();

y = (int)e.getY();

p1 = new Point(x, y, null, toolFlag, Econ);

paintInfo.addElement(p1);

repaint();

break;

default :

}

}

public void mouseMoved(MouseEvent e) {}

public void update(Graphics g)

{

paint(g);

}

public void mousePressed(MouseEvent e)

{

Point p2;

switch(toolFlag){

case 3://直线

x = (int)e.getX();

y = (int)e.getY();

p2 = new Point(x, y, c, toolFlag, con);

paintInfo.addElement(p2);

break;

case 4: //圆

x = (int)e.getX();

y = (int)e.getY();

p2 = new Point(x, y, c, toolFlag, con);

paintInfo.addElement(p2);

break;

case 5: //矩形

x = (int)e.getX();

y = (int)e.getY();

p2 = new Point(x, y, c, toolFlag, con);

paintInfo.addElement(p2);

break;

default :

}

}

public void mouseReleased(MouseEvent e)

{

Point p3;

switch(toolFlag){

case 0://画笔

paintInfo.addElement(cutflag);

break;

case 1: //eraser

paintInfo.addElement(cutflag);

break;

case 3://直线

x = (int)e.getX();

y = (int)e.getY();

p3 = new Point(x, y, c, toolFlag, con);

paintInfo.addElement(p3);

paintInfo.addElement(cutflag);

repaint();

break;

case 4: //圆

x = (int)e.getX();

y = (int)e.getY();

p3 = new Point(x, y, c, toolFlag, con);

paintInfo.addElement(p3);

paintInfo.addElement(cutflag);

repaint();

break;

case 5: //矩形

x = (int)e.getX();

y = (int)e.getY();

p3 = new Point(x, y, c, toolFlag, con);

paintInfo.addElement(p3);

paintInfo.addElement(cutflag);

repaint();

break;

default:

}

}

public void mouseEntered(MouseEvent e){}

public void mouseExited(MouseEvent e){}

public void mouseClicked(MouseEvent e){}

public void actionPerformed(ActionEvent e)

{

if(e.getSource()==pen)//画笔

{toolFlag = 0;}

if(e.getSource()==eraser)//橡皮

{toolFlag = 1;}

if(e.getSource()==clear)//清除

{

toolFlag = 2;

paintInfo.removeAllElements();

repaint();

}

if(e.getSource()==drLine)//画线

{toolFlag = 3;}

if(e.getSource()==drCircle)//画圆

{toolFlag = 4;}

if(e.getSource()==drRect)//画矩形

{toolFlag = 5;}

if(e.getSource()==colchooser)//调色板

{

Color newColor = JColorChooser.showDialog(this,"调色板",c);

c = newColor;

}

if(e.getSource()==openPic)//打开图画

{

openPicture.setVisible(true);

if(openPicture.getFile()!=null)

{

int tempflag;

tempflag = toolFlag;

toolFlag = 2 ;

repaint();

try{

paintInfo.removeAllElements();

File filein = new File(openPicture.getDirectory(),openPicture.getFile());

picIn = new FileInputStream(filein);

VIn = new ObjectInputStream(picIn);

paintInfo = (Vector)VIn.readObject();

VIn.close();

repaint();

toolFlag = tempflag;

}

catch(ClassNotFoundException IOe2)

{

repaint();

toolFlag = tempflag;

System.out.println("can not read object");

}

catch(IOException IOe)

{

repaint();

toolFlag = tempflag;

System.out.println("can not read file");

}

}

}

if(e.getSource()==savePic)//保存图画

{

savePicture.setVisible(true);

try{

File fileout = new File(savePicture.getDirectory(),savePicture.getFile());

picOut = new FileOutputStream(fileout);

VOut = new ObjectOutputStream(picOut);

VOut.writeObject(paintInfo);

VOut.close();

}

catch(IOException IOe)

{

System.out.println("can not write object");

}

}

}

}//end paintboard

public class PaintJava

{

public static void main(String args[])

{ new paintboard("画图程序"); }

}

累啊..给分吧...!!

java烟花程序看不懂,求解释!

import java.awt.*;

import java.applet.*;

import java.awt.event.*;

import javax.swing.*;

public class Fireworks extends Applet implements MouseListener, Runnable {

int x, y;//记录鼠标点击的坐标

int top, point;//好像没用到

public void init() {

x = 0;

y = 0;

setBackground(Color.black);// 设置背景色为黑色

addMouseListener(this);//添加鼠标监听

}

public void paint(Graphics g) {

}

public static void main(String args[]) {

Fireworks applet = new Fireworks();

JFrame frame = new JFrame("TextAreaNew");

frame.addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent e) {//右上角的叉

System.exit(0);

}

});

frame.add(applet, BorderLayout.CENTER);

frame.setSize(800, 400);//程序的框框大小

applet.init();

applet.start();

frame.setVisible(true);//

}

public void run() {

// 变量初始化

Graphics g1;

g1 = getGraphics();//这是画笔,得到一个画笔

int y_move, y_click, x_click;

int v;//用于计算等待的时间

x_click = x;

y_click = y;//把点击的坐标中败保存下来,因为其它线程会去改这两个坐标。

y_move = 400;//用来计算变动的那个点,现在是屏幕的最大高度

v = 3;

int r, g, b;

while (y_move  y_click)//如果点击的位置小于最大高度。

{

g1.setColor(Color.black);//画笔设成黑色

g1.fillOval(x_click, y_move, 5, 5);//画圆,圆点在点击的X轴,程序界面的最高点,长为5,宽为5

y_move -= 5;//最高点-5

r = (((int) Math.round(Math.random() * 4321)) % 200) + 55;

g = (((int) Math.round(Math.random() * 4321)) % 200) + 55;

b = (((int) Math.round(Math.random() * 4321)) % 200) + 55;//rgb是光的三原色,这个就是烟花产生的颜色,这里定义成随机的,但在一个范围里

g1.setColor(new Color(r, g, b));//把画笔改成那个颜色

g1.fillOval(x_click, y_move, 5, 5);//画一个亮拿这样的圆

for (int j = 0; j = 10; j++) {

if (r  55)

r -= 20;

if (g  55)

g -= 20;

if (b  55)

b -= 20;

g1.setColor(new Color(r, g, b));

g1.fillOval(x_click, y_move + j * 5, 5, 5);//这一段都是改变颜色,然后画圆的

}

g1.setColor(Color.black);

g1.fillOval(x_click, y_move + 5 * 10, 5, 5);//把上一次画的彩色圆,用黑色画一遍,就能让它消失在背景里

try {

Thread.currentThread().sleep(v++);//让程序等一下,让你看到效果,不然画完的东西一下就不见了,你看不清。

} catch (InterruptedException e) {

}

}//上面这段代码是烟花的升上去的那一串东西的卖键颤效果

for (int j = 12; j = 0; j--) {

g1.setColor(Color.black);

g1.fillOval(x_click, y_move + (j * 5), 5, 5);

try {

Thread.currentThread().sleep((v++) / 3);

} catch (InterruptedException e) {

}

}//让最后的那串东西的点消失

y_move = 400;

g1.setColor(Color.black);

while (y_move  y_click) {

g1.fillOval(x_click - 2, y_move, 9, 5);

y_move -= 5;

}//这段不太清楚是干什么的,我把它去掉,看不出效果的变化

v = 15;

for (int i = 0; i = 25; i++) {

r = (((int) Math.round(Math.random() * 4321)) % 200) + 55;

g = (((int) Math.round(Math.random() * 4321)) % 200) + 55;

b = (((int) Math.round(Math.random() * 4321)) % 200) + 55;

g1.setColor(new Color(r, g, b));

g1.drawOval(x_click - 3 * i, y_click - 3 * i, 6 * i, 6 * i);

if (i  23) {

g1.drawOval(x_click - 3 * (i + 1), y_click - 3 * (i + 1),

6 * (i + 1), 6 * (i + 1));

g1.drawOval(x_click - 3 * (i + 2), y_click - 3 * (i + 2),

6 * (i + 2), 6 * (i + 2));

}//上面这段是画爆炸的效果

try {

Thread.currentThread().sleep(v++);//停一下,看效果

} catch (InterruptedException e) {

}

g1.setColor(Color.black);

g1.drawOval(x_click - 3 * i, y_click - 3 * i, 6 * i, 6 * i);//然后画黑圈,相当于让彩圈消失。

}

}

public void mousePressed(MouseEvent e) {//点击从这里开始~~~~~~~~~~~~~~

x = e.getX();

y = e.getY();//得到鼠标点击的坐标

Thread one = new Thread(this);//新建一个线程

one.start();//启动这个线程,到上面的run方法

one = null;//把这个线程置为空,让它执行完以后就释放

}

如果你想一下自己写要怎样写这个程序,就很容易理解这个程序了。

一直从下向上画圆,然后把下面的圆擦掉,就能得到一个向上升的烟花效果,

爆炸效果就是先画小圆再画大圆,然后擦掉小圆,再擦掉大圆。

如图,如何用Java代码写?

Java程序:

import java.awt.Graphics;

import javax.swing.JFrame;

import javax.swing.JPanel;

public class Exercise12_10 extends JFrame {

private static final long serialVersionUID = 1L;

private CanvasPanel pnlCanvas = null;

public Exercise12_10(){

super("Exercise12_10");

pnlCanvas = new CanvasPanel();

this.add(pnlCanvas);

this.setSize(250, 250);

this.setVisible(true);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

public static void main(String[] args) {

new Exercise12_10();

}

}

class CanvasPanel 察并蠢extends JPanel{

private static final long serialVersionUID = 1L;

public void paint(Graphics g){

int W = this.getWidth();

int H = this.getHeight();

int i, j;

int width = W / 8;

int height = H / 8;

int x, y;

for(i=0; i9; i++){

for(j=0; j8; j++){

x = j * width;

y = i * height;

if((i+j)%2 == 0){//画矩形

g.drawRect(x, 蔽肆y, width, height);

}

else{//填充矩形败陪

g.fillRect(x, y, width, height);

}

}

}

}

}

运行结果如图所示:


当前标题:用JAVA代码画太阳花 太阳花编程代码
本文路径:http://cdiso.cn/article/ddpgdod.html

其他资讯