求java版画图程序的源代码
找到了,很久以前写的一个简单画图,呵呵~当时要求用AWT写,很难受。
packagenet.miqiang.gui;
importjava.awt.BasicStroke;
importjava.awt.BorderLayout;
importjava.awt.Button;
importjava.awt.Color;
importjava.awt.Cursor;
importjava.awt.Dimension;
importjava.awt.Frame;
importjava.awt.Graphics;
importjava.awt.Graphics2D;
importjava.awt.GridLayout;
importjava.awt.Label;
importjava.awt.Panel;
importjava.awt.RenderingHints;
importjava.awt.Toolkit;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjava.awt.event.MouseAdapter;
importjava.awt.event.MouseEvent;
importjava.awt.event.MouseListener;
importjava.awt.event.MouseMotionListener;
importjava.awt.event.WindowAdapter;
importjava.awt.event.WindowEvent;
importjava.awt.image.BufferedImage;
*简单画图板程序
*好久没用AWT了,写起来真别扭,如果用swing会很舒服,有空再改写吧。
*@author米强
publicclassTestMainextendsFrame{
privatePalettepalette=null;
//显示当前颜色的面板
privatePanelnonceColor=null;
//画笔粗细
privateLabeldrawWidth=null;
//画笔端点的装饰
privateLabeldrawCap=null;
//选取颜色按钮的监听事件类
privateButtonColorActionbuttonColorAction=null;
//鼠标进入按钮后光标样式的监听事件类
privateButtonCursorbuttonCursor=null;
//画笔样式的监听事件
privateButtonStrokeActionbuttonStrokeAction=null;
*构造方法
publicTestMain(){
//设置标题栏文字
super("简易画图板");
//构造一个画图板
palette=newPalette();
Panelpane=newPanel(newGridLayout(2,1));
//画笔颜色选择器
PanelpaneColor=newPanel(newGridLayout(1,13));
//12个颜色选择按钮
Button[]buttonColor=newButton[12];
Color[]color={Color.black,Color.blue,Color.cyan,Color.darkGray,Color.gray,Color.green,Color.magenta,Color.orange,Color.pink,Color.red,Color.white,Color.yellow};
//显示当前颜色的面板
nonceColor=newPanel();
nonceColor.setBackground(Color.black);
paneColor.add(nonceColor);
buttonColorAction=newButtonColorAction();
buttonCursor=newButtonCursor();
for(inti=0;i
buttonColor[i]=newButton();
buttonColor[i].setBackground(color[i]);
buttonColor[i].addActionListener(buttonColorAction);
buttonColor[i].addMouseListener(buttonCursor);
paneColor.add(buttonColor[i]);
pane.add(paneColor);
//画笔颜色选择器
PanelpaneStroke=newPanel(newGridLayout(1,13));
//控制画笔样式
buttonStrokeAction=newButtonStrokeAction();
Button[]buttonStroke=newButton[11];
buttonStroke[0]=newButton("1");
buttonStroke[1]=newButton("3");
buttonStroke[2]=newButton("5");
buttonStroke[3]=newButton("7");
buttonStroke[4]=newButton("9");
buttonStroke[5]=newButton("11");
buttonStroke[6]=newButton("13");
buttonStroke[7]=newButton("15");
buttonStroke[8]=newButton("17");
buttonStroke[9]=newButton("■");
buttonStroke[10]=newButton("●");
drawWidth=newLabel("3",Label.CENTER);
drawCap=newLabel("●",Label.CENTER);
drawWidth.setBackground(Color.lightGray);
drawCap.setBackground(Color.lightGray);
paneStroke.add(drawWidth);
for(inti=0;i
paneStroke.add(buttonStroke[i]);
buttonStroke[i].addMouseListener(buttonCursor);
buttonStroke[i].addActionListener(buttonStrokeAction);
if(i<=8){
buttonStroke[i].setName("width");
}else{
buttonStroke[i].setName("cap");
if(i==8){
paneStroke.add(drawCap);
pane.add(paneStroke);
//将画笔颜色选择器添加到窗体中
this.add(pane,BorderLayout.NORTH);
//将画图板添加到窗体中
this.add(palette);
//添加窗口监听,点击关闭按钮时退出程序
this.addWindowListener(newWindowAdapter(){
publicvoidwindowClosing(WindowEvente){
System.exit(0);
//设置窗体ICON图标
this.setIconImage(Toolkit.getDefaultToolkit().createImage("images/palette.png"));
//设置窗口的大小
this.setSize(newDimension(400,430));
//设置窗口位置,处于屏幕正中央
this.setLocationRelativeTo(null);
//显示窗口
this.setVisible(true);
*程序入口
*@paramargs
*字符串数组参数
publicstaticvoidmain(String[]args){
newTestMain();
*选取颜色按钮的监听事件类
*@author米强
classButtonColorActionimplementsActionListener{
publicvoidactionPerformed(ActionEvente){
Colorcolor_temp=((Button)e.getSource()).getBackground();
nonceColor.setBackground(color_temp);
palette.setColor(color_temp);
*鼠标进入按钮变换光标样式监听事件类
*@author米强
classButtonCursorextendsMouseAdapter{
publicvoidmouseEntered(MouseEvente){
((Button)e.getSource()).setCursor(newCursor(Cursor.HAND_CURSOR));
publicvoidmouseExited(MouseEvente){
((Button)e.getSource()).setCursor(newCursor(Cursor.DEFAULT_CURSOR));
*设置画笔的监听事件类
*@author米强
classButtonStrokeActionimplementsActionListener{
publicvoidactionPerformed(ActionEvente){
Buttonbutton_temp=(Button)e.getSource();
Stringname=button_temp.getName();
if(name.equalsIgnoreCase("width")){
drawWidth.setText(button_temp.getLabel());
palette.setStroke(Float.parseFloat(button_temp.getLabel()));
}elseif(name.equalsIgnoreCase("cap")){
drawCap.setText(button_temp.getLabel());
if(button_temp.getLabel().equals("■")){
palette.setStroke(BasicStroke.CAP_SQUARE);
}elseif(button_temp.getLabel().equals("●")){
palette.setStroke(BasicStroke.CAP_ROUND);
*@author米强
clasSPAletteextendsPanelimplementsMouseListener,MouseMotionListener{
//鼠标X坐标的位置
privateintmouseX=0;
//上一次X坐标位置
privateintoldMouseX=0;
//鼠标Y坐标的位置
privateintmouseY=0;
//上一次Y坐标位置
privateintoldMouseY=0;
//画图颜色
privateColorcolor=null;
//画笔样式
privateBasicStrokestroke=null;
//缓存图形
privateBufferedImageimage=null;
*构造一个画板类
publicPalette(){
this.addMouseListener(this);
this.addMouseMotionListener(this);
//默认黑色画笔
color=newColor(0,0,0);
//设置默认画笔样式
stroke=newBasicStroke(3.0f,BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND);
//建立1280*1024的RGB缓存图象
image=newBufferedImage(1280,1024,BufferedImage.TYPE_INT_RGB);
//设置颜色
image.getGraphics().setColor(Color.white);
//画背景
image.getGraphics().fillRect(0,0,1280,1024);
*重写paint绘图方法
publicvoidpaint(Graphicsg){
super.paint(g);
//转换为Graphics2D
Graphics2Dg2d=(Graphics2D)g;
//获取缓存图形Graphics2D
Graphics2Dbg=image.createGraphics();
//图形抗锯齿
bg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
//设置绘图颜色
bg.setColor(color);
//设置画笔样式
bg.setStroke(stroke);
//画线,从上一个点到新的点
bg.drawLine(oldMouseX,oldMouseY,mouseX,mouseY);
//将缓存中的图形画到画板上
g2d.drawImage(image,0,0,this);
*重写update方法
publicvoidupdate(Graphicsg){
this.paint(g);
*@returnstroke
publicBasicStrokegetStroke(){
returnstroke;
*@paramstroke要设置的stroke
publicvoidsetStroke(BasicStrokestroke){
this.stroke=stroke;
*设置画笔粗细
*@paramwidth
publicvoidsetStroke(floatwidth){
this.stroke=newBasicStroke(width,stroke.getEndCap(),stroke.getLineJoin());
*设置画笔端点的装饰
*@paramcap参考java.awt.BasicStroke类静态字段
publicvoidsetStroke(intcap){
this.stroke=newBasicStroke(stroke.getLineWidth(),cap,stroke.getLineJoin());
*@returncolor
publicColorgetColor(){
returncolor;
*@paramcolor要设置的color
publicvoidsetColor(Colorcolor){
this.color=color;
publicvoidmouseClicked(MouseEventmouseEvent){
*鼠标按下
publicvoidmousePressed(MouseEventmouseEvent){
this.oldMouseX=this.mouseX=mouseEvent.getX();
this.oldMouseY=this.mouseY=mouseEvent.getY();
repaint();
publicvoidmouseReleased(MouseEventmouseEvent){
*鼠标进入棋盘
publicvoidmouseEntered(MouseEventmouseEvent){
this.setCursor(newCursor(Cursor.CROSSHAIR_CURSOR));
*鼠标退出棋盘
publicvoidmouseExited(MouseEventmouseEvent){
this.setCursor(newCursor(Cursor.DEFAULT_CURSOR));
*鼠标拖动
publicvoidmouseDragged(MouseEventmouseEvent){
this.oldMouseX=this.mouseX;
this.oldMouseY=this.mouseY;
this.mouseX=mouseEvent.getX();
this.mouseY=mouseEvent.getY();
repaint();
publicvoidmouseMoved(MouseEventmouseEvent){
C语言画图程序
如果你用GDK的话,那你也加我吧
关于MFC的一个简单画图程序,
原发布者:guanshuai69MFC简单的绘图程序——王帅目录摘要2关键字21引言2设计目的32功能说明22.1菜单栏32.1.1图形32.1.2画笔42.1.3画硬币42.2工具栏52.3状态栏53功能的实现53.1视图设计53.2编辑资源63.3编程思路及各个函数的实现7(1)思路7(2)代码的实现71.为基本形状创建一个基类82.基本形状类的创建82.1矩形类的创建及定义82.2圆形类的创建及定义102.3正五边形类的创建及定义112.4正三角形类的创建及定义142.5椭圆类的创建及定义162.6正四边形类的创建及的定义172.7正六边形类的创建及定义182.8直线类的创建及定义193.各基本形状类在CMyDraw2_0类中的调用和绘图的实现203.1矩形类的调用与与绘图的实现203.2圆形类的调用243.3正三角形类的调用253.4基本类型调用的剩余代码264.画笔的使用、颜色及大小的调整295.画硬币356.工具栏中的自定义控件387.状态栏中的显示394程序功能的测试415最后总结42摘要:本绘图程序能够进行基本图形的绘画,如直线,圆,矩形,三角形等等,并且在此基础上添加了多边形的绘画,如正五边形等。除此之外