java简易绘图软件代码_java语言的科学与艺术-编程练习---10.5(简单的绘图程序)...

更新时间:2023-07-01 08:46:57 阅读: 评论:0

取名寓意好的字
java简易绘图软件代码_java语⾔的科学与艺术-编程练习---
10.5(简单的绘图程序)...知识科普
创建⼀个简单的绘图程序,可以绘制矩形,椭圆和直线。
1.先⽣成程序界⾯:
创⼀个添加显⽰图⽚按钮的⽅法:
1 privateJButton addButton(String str){
2 ImageIcon icon = new ImageIcon(str);//按钮图标
3 JButton button = newJButton(icon);
4 button.tContentAreaFilled(fal);//按钮透明;
5 returnbutton;
6 }
初始化界⾯:
1 public voidinit(){
2 tLayout(new BorderLayout(5,1)); //设置布局;
3 JButton rectFilled = addButton("rectfilled.gif");
4 rectFilled.tActionCommand("rectFilled");
5 JButton rect =
addButton("rect.gif");6 rect.tActionCommand("rect");7 JButton ovalFilled = addButton("ovalfilled.gif");8
ovalFilled.tActionCommand("ovalFilled");9 JButton oval = addButton("oval.gif");10 oval.tActionCommand("oval");11 JButton line = addButton("line.gif");12 line.tActionCommand("line");13 add(rectFilled, BorderLayout.WEST);14 add(rect, BorderLayout.WEST);15 add(ovalFilled, BorderLayout.WEST);16 add(oval, BorderLayout.WEST);17 add(line,
BorderLayout.WEST);18 }
2.创建按钮事件
点击按钮分别执⾏的事件;为了点击按钮画出相应的图形,我设置了5个boolean变量;
private boolean isDrawRectFilled = fal, isDrawRect = fal, isDrawOvalFilled = fal, isDrawOval = fal, isDrawLine = fal;
按钮事件代码:
1 public voidactionPerformed(ActionEvent e){
2 String cmd =e.getActionCommand();
3 if(cmd.equals("rectFilled")){
4 isDrawRectFilled = true;
5 isDrawRect = fal;坏蛋冰激凌
6 isDrawOval = fal;
7 isDrawOvalFilled = fal;
8 isDrawLine = fal;
9 } el if (cmd.equals("rect")){10 isDrawRect = true;11 isDrawRectFilled = fal;12 isDrawOval = fal;13 isDrawOvalFilled = fal;14 isDrawLine = fal;15 } el if (cmd.equals("ovalFilled")){16 isDrawRectFilled = fal;17 isDrawRect = fal;18 isDrawOval = fal;19 isDrawOvalFilled = true;20 isDrawLine = fal;21 } el if (cmd.equals("oval")){22 isDrawRectFilled = fal;23 isDrawRect = fal;24 isDrawOval = true;25 isDrawOvalFilled = fal;26 isDrawLine = fal;27 } el if (cmd.equals("line")) {28 isDrawRectFilled = fal;29 isDrawRect = fal;30 isDrawOval = fal;31 isDrawOvalFilled = fal;32 isDrawLine = true;33 }34 }
形容声音大
分别将所需要画出的图形设为true,将其他设置为fal;
3.主要的部分:⿏标点击和拖拽,画出相应的图形:
(1)先定义⼀个GPiont 变量⽤于储存⿏标按下的位置:
private GPoint press; //记录⿏标按下的点;
(2)分别创建储存相应图形的变量;
private GLine line;
private GRect rect;
private GOval oval;
(3)画矩形和画椭圆的代码类似;
分别设置他们的坐标和宽和⾼:
double x = X();
double y = Y();
诺基亚5210double 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.ndToFront();
}
最后的效果:
完整代码:
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 tLayout(new BorderLayout(5,1)); //设置布局;化妆品过期
14 JButton rectFilled = addButton("rectfilled.gif");15 rectFilled.tActionCommand("rectFilled");16 JButton rect =
addButton("rect.gif");17 rect.tActionCommand("rect");18 JButton ovalFilled = addButton("ovalfilled.gif");19
ovalFilled.tActionCommand("ovalFilled");20 JButton oval = addButton("oval.gif");21 oval.tActionCommand("oval");22 JButton line = addButton("line.gif");23 line.tActionCommand("liabcc的四字词语
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 addMouListeners();31 }32 /*
33 * 添加按钮⽅法,str 是显⽰的图⽚的路径;34 */
35 privateJButton addButton(String str){36 ImageIcon icon = new ImageIcon(str);//按钮图标
37 JButton button = newJButton(icon);38 button.tContentAreaFilled(fal);//按钮透明;
39 returnbutton;40 }41 /*侦听的按钮事件*/
42 public voidactionPerformed(ActionEvent e){43 String cmd =e.getActionCommand();44 if(cmd.equals("rectFilled")){45 isDrawRectFilled = true;46 isDrawRect = fal;47 isDrawOval = fal;48 isDrawOvalFilled = fal;49 isDrawLine = fal;50 } el if (cmd.equals("rect")){51 isDrawRect = true;52 isDrawRectFilled = fal;53 isDrawOval = fal;54 isDrawOvalFilled = fal;55 isDrawLine = fal;56 } el if (cmd.equals("ovalFilled")){57 isDrawRectFilled = fal;58 isDrawRect = fal;59 isDrawOval = fal;60 isDrawOvalFilled = true;61 isDrawLine = fal;62 } el if (cmd.equals("oval")){63 isDrawRectFilled = fal;64 isDrawRect = fal;65 isDrawOval = true;66 isD
rawOvalFilled = fal;67 isDrawLine = fal;68 } el if
(cmd.equals("line")){69 isDrawRectFilled = fal;70 isDrawRect = fal;71 isDrawOval = fal;72 isDrawOvalFilled = fal;73 isDrawLine = true;74 }75 }76 public voidmouPresd(MouEvent e){77 press = Point());78 gobj
=getElementAt(press);79 if(gobj != null){80 gobj.ndToFront();81 }82 line = X(), e.getY(),e.getX(),
87 public voidmouDragged(MouEvent 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.tSize(width, height);104 oval.s
etSize(width, height);105 if(gobj != null){X() - X(), e.getY() -Y());107 press =
Point());108 } el{109 if(isDrawRectFilled == true){110 rect.tFilled(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.tFilled(true);121 add(oval, x, y);122 }123 if(isDrawLine == true){124 add(line);125 }126 }127 }128
129 private boolean isDrawRectFilled = fal, isDrawRect = fal, isDrawOvalFilled = fal, isDrawOval = fal, isDrawLine = fal;130 privateGLine line;131 privateGRect rect;132 privateGOval oval;133 private GPoint press; //记录⿏标按下的点;
134 private GObject gobj; //记录⿏标按下点的图形;
135 }

本文发布于:2023-07-01 08:46:57,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/82/1071831.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:图形   按钮   事件   标按   点击   设置   储存   椭圆
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图