java简易绘图软件代码_java语⾔的科学与艺术-编程练习---
10.5(简单的绘图程序)...
创建⼀个简单的绘图程序,可以绘制矩形,椭圆和直线。
1.先⽣成程序界⾯:
创⼀个添加显⽰图⽚按钮的⽅法:
1 privateJButton addButton(String str){
2 ImageIcon icon = new ImageIcon(str);//按钮图标
3 JButton button = newJButton(icon);
4 button.setContentAreaFilled(false);//按钮透明;
5 returnbutton;
6 }
初始化界⾯:
1 public voidinit(){
2 setLayout(new BorderLayout(5,1)); //设置布局;
3 JButton rectFilled = addButton("rectfilled.gif");
4 rectFilled.setActionCommand("rectFilled");
5 JButton rect =
addButton("rect.gif");6 rect.setActionCommand("rect");7 JButton ovalFilled = addButton("ovalfilled.gif");8
ovalFilled.setActionCommand("ovalFilled");9 JButton oval = addButton("oval.gif");10 oval.setActionCommand("oval");11 JButton line = addButton("line.gif");12 line.setActionCommand("line");13 add(rectFilled, BorderLayout.WEST);14 add(rect, BorderLayout.WEST);15 add(ovalFilled, BorderLayout.WEST);16 add(oval, BorderLayout.WEST);17 add(line,
java爱心代码编程简单BorderLayout.WEST);18 }
2.创建按钮事件
点击按钮分别执⾏的事件;为了点击按钮画出相应的图形,我设置了5个boolean变量;
private boolean isDrawRectFilled = false, isDrawRect = false, isDrawOvalFilled = false, isDrawOval = false, isDrawLine = false;
按钮事件代码:
1 public voidactionPerformed(ActionEvent e){
2 String cmd =e.getActionCommand();
3 if(cmd.equals("rectFilled")){
4 isDrawRectFilled = true;
5 isDrawRect = false;
6 isDrawOval = false;
7 isDrawOvalFilled = false;
8 isDrawLine = false;
9 } else if (cmd.equals("rect")){10 isDrawRect = true;11 isDrawRectFilled = false;12 isDrawOval = false;13 isDrawOvalFilled = false;14 isDrawLine = false;15 } else if (cmd.equals("ovalFilled")){16 isDrawRectFilled = false;17 isDrawRect = false;18 isDrawOval = false;19 isDrawOvalFilled = true;20 isDrawLine = false;21 } else if (cmd.equals("oval")){22 isDrawRectFilled = false;23 isDrawRect = false;24 isDrawOval = true;25 isDrawOvalFilled = false;26 isDrawLine = false;27 } else if (cmd.equals("line")) {28 isDrawRectFilled = false;29 isDrawRect = false;30 isDrawOval = false;31 isDrawOvalFilled = false;32 isDrawLine = true;33 }34 }
分别将所需要画出的图形设为true,将其他设置为false;
3.主要的部分:⿏标点击和拖拽,画出相应的图形:
(1)先定义⼀个GPiont 变量⽤于储存⿏标按下的位置:
private GPoint press; //记录⿏标按下的点;
(2)分别创建储存相应图形的变量;
private GLine line;
private GRect rect;
private GOval oval;
(3)画矩形和画椭圆的代码类似;
分别设置他们的坐标和宽和⾼:
double x = X();
double y = Y();
double width = e.getX() - X();
double height = e.getY() - Y();
同时位置确保在每个⽅向都能画出图形,需要设置
//确保向左下⽅拖拽也能画出图形;
if(width < 0 ){
x = e.getX();
width = -width;
}
//确保向上⽅拖拽也能画出图形;
if(height < 0){
y = e.getY();
height = -height;
}
(4)画直线⽐较简单:
⿏标点击事件中加⼊⼀个:
line = new X(), e.getY(),e.getX(), e.getY());
在⿏标拖拽事件中设置终点:
line.X(),e.getY());
4.同时要允许可以拖动画出的图形:
设置⼀个GObject 变量,储存⿏标按下点的图形,如果不是null则拖动当前图形不是绘画。
1 if(gobj != null){
X() - X(), e.getY() -Y());
3 press = Point());
4 } 5.⿏标点击图形将其移动到最前
if(gobj != null){
gobj.sendToFront();
}
最后的效果:
完整代码:
1 aphics.*;
2 import acm.program.*;3
4 import java.awt.*;
5 import java.awt.event.*;6
7 import acm.gui.*;8 import javax.swing.*;9
10 public class SimpleDraw extendsGraphicsProgram {11 //初始化界⾯
12 public voidinit(){13 setLayout(new BorderLayout(5,1)); //设置布局;
14 JButton rectFilled = addButton("rectfilled.gif");15 rectFilled.setActionCommand("rectFilled");16 JButton rect =
addButton("rect.gif");17 rect.setActionCommand("rect");18 JButton ovalFilled = addButton("ovalfilled.gif");19
ovalFilled.setActionCommand("ovalFilled");20 JButton oval = addButton("oval.gif");21 oval.setActionCommand("oval");22 JButton line = addButton("line.gif");23 line.setActionCommand("li
ne");24 add(rectFilled, BorderLayout.WEST);25 add(rect, BorderLayout.WEST);26 add(ovalFilled, BorderLayout.WEST);27 add(oval, BorderLayout.WEST);28 add(line,
BorderLayout.WEST);29 addActionListeners();30 addMouseListeners();31 }32 /*
33 * 添加按钮⽅法,str 是显⽰的图⽚的路径;34 */
35 privateJButton addButton(String str){36 ImageIcon icon = new ImageIcon(str);//按钮图标
37 JButton button = newJButton(icon);38 button.setContentAreaFilled(false);//按钮透明;
39 returnbutton;40 }41 /*侦听的按钮事件*/
42 public voidactionPerformed(ActionEvent e){43 String cmd =e.getActionCommand();44 if(cmd.equals("rectFilled")){45 isDrawRectFilled = true;46 isDrawRect = false;47 isDrawOval = false;48 isDrawOvalFilled = false;49 isDrawLine = false;50 } else if (cmd.equals("rect")){51 isDrawRect = true;52 isDrawRectFilled = false;53 isDrawOval = false;54 isDrawOvalFilled = false;55 isDrawLine = false;56 } else if (cmd.equals("ovalFilled")){57 isDrawRectFilled = false;58 isDrawRect = false;59 isDrawOval = false;60 isDrawOvalFilled = true;61 isDrawLine = false;62 } else if (cmd.equals("oval")){63 isDrawRectFilled = false;64 isDrawRect = false;65 isDrawOval = true;66 isDr
awOvalFilled = false;67 isDrawLine = false;68 } else if
(cmd.equals("line")){69 isDrawRectFilled = false;70 isDrawRect = false;71 isDrawOval = false;72 isDrawOvalFilled = false;73 isDrawLine = true;74 }75 }76 public voidmousePressed(MouseEvent e){77 press = Point());78 gobj
=getElementAt(press);79 if(gobj != null){80 gobj.sendToFront();81 }82 line = X(), e.getY(),e.getX(),
87 public voidmouseDragged(MouseEvent e){88 double x =X();89 double y =Y();90 double width =
94 if(width < 0){95 x =e.getX();96 width = -width;97 }98 //确保向上⽅拖拽也能画出图形;
99 if(height < 0){100 y =e.getY();101 height = -height;102 }103 rect.setSize(width, height);104 oval.s
etSize(width, height);105 if(gobj != null){X() - X(), e.getY() -Y());107 press =
Point());108 } else{109 if(isDrawRectFilled == true){110 rect.setFilled(true);111 add(rect, x, y);112 }113
if(isDrawRect == true){114 add(rect, x, y);115 }116 if(isDrawOval == true){117 add(oval, x, y);118 }119 if(isDrawOvalFilled == true){120 oval.setFilled(true);121 add(oval, x, y);122 }123 if(isDrawLine == true){124 add(line);125 }126 }127 }128
129 private boolean isDrawRectFilled = false, isDrawRect = false, isDrawOvalFilled = false, isDrawOval = false, isDrawLine = false;130 privateGLine line;131 privateGRect rect;132 privateGOval oval;133 private GPoint press; //记录⿏标按下的点;
134 private GObject gobj; //记录⿏标按下点的图形;
135 }
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论