JAVA实现简单的画图板

JAVA实现简单的画图板,第1张

楼主写一个html,很容易把下面代码嵌入到applet,可以google一下实现,
还有copy自己不知道算不算复制。。。-_-!
--------------------------------------------------------------------
楼主给你一个我的,直接保存成pbjava编译运行,就是你要的画图功能 ,可以参考一下
____________________________________________________________________
import javaapplet;
import javaawt;
import javaawtevent;
import javautil;
import javaxswing;
import javaawtgeom;
import javaio;
class Point implements Serializable
{
int x,y;
Color col;
int tool;
int boarder;
Point(int x, int y, Color col, int tool, int boarder)
{
thisx = x;
thisy = y;
thiscol = col;
thistool = tool;
thisboarder = 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,BasicStrokeCAP_BUTT,BasicStrokeJOIN_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();
ColChoiceadd("black");
ColChoiceadd("red");
ColChoiceadd("blue");
ColChoiceadd("green");
ColChoiceaddItemListener(this);
//画笔大小选择
SizeChoice = new Choice();
SizeChoiceadd("1");
SizeChoiceadd("3");
SizeChoiceadd("5");
SizeChoiceadd("7");
SizeChoiceadd("9");
SizeChoiceaddItemListener(this);
//橡皮大小选择
EraserChoice = new Choice();
EraserChoiceadd("5");
EraserChoiceadd("9");
EraserChoiceadd("13");
EraserChoiceadd("17");
EraserChoiceaddItemListener(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("显示调色板");
//各组件事件监听
clearaddActionListener(this);
eraseraddActionListener(this);
penaddActionListener(this);
drLineaddActionListener(this);
drCircleaddActionListener(this);
drRectaddActionListener(this);
openPicaddActionListener(this);
savePicaddActionListener(this);
colchooseraddActionListener(this);
颜色 = new Label("画笔颜色",LabelCENTER);
大小B = new Label("画笔大小",LabelCENTER);
大小E = new Label("橡皮大小",LabelCENTER);
//面板添加组件
toolPaneladd(openPic);
toolPaneladd(savePic);
toolPaneladd(pen);
toolPaneladd(drLine);
toolPaneladd(drCircle);
toolPaneladd(drRect);
toolPaneladd(颜色); toolPaneladd(ColChoice);
toolPaneladd(大小B); toolPaneladd(SizeChoice);
toolPaneladd(colchooser);
toolPaneladd(eraser);
toolPaneladd(大小E); toolPaneladd(EraserChoice);
toolPaneladd(clear);
//工具面板到APPLET面板
add(toolPanel,BorderLayoutNORTH);
setBounds(60,60,900,600); setVisible(true);
validate();
//dialog for save and load
openPicture = new FileDialog(this,"打开图画",FileDialogLOAD);
openPicturesetVisible(false);
savePicture = new FileDialog(this,"保存图画",FileDialogSAVE);
savePicturesetVisible(false);
openPictureaddWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{ openPicturesetVisible(false); }
});
savePictureaddWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{ savePicturesetVisible(false); }
});
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{ Systemexit(0);}
});
}
public void paint(Graphics g)
{
Graphics2D g2d = (Graphics2D)g;
Point p1,p2;
n = paintInfosize();
if(toolFlag==2)
gclearRect(0,0,getSize()width,getSize()height);//清除
for(int i=0; i<n ;i++){
p1 = (Point)paintInfoelementAt(i);
p2 = (Point)paintInfoelementAt(i+1);
size = new BasicStroke(p1boarder,BasicStrokeCAP_BUTT,BasicStrokeJOIN_BEVEL);
g2dsetColor(p1col);
g2dsetStroke(size);
if(p1tool==p2tool)
{
switch(p1tool)
{
case 0://画笔
Line2D line1 = new Line2DDouble(p1x, p1y, p2x, p2y);
g2ddraw(line1);
break;
case 1://橡皮
gclearRect(p1x, p1y, p1boarder, p1boarder);
break;
case 3://画直线
Line2D line2 = new Line2DDouble(p1x, p1y, p2x, p2y);
g2ddraw(line2);
break;
case 4://画圆
Ellipse2D ellipse = new Ellipse2DDouble(p1x, p1y, Mathabs(p2x-p1x) , Mathabs(p2y-p1y));
g2ddraw(ellipse);
break;
case 5://画矩形
Rectangle2D rect = new Rectangle2DDouble(p1x, p1y, Mathabs(p2x-p1x) , Mathabs(p2y-p1y));
g2ddraw(rect);
break;
case 6://截断,跳过
i=i+1;
break;
default :
}//end switch
}//end if
}//end for
}
public void itemStateChanged(ItemEvent e)
{
if(egetSource()==ColChoice)//预选颜色
{
String name = ColChoicegetSelectedItem();
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(egetSource()==SizeChoice)//画笔大小
{
String selected = SizeChoicegetSelectedItem();
if(selected=="1")
{
con = 1;
size = new BasicStroke(con,BasicStrokeCAP_BUTT,BasicStrokeJOIN_BEVEL);
}
else if(selected=="3")
{
con = 3;
size = new BasicStroke(con,BasicStrokeCAP_BUTT,BasicStrokeJOIN_BEVEL);
}
else if(selected=="5")
{con = 5;
size = new BasicStroke(con,BasicStrokeCAP_BUTT,BasicStrokeJOIN_BEVEL);
}
else if(selected=="7")
{con = 7;
size = new BasicStroke(con,BasicStrokeCAP_BUTT,BasicStrokeJOIN_BEVEL);
}
else if(selected=="9")
{con = 9;
size = new BasicStroke(con,BasicStrokeCAP_BUTT,BasicStrokeJOIN_BEVEL);
}
}
else if(egetSource()==EraserChoice)//橡皮大小
{
String Esize = EraserChoicegetSelectedItem();
if(Esize=="5")
{ Econ = 52; }
else if(Esize=="9")
{ Econ = 92; }
else if(Esize=="13")
{ Econ = 132; }
else if(Esize=="17")
{ Econ = 173; }
}
}
public void mouseDragged(MouseEvent e)
{
Point p1 ;
switch(toolFlag){
case 0://画笔
x = (int)egetX();
y = (int)egetY();
p1 = new Point(x, y, c, toolFlag, con);
paintInfoaddElement(p1);
repaint();
break;
case 1://橡皮
x = (int)egetX();
y = (int)egetY();
p1 = new Point(x, y, null, toolFlag, Econ);
paintInfoaddElement(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)egetX();
y = (int)egetY();
p2 = new Point(x, y, c, toolFlag, con);
paintInfoaddElement(p2);
break;
case 4: //圆
x = (int)egetX();
y = (int)egetY();
p2 = new Point(x, y, c, toolFlag, con);
paintInfoaddElement(p2);
break;
case 5: //矩形
x = (int)egetX();
y = (int)egetY();
p2 = new Point(x, y, c, toolFlag, con);
paintInfoaddElement(p2);
break;
default :
}
}
public void mouseReleased(MouseEvent e)
{
Point p3;
switch(toolFlag){
case 0://画笔
paintInfoaddElement(cutflag);
break;
case 1: //eraser
paintInfoaddElement(cutflag);
break;
case 3://直线
x = (int)egetX();
y = (int)egetY();
p3 = new Point(x, y, c, toolFlag, con);
paintInfoaddElement(p3);
paintInfoaddElement(cutflag);
repaint();
break;
case 4: //圆
x = (int)egetX();
y = (int)egetY();
p3 = new Point(x, y, c, toolFlag, con);
paintInfoaddElement(p3);
paintInfoaddElement(cutflag);
repaint();
break;
case 5: //矩形
x = (int)egetX();
y = (int)egetY();
p3 = new Point(x, y, c, toolFlag, con);
paintInfoaddElement(p3);
paintInfoaddElement(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(egetSource()==pen)//画笔
{toolFlag = 0;}
if(egetSource()==eraser)//橡皮
{toolFlag = 1;}
if(egetSource()==clear)//清除
{
toolFlag = 2;
paintInforemoveAllElements();
repaint();
}
if(egetSource()==drLine)//画线
{toolFlag = 3;}
if(egetSource()==drCircle)//画圆
{toolFlag = 4;}
if(egetSource()==drRect)//画矩形
{toolFlag = 5;}
if(egetSource()==colchooser)//调色板
{
Color newColor = JColorChoosershowDialog(this,"调色板",c);
c = newColor;
}
if(egetSource()==openPic)//打开图画
{
openPicturesetVisible(true);
if(openPicturegetFile()!=null)
{
int tempflag;
tempflag = toolFlag;
toolFlag = 2 ;
repaint();
try{
paintInforemoveAllElements();
File filein = new File(openPicturegetDirectory(),openPicturegetFile());
picIn = new FileInputStream(filein);
VIn = new ObjectInputStream(picIn);
paintInfo = (Vector)VInreadObject();
VInclose();
repaint();
toolFlag = tempflag;
}
catch(ClassNotFoundException IOe2)
{
repaint();
toolFlag = tempflag;
Systemoutprintln("can not read object");
}
catch(IOException IOe)
{
repaint();
toolFlag = tempflag;
Systemoutprintln("can not read file");
}
}
}
if(egetSource()==savePic)//保存图画
{
savePicturesetVisible(true);
try{
File fileout = new File(savePicturegetDirectory(),savePicturegetFile());
picOut = new FileOutputStream(fileout);
VOut = new ObjectOutputStream(picOut);
VOutwriteObject(paintInfo);
VOutclose();
}
catch(IOException IOe)
{
Systemoutprintln("can not write object");
}
}
}
}//end paintboard
public class pb
{
public static void main(String args[])
{ new paintboard("画图程序"); }
}

Graphics类提供基本绘图方法,Graphics2D类提供更强大的绘图能力。本节讲解Graphics类,下节讲解Graphics2D。
Graphics类提供基本的几何图形绘制方法,主要有:画线段、画矩形、画圆、画带颜色的图形、画椭圆、画圆弧、画多边形等。
1 画线
在窗口画一条线段,可以使用Graphics类的drawLine()方法:
drawLine(int x1,int y1,int x2,int y2)
例如,以下代码在点(3,3)与点(50,50)之间画线段,在点(100,100)处画一个点。
gdrawLine(3,3,50,50);//画一条线段
gdrawLine(100,100,100,100);//画一个点。
2 画矩形
有两种矩形:普通型和圆角型。
(1) 画普通矩形有两个方法:
drawRect(int x,int y,int width,int height):画线框围起来的矩形。其中参数x和y指定左上角的位置,参数width和height是矩形的宽和高。
fillRect(int x,int y,int width,int height):是用预定的颜色填充一个矩形,得到一个着色的矩形块。
以下代码是画矩形的例子:
gdrawRect(80,100,40,25);//画线框
gsetColor(Coloryellow);gfillRect(20,70,20,30);//画着色块
(2)画圆角矩形也有两个方法:
drawRoundRect(int x,int y,int width, int height, int arcWidth, int arcHeight):是用线围起来的圆角矩形。其中参数x和y指定矩形左上角的位置;参数width和heigth是矩形的宽和高;arcWidth和arcHeight分别是圆角弧的横向直径和圆角弧的纵向直径。
fillRoundRect(int x,int y,int width,int height,int arcWidth,int archeight):是用预定的颜色填充的圆角矩形。各参数的意义同前一个方法。
以下代码是画矩形的例子:
gdrawRoundRect(10,10,150,70,40,25);//画一个圆角矩形
gsetColor(Colorblue); gfillRoundRect(80,100,100,100,60,40);//涂一个圆角矩形块
gdrawRoundRect(10,150,40,40,40,40);//画圆
gsetColor(Colorred); gfillRoundRect(80,100,100,100,100,100);//画圆块
可以用画圆角矩形方法画圆形,当矩形的宽和高相等,圆角弧的横向直径和圆角弧的纵向直径也相等,并等于矩形的宽和高时,画的就是圆形。参见上述例子中的注释,前一个是画圆,后一个是涂圆块。
3 画三维矩形
画三维矩形有两个方法:
draw3DRect(int x,int y,int width,int height, boolean raised):画一个突出显示的矩形。其中x和y指定矩形左上角的位置,参数width和height是矩形的宽和高,参数raised是突出与否。
fill3DRect(int x,int y,int width,int height,boolean raised):用预定的颜色填充一个突出显示的矩形。
以下代码是画突出矩形的例子:
gdraw3DRect(80,100,40,25,true);//画一个线框
gsetColor(Coloryellow); gfill3DRect(20,70,20,30,true);//画一个着色块
4画椭圆形
椭圆形由椭圆的横轴和纵轴确定。画椭圆形有两个方法:
drawOval(int x,int y,int width,int height):是画用线围成的椭圆形。其中参数x和参数y指定椭圆形左上角的位置,参数width和height是横轴和纵轴。
fillOval(int x,int y,int width,int height):是用预定的颜色填充的椭圆形,是一个着色块。也可以用画椭圆形方法画圆形,当横轴和纵轴相等时,所画的椭圆形即为圆形。
以下代码是画椭圆形的例子:
gdrawOval(10,10,60,120);//画椭圆
gsetColor(Colorcyan);gfillOval(100,30,60,60);//涂圆块
gsetColor(Colormagenta);gfillOval(15,140,100,50);//涂椭圆
5 画圆弧
画圆弧有两个方法:
drawArc(int x,int y,int width,int height,int startAngle, int arcAngle):画椭圆一部分的圆弧线。椭圆的中心是它的外接矩形的中心,其中参数是外接矩形的左上角坐标(x,y),宽是width,高是heigh。参数startAngle的单位是 “度”,起始角度0度是指3点钟方位参数startAngle和arcAngle表示从startAngle角度开始,逆时针方向画arcAngle度的弧,约定,正值度数是逆时针方向,负值度数是顺时针方向,例如-90度是6点钟方位。
fillArc(int x,int y,int width, int height, int startAngle, int arcAngle):用setColor()方法设定的颜色,画着色椭圆的一部分。
以下代码是画圆弧的例子:
gdrawArc(10,40,90,50,0,180);//画圆弧线
gdrawArc(100,40,90,50,180,180);//画圆弧线
gsetColor(Coloryellow); gfillArc(10,100,40,40,0,-270);//填充缺右上角的四分之三的椭圆
gsetColor(Colorgreen); gfillArc(60,110,110,60,-90,-270);//填充缺左下角的四分之三的椭圆
6 画多边形
多边形是用多条线段首尾连接而成的封闭平面图。多边形线段端点的x坐标和y坐标分别存储在两个数组中,画多边形就是按给定的坐标点顺序用直线段将它们连起来。以下是画多边形常用的两个方法:
drawPolygon(int xpoints[],int yPoints[],int nPoints):画一个多边形
fillPolygon(int xPoints[],int yPoints[],int nPoints):用方法setColor()设定的颜色着色多边形。其中数组xPoints[]存储x坐标点,yPoints[]存储y坐标点,nPoints是坐标点个数。
注意,上述方法并不自动闭合多边形,要画一个闭合的多边形,给出的坐标点的最后一点必须与第一点相同以下代码实现填充一个三角形和画一个八边形。
int px1[]={50,90,10,50};//首末点相重,才能画多边形
int py1[]={10,50,50,10};
int px2[]={140,180,170,180,140,100,110,140};
int py2[]={5,25,35,45,65,35,25,5};
gsetColor(Colorblue);
gfillPolygon(px1,py1,4);
gsetColor(Colorred);
gdrawPolygon(px2,py2,9);
也可以用多边形对象画多边形。用多边形类Polygon创建一个多边形对象,然后用这个对象绘制多边形。Polygon类的主要方法:
Polygon():创建多边形对象,暂时没有坐标点。
Polygon(int xPoints[],int yPoints[],int nPoints):用指定的坐标点创建多边形对象。
addPoint():将一个坐标点加入到Polygon对象中。
drawPolygon(Polygon p):绘制多边形。
fillPolygon(Polygon p):和指定的颜色填充多边形。
例如,以下代码,画一个三角形和填充一个的三角形。注意,用多边形对象画封闭多边形不要求首末点重合。
int x[]={140,180,170,180,140,100,110,100};
int y[]={5,25,35,45,65,45,35,25};
Polygon ponlygon1=new Polygon();
polygon1addPoint(50,10);
polygon1addPoint(90,50);
polygon1addPoint(10,50);
gdrawPolygon(polygon1);
gsetColor(Coloryellow);
Polygon polygon2 = new Polygon(x,y,8);
gfillPolygon(polygon2);
7 擦除矩形块
当需要在一个着色图形的中间有一个空缺的矩形的情况,可用背景色填充一矩形块实现,相当于在该矩形块上使用了 “橡皮擦”实现的方法是:
clearRect(int x,int y, int width,int height):擦除一个由参数指定的矩形块的着色。
例如,以下代码实现在一个圆中擦除一个矩形块的着色:
gsetColor(Colorblue);
gfillOval(50,50,100,100);gclearRect(70,70,40,55);
8 限定作图显示区域
用一个矩形表示图形的显示区域,要求图形在指定的范围内有效,不重新计算新的坐标值,自动实现超出部分不显示。方法是clipRect(int x,int y,int width,int height),限制图形在指定区域内的显示,超出部分不显示。多个限制区有覆盖时,得到限制区域的交集区域。例如,代码:
gclipRect(0,0,100,50);gclipRect(50,25,100,50);
相当于
gclipRect(50,25,50,25);
9 复制图形
利用Graphics类的方法copyArea()可以实现图形的复制,其使用格式是:
copyArea(int x,int y,int width,int height, int dx, int dy),dx和dy分别表示将图形粘贴到原位置偏移的像素点数,正值为往右或往下偏移是,负值为往左或往上偏移量。位移的参考点是要复制矩形的左上角坐标。
例如,以下代码示意图形的复制,将一个矩形的一部分、另一个矩形的全部分别自制。
gdrawRect(10,10,60,90);
gfillRect(90,10,60,90);
gcopyArea(40,50,60,70,-20,80);
gcopyArea(110,50,60,60,10,80);

画椭圆的方法如下:

画椭圆只需使用一根绳子(无d性)、一支笔以及两个图钉,具体 *** 作如下:

如图所示,如细钢丝绳,两端固定在钉子上,用划线笔撑直绳子,笔与绳之间是滑动的,这样转圈画出的就是一个椭圆。

确定两个固定钉子的距离和绳的长度,就可以画出任意长轴或任意短轴的椭圆(“任意”只是相对而言,因为现实画法会存在误差)。

根据椭圆长宽尺寸(长轴AB与短轴CD大小,如下图),可求出两个钉子之间的距离和绳长。同理已知两个钉子之间的距离和绳长,也可求出椭圆长宽尺寸。具体如下:

设长轴AB=2a,短轴CD=2b,两个钉子之间距离为2c,那么a,b,c三者满足关系:

c²=a² - b²,绳子长度就是2a。

在数学中,椭圆是围绕两个焦点的平面中的曲线,使得对于曲线上的每个点,到两个焦点的距离之和是恒定的。因此,它是圆的概括,其是具有两个焦点在相同位置处的特殊类型的椭圆。

椭圆是封闭式圆锥截面:由锥体与平面相交的平面曲线。椭圆与其他两种形式的圆锥截面有很多相似之处:抛物线和双曲线,两者都是开放的和无界的。圆柱体的横截面为椭圆形,除非该截面垂直于圆柱体轴线。


欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/yw/13342753.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-07-18
下一篇 2023-07-18

发表评论

登录后才能评论

评论列表(0条)

保存