首页编程java编程java用什么当画板(java画板程序)

java用什么当画板(java画板程序)

编程之家2023-10-13112次浏览

今天给各位分享java用什么当画板的知识,其中也会对java画板程序进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

java用什么当画板(java画板程序)

Java绘图机制是什么样的

JAVA的绘图功能非常丰富,绘图包括字体、颜色、图形,以下我们将分技术专题来讲。

一、关于JAVA的绘图机制。

JAVA中的任何一个图形组件,小到文本框、标签,大到一个FRAME,一个DIALOG,都有一个专门负责显示其界面的函数,这个函数名称是固定的:paint,它的原型为: public void paint(Graphics g){……}每当组件大小、位置、组件内容发生变化时,该函数即负责生成新的图形界面显示。由于该函数可以被子类继承,因此,继承的子类有能力修改该函数。如果子类中没有出现该函数,则表示其行为完全继承自父类。则不管是组件中是否添加了新的内容,是否发生了大小的改变,是否发生了位移,父类都要有一个专门的线程,来负责描绘变化以后的组件界面。 paint函数由父类自动维护,并且如果子类一旦重载该函数,必须自己去维护所有的界面显示。

java用什么当画板(java画板程序)

二、设置画笔颜色

1、颜色常识

任何颜色都是三原色组成(RGB),JAVA中支持224位彩色,即红绿蓝色分量可取值介于0..255之间。下面首先学习一个JAVA中的类Color Color中的常量:

java用什么当画板(java画板程序)

public final static Color black=new Color(0,0,0);

public final static Color blue=new Color(0,0,255);

…..

Color的构造函数:

public Color(int r,int g,int b);

使用举例:如果想构造一个灰色对象,则用下面的句子:

Color gray=new Color(205,205,205);

2、设置画笔颜色语法

g.setColor(color);//color是一个Color对象

每修改一次颜色它影响的就是下面所有的绘图语句,一直影响到再次碰到setColor函数才以新的颜色代替。

3、使用JColorChooser组件选择颜色 JAVA中有一个已经定义好的选色器,通过简单的语法我们就可以将该窗口调出来,从其中选择自己喜欢的颜色。下面的这个例子就是通过颜色选取器选取颜色,并将选择到的颜色做为窗体的背景色。

(1)JColorChooser简介 JColorChooser组件的showDialog()方法让用户从弹出的窗口中选择一个颜色,并传给Color对象。其调用语法如下: color=JColorChooser.showDialog(this,”选色”,color);第一个参数指定调用选色器的父窗体,第二个参数指定选色器窗口标题,最后一个为接收颜色的颜色对象。

4、如何将一个图形(以文件存在,如JPG或者GIF)画到窗体的画布中其实放置图形到画板中实际就是调用了画板的drawImage函数。其大致思路如下:首先获取一个ImageIcon对象,这个对象将会从指定的文件中读取相关图象信息,它支持GIF和JPG、BMP等基本图象格式。语法如下:

ImageIcon icon=new ImageIcon(GraExp5.class.getResource("1.gif"));

获取到图象的图标以后,就可以从图标中获取到绘制到画板上的实际需要的图象:

Image img=icon.getImage();

有了这个图象对象,我们就可以用画板的drawImage函数画图了。

g.drawImage(img,0,0,null);

java编制一个具有如下界面的画板, 能进行基本图形的绘制。

package bdzhidao;

import java.awt.*;

import javax.swing.*;

import java.awt.event.*;

public class DrawingBoard extends JFrame{

int[] xPoint={200,300,500};

int[] yPoint={100,180,240};

static int m=15;

static Color color=new Color(255,0,0);

private JComboBox jcb2=new JComboBox(new Object[]{

" 20"," 28"," 36"," 44"," 52"," 72"

});

private JComboBox jcb3=new JComboBox(new Object[]{

"红色","黑色","蓝色","绿色"

});

public DrawingBoard(){

JMenuBar Bar=new JMenuBar();

JMenu jmFile=new JMenu("文件");

JMenuItem jmi1=new JMenuItem("新建");

JMenuItem jmi2=new JMenuItem("打开");

JMenuItem jmi3=new JMenuItem("保存");

JMenuItem jmi4=new JMenuItem("退出");

jmFile.add(jmi1);

jmFile.add(jmi2);

jmFile.add(jmi3);

jmFile.add(jmi4);

JLabel jlb1=new JLabel("线条");

JLabel jlb2=new JLabel("线宽");

JLabel jlb3=new JLabel("颜色");

final JComboBox jcb1=new JComboBox(new Object[]{

"实线","虚线"

});

JButton jbt0=new JButton("直线");

JButton jbt1=new JButton("三角形");

JButton jbt2=new JButton("圆");

JButton jbt3=new JButton("矩形");

JPanel jp1=new JPanel();

jp1.add(jlb1);

jp1.add(jcb1);

jp1.add(jlb2);

jp1.add(jcb2);

jp1.add(jlb3);

jp1.add(jcb3);

jp1.add(jbt0);

jp1.add(jbt1);

jp1.add(jbt2);

jp1.add(jbt3);

final DrawingPanel jp2=new DrawingPanel();

jp2.setBackground(Color.white);

jp2.setPreferredSize(new Dimension(736,300));

this.add(Bar);

this.add(jp1,BorderLayout.CENTER);

this.add(jp2,BorderLayout.SOUTH);

jcb1.addItemListener(new ItemListener(){

public void itemStateChanged(ItemEvent e){

if(jcb1.getSelectedItem()=="实线"){

Graphics g=jp2.getGraphics();

g.drawLine(10,10,736,10);

}

if(jcb1.getSelectedItem()=="虚线"){

Graphics g=jp2.getGraphics();

for(int i=1;i<100;i++){

g.drawLine(6*i-6,50,6*i-3,50);

}

}

}

});

jcb2.addItemListener(new ItemListener(){

public void itemStateChanged(ItemEvent e){

if(jcb2.getSelectedItem()==" 20"){

m=20;

repaint();

}

if(jcb2.getSelectedItem()==" 28"){

m=28;repaint();

}

if(jcb2.getSelectedItem()==" 36"){

m=36;repaint();

}

if(jcb2.getSelectedItem()==" 44"){

m=44;repaint();

}

if(jcb2.getSelectedItem()==" 52"){

m=52;repaint();

}

if(jcb2.getSelectedItem()==" 72"){

m=72;repaint();

}

}

});

jcb3.addItemListener(new ItemListener(){

public void itemStateChanged(ItemEvent e){

if(jcb3.getSelectedItem()=="红色"){

color=new Color(255,0,0);

repaint();

}

if(jcb3.getSelectedItem()=="蓝色"){

color=new Color(0,0,255);

repaint();

}

if(jcb3.getSelectedItem()=="黑色"){

color=new Color(0,0,0);

repaint();

}

if(jcb3.getSelectedItem()=="绿色"){

color=new Color(0,128,0);

repaint();

}

}

});

jbt0.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e){

Graphics g=jp2.getGraphics();

g.drawLine(10,10,736,10);

}

});

jbt1.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e){

Graphics grap=jp2.getGraphics();//创建所画组件对象

grap.drawPolygon(xPoint,yPoint,3);

}

});

jbt2.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e){

Graphics gr=jp2.getGraphics();

gr.drawOval(100,10,200,200);

}

});

jbt3.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e){

Graphics G= jp2.getGraphics();

G.drawRect(500,50,200,200);

}

});

}

public static void main(String[] args){

JFrame frame=new DrawingBoard();

frame.setTitle("测试画图板");

frame.setSize(736,379);

frame.setLocation(100,100);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setVisible(true);

}

static class DrawingPanel extends JPanel{

protected void paintComponent(Graphics g){

super.paintComponent(g);

g.setFont(new Font("宋体",Font.BOLD,m));

g.setColor(color);

g.drawString("Heleoeo",100, 100);

}

}

}

有一些问题还没有解决,因为重画,所以字符串可以重画,别的我没有重画,你自己可以修改一下,

还有一个问题,你的菜单条上的功能我暂时不会写,所以没有加上去,

希望对你有帮助,一起努力吧!!!

java画板程序

package draw;

import java.awt.*;

import java.awt.event.*;

import java.awt.geom.*;

import java.io.*;

import java.util.*;

import javax.swing.*;

//the point

//impress the info of one point,the x and y

class OnePoint implements Serializable

{

int x;

int y;

int tool;

Color c;

int border;

public OnePoint(int x,int y,int tool,Color cc,int border)

{

this.x=x;

this.y=y;

this.tool=tool;

this.c=cc;

this.border=border;

}

public static void main(String[] args)

{

DrawingBoard oneBorder=new DrawingBoard();

}

}

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

{

Button pen,line,ellipse,rect,clear,colorboard,storebutton,openbutton;

Choice sizechoice,colorchoice;

Label pensize, pencolor;

Panel panel;

FileDialog storefile, openfile;

FileInputStream filein;

FileOutputStream fileout;

ObjectInputStream objectin;

ObjectOutputStream objectout;

int mode=0;

int flagtool=0;

Color flagcolor;

int border;

BasicStroke size;

private Point2D[] p=new Point2D[3];;

OnePoint p1,p2;

Vector<OnePoint> points=new Vector<OnePoint>();

public DrawingBoard()

{

pen=new Button("画笔");

line=new Button("直线");

ellipse=new Button("圆");

rect=new Button("矩形");

clear=new Button("清除");

colorboard=new Button("调色板");

storebutton=new Button("存储文件");

openbutton=new Button("打开文件");

pensize=new Label("画笔大小");

pencolor=new Label("画笔颜色");

storefile=new FileDialog(this,"存储文件",FileDialog.SAVE);

storefile.setVisible(false);

storefile.addWindowListener(new WindowAdapter(){

public void windowClosing(WindowEvent e){

storefile.setVisible(false);

}

});

openfile=new FileDialog(this,"打开文件",FileDialog.LOAD);

openfile.setVisible(false);

openfile.addWindowListener(new WindowAdapter(){

public void windowClosing(WindowEvent e){

openfile.setVisible(false);

}

});

sizechoice=new Choice();

sizechoice.add("1");

sizechoice.add("2");

sizechoice.add("4");

sizechoice.add("6");

sizechoice.add("8");

sizechoice.addItemListener(this);

colorchoice=new Choice();

colorchoice.add("black");

colorchoice.add("red");

colorchoice.add("blue");

colorchoice.add("green");

colorchoice.addItemListener(this);

pen.addActionListener(this);

line.addActionListener(this);

ellipse.addActionListener(this);

rect.addActionListener(this);

clear.addActionListener(this);

colorboard.addActionListener(this);

storebutton.addActionListener(this);

openbutton.addActionListener(this);

panel=new Panel();

panel.add(storebutton);

panel.add(openbutton);

panel.add(pen);

panel.add(line);

panel.add(ellipse);

panel.add(rect);

panel.add(clear);

panel.add(sizechoice);

panel.add(pensize);

panel.add(colorchoice);

panel.add(pencolor);

panel.add(colorboard);

add(panel,BorderLayout.NORTH);

setBounds(100,100,700,600);

setVisible(true);

addWindowListener(new WindowAdapter(){

public void windowClosing(WindowEvent e){

System.exit(0);

}

});

/**

*添加鼠标事件的监听器,否则,鼠标的移动和点击都将无法识别!

**/

addMouseListener(this);

addMouseMotionListener(this);

}

public void paint(Graphics g)

{

Graphics2D g2d=(Graphics2D)g;

if(flagtool==2)

{//qing chu

g.clearRect(0,0,getSize().width,getSize().height);

}

for(int i=0;i<points.size()-1;i++)

{

p1=(OnePoint)points.elementAt(i);

p2=(OnePoint)points.elementAt(i+1);

g2d.setColor(p1.c);//////////////需要使用Graphics2D从Graphics类中继承下来的方法 setColor()设置当前的颜色

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

g2d.setStroke(size);

if(p1.tool==p2.tool)

{

switch(p1.tool)

{

case 0:

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

g2d.draw(line1);

break;

case 1:

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

g2d.draw(line2);

break;

case 3:

Ellipse2D.Double 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 4:

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

g2d.draw(rect);

break;

default:

}

}

}

}

public void mouseClicked(MouseEvent e){}

public void mouseEntered(MouseEvent e){}

public void mouseExited(MouseEvent e){}

public void mousePressed(MouseEvent e)//鼠标点下时候,将当前的点信息记录

{

mode=0;

p[0]=e.getPoint();

OnePoint pp1=new OnePoint(e.getX(),e.getY(),flagtool,flagcolor,border);

points.addElement(pp1);

//repaint();

}

public void mouseReleased(MouseEvent e)//鼠标松开时候,如果是画笔,则当前截断,是其余状态记下一枚点信息

{

mode=1;

if(flagtool==0)

{

points.addElement(new OnePoint(-1,-1,22,flagcolor,border));

}

else

{

OnePoint pp2=new OnePoint(e.getX(),e.getY(),flagtool,flagcolor,border);

points.addElement(pp2);

points.add(new OnePoint(-1,-1,22,flagcolor,border));

}

repaint();

}

public void itemStateChanged(ItemEvent e)

{

if(e.getSource()==colorchoice)

{

String selected=colorchoice.getSelectedItem();

if(selected=="black"){flagcolor=new Color(0,0,0);}

else if(selected=="red"){flagcolor=new Color(255,0,0);}

else if(selected=="blue"){ flagcolor=new Color(0,0,255);}

else if(selected=="green"){ flagcolor=new Color(0,255,0);}

}

else if(e.getSource()==sizechoice)

{

String selected=sizechoice.getSelectedItem();

if(selected=="1"){ border=1;}

else if(selected=="2"){ border=2*2;}

else if(selected=="4"){ border=4*2;}

else if(selected=="6"){ border=6*2;}

else if(selected=="8"){ border=8*2;}

}

}

/*public void update(Graphics g){//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

paint(g);

}*/

public void actionPerformed(ActionEvent e)

{

// TODO Auto-generated method stub

if(e.getSource()==pen){flagtool=0;}

else if(e.getSource()==line){ flagtool=1;}

else if(e.getSource()==clear)

{

flagtool=2;

points.removeAllElements();

repaint();//此语要有,否则今生无法删除!

}

else if(e.getSource()==ellipse){ flagtool=3;}

else if(e.getSource()==rect){ flagtool=4;}

else if(e.getSource()==colorboard)

{

/*

*使用 javax.swing.×包中的 JColorChooser类的静态方法showDialog(Component component,String title,Color color),

*该方法的参数,component是当前显示对话框的父框架,color是设置调色板初始的被选颜色

*

*该方法返回被选的颜色,类型为Color

**/

Color color=JColorChooser.showDialog(this,"调色板",flagcolor);

flagcolor=color;

}

else if(e.getSource()==openbutton)

{

openfile.setVisible(true);

if(openfile.getFile()!=null)

{

int temp=flagtool;

flagtool=2;

repaint();

try{

points.removeAllElements();

File file=new File(openfile.getDirectory(),openfile.getFile());

filein=new FileInputStream(file);

objectin=new ObjectInputStream(filein);

points=(Vector)objectin.readObject();

objectin.close();

filein.close();

flagtool=temp;

repaint();

}

catch(Exception ee){ System.out.println(ee.toString());}

}

}

else if(e.getSource()==storebutton)

{

storefile.setVisible(true);

if(storefile.getFile()!=null)

{

try{

File file=new File(storefile.getDirectory(),storefile.getFile());

fileout=new FileOutputStream(file);

objectout=new ObjectOutputStream(fileout);

objectout.writeObject(points);

objectout.close();

fileout.close();

repaint();

}

catch(FileNotFoundException e1)

{

System.out.println(e1.toString());

e1.printStackTrace();

} catch(IOException ee){

System.out.println(ee.toString());

ee.printStackTrace();

}

}

}

}

public void mouseDragged(MouseEvent e)//鼠标拖动时候,//当且仅当 flagtool==0,或者表示为橡皮的时候

//才将拖动过程中涉及到的点全部记录下来,并且调用repain()方法,重画当前

// TODO Auto-generated method stub

{

if(flagtool==0)

{

OnePoint pp3=new OnePoint(e.getX(),e.getY(),flagtool,flagcolor,border);

points.addElement(pp3);

repaint();

}

if(flagtool==1)

{

OnePoint pp3=new OnePoint(e.getX(),e.getY(),flagtool,flagcolor,border);

repaint();

}

}

public void mouseMoved(MouseEvent e){

// TODO Auto-generated method stub

}

}

END,本文到此结束,如果可以帮助到大家,还望关注本站哦!

java 流对象是什么(java中的流是什么)java序列化id是什么,java 中的序列化是什么意思