java几何画板实例源码



java几何画板实例源码。刚学了Java语法基础知识,JFrame的应用。我们小组动手做一个Java几何画板的程序,本次的任务是实现几何画板的功能,能够绘画点、线、圆以及多边形;通过这次任务来理解类、接口的相关概念。初学者实例。

大家从布置作业的当天就开始着手准备,查阅相关视频、网络代码、文档资料,因为要实现GUI功能,我们就提前开了第8章的相关知识点,并且要了解监听器、事件驱动等概念,历时8天,终于初步完成了相关功能,以下是对该程序的相关说明:一下是java几何画板实例的步骤:

首先要设置一个JFrame主面板,设置其大小、标题栏,并在此基础上使其尽可能美观;

新建一个Panel 对象 toolPane 用来添加相关Button、Label,并设置其背景色,使之更加美观

创建了一个“颜色”的标签,并在其上添加“Choice”对象,令其产生下拉菜单,使界面有序、简洁

在页脚创建一个Button用来添加鼠标驱动并显示相关信息,当鼠标点击该按钮时主面板会显示我们小组的成员信息,尽可能用小的面板展示更多的信息

在ShowGraphics构造方法中实现面板的图形布置,程序更加有层次感。主类ShowGraphics继承JFrame并实现MouseMotionListener、MouseListener、ItemListener接口,接下来对其相关抽象方法重写来完成预期功能。

对mousePressed方法的重写实现读取鼠标按下时的位置,值得一提的是刚开始点击button时也会记录当前位置,经过小组讨论,决定添加一条if语句使点击button时不反应来解决该问题。

对mouseDragged方法的重写实现记录鼠标拖拽重点位置的功能,通过起点和终点即可确定一条线和一个圆。

对mouseClicked方法的重写实现点击button时绘制对应图形的功能,只需判断当前点击的是哪个button,再实现该button对应行为即可;需要注意的是画圆所调用的方法:drawOval(x1, y1, x2, y2)是以(x1,y1)、(x2, y2)为两顶点所构成的矩形的内接椭圆,因此要画以(x1,y1)为圆心,(x2,y2)到(x1,y1)的距离为半径时需要做相应处理,我们小组的方法就是令radius为两点之间的距离,之后用drawOval(x1-radius, y1-radius, 2*radius, 2*radius)来实现预期功能

改变颜色这个功能很简单,当点击某个更改颜色的按钮时,令变量col改为相应颜色即可,在每次画图时调用setColor(col);另外我们小组选取了几个相对比较柔和的颜色来作为Choice中的对应项,方便美化图形

程序编写调试期间遇到的最大难题是刚开始只能先点击某个按钮,再拖拽鼠标才能实现画笔功能,后来查阅资料了解到问题在于没有在主面板上添加鼠标驱动、鼠标移动驱动,添加后整个程序就达到了我们组的预期目标,不得不说这是画龙点睛的一笔。

几何画板的缺点与不足:
1. 程序的组织不够合理,程序中画图形部分太过冗杂,如果能够实现点击某个按钮,就调用对应画图函数则会使程序更加清晰明了
2. 画笔功能实现的过程中,可以在边框上作画,这明显是不合理的,但是限于小组能力有限,暂时还不能解决
3. 三角形、五边形、六边形的绘画不知道该怎么用鼠标移动实现,我们选择的方法是以鼠标拖拽的终点为中心,画出一个固定形状的图形,这个功能就很鸡肋
作为一个初学者我的代码是很挫的。。。大神默默看到的话绕过就好微笑

java几何画板代码如下:

/** @author 殷华 */
import java.awt.BorderLayout;
import java.awt.Choice;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Label;
import java.awt.Panel;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JFrame;

@SuppressWarnings(“serial”)//这一句是Eclipse提醒设置的,如果没有这条语句就会有警告,不了解这条语句的功能
public class ShowGraphics extends JFrame implements MouseMotionListener, MouseListener, ItemListener{


//——————————————
Object obj;//定义根类
Panel toolPanel = new Panel();//建立工具面板,把按钮、选择菜单添加到该面板上,最后添加到Frame面板上
JButton drPoint = new JButton(“点”);
JButton drLine = new JButton(“线”);
JButton drCircle = new JButton(“圆”);
JButton drTriangle = new JButton(“三角形”);
JButton drRectangle = new JButton(“四边形”);
JButton drPentagon = new JButton(“五边形”);
JButton drSexangle = new JButton(“六边形”);
JButton drRedHeart = new JButton(“爱心”);
JButton Clear = new JButton(“清空”);
JButton colchooser = new JButton(“调色板”);
JButton Information = new JButton(“~~~~~~~~~~~~~~~~~~【大狼出品,必属精品】~~~~~~~~~~~~~~~~~~”);
Color col = new Color(225, 145, 57);
Choice ColChoice;//颜色选择菜单
Label 颜色;
//—————————————-
/**
* @param drPoint 画点
* @param drLine 画线
* @param drCircle 画圆
* @param drTriangle 画四方形
* @param drPentagon 画五边形
* @param drSexangle 画六边形
* @param drRedHeart 画红心
* @param col 颜色
* */
public ShowGraphics(){//构造方法

//在主面板上添加鼠标监听器、鼠标运动监听器
addMouseMotionListener(this);
addMouseListener(this);

//对窗口进行设置
//对Panel toolPanel背景色进行设置
toolPanel.setBackground(new Color(201,230,224));
//创建颜色下拉项菜单
ColChoice = new Choice();
ColChoice.add(“秋橙色”);
ColChoice.add(“绯红色”);
ColChoice.add(“金盏色”);
ColChoice.add(“品红色”);
ColChoice.addItemListener(this);//添加该监听器,可以捕获其组件的活动
颜色 = new Label(“画笔颜色”,Label.CENTER);//创建标签

//将Button加入toolPanel中
toolPanel.add(drPoint);
toolPanel.add(drLine);
toolPanel.add(drCircle);
toolPanel.add(drTriangle);
toolPanel.add(drRectangle);
toolPanel.add(drPentagon);
toolPanel.add(drSexangle);
toolPanel.add(drRedHeart);
toolPanel.add(颜色); toolPanel.add(ColChoice);
toolPanel.add(colchooser);
toolPanel.add(Clear);
//添加工具面板到主面板
add(toolPanel, BorderLayout.NORTH);
/**
* setSize(1000, 600) 设置主面板大小
* setResizable(false) 设置其窗口大小不可变
* setLocationRelativeTo(null) 设置 frame 位于屏幕正中间位置
* setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) 设置使窗口可以正常退出
*
*
* */
setSize(1000, 600);//设置主面板大小
setVisible(true);
setTitle(“狼狼画板 v1.0″);
setResizable(false);//设置其窗口大小不可变
setLocationRelativeTo(null);//设置 frame 位于屏幕正中间位置
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置使窗口可以正常退出
add(Information, BorderLayout.PAGE_END);
//页脚添加标签

//为Button添加鼠标监听、鼠标移动监听
drPoint.addMouseListener(this);
drPoint.addMouseMotionListener(this);
drLine.addMouseListener(this);
drLine.addMouseMotionListener(this);
drCircle.addMouseListener(this);
drCircle.addMouseMotionListener(this);
drTriangle.addMouseListener(this);
drTriangle.addMouseMotionListener(this);
drRectangle.addMouseListener(this);
drRectangle.addMouseMotionListener(this);
drPentagon.addMouseListener(this);
drPentagon.addMouseMotionListener(this);
drSexangle.addMouseListener(this);
drSexangle.addMouseMotionListener(this);
drRedHeart.addMouseListener(this);
drRedHeart.addMouseMotionListener(this);
Clear.addMouseListener(this);
Clear.addMouseMotionListener(this);
colchooser.addMouseListener(this);
colchooser.addMouseMotionListener(this);
Information.addMouseListener(this);
Information.addMouseMotionListener(this);
}

//—————————————
int x1, y1, x2, y2;
/** 之前点击按钮时就记录坐标,一直卡在这里
* 用于得到鼠标按压处的位置
*/
public void mousePressed(MouseEvent e){

if(e.getSource()!=drPoint && e.getSource()!=drLine && e.getSource()!=drCircle &&
e.getSource()!= drRectangle && e.getSource()!=drPentagon && e.getSource()!=drSexangle
&& e.getSource()!=drRedHeart && e.getSource()!=colchooser && e.getSource()!=Clear){
x1=e.getX();
y1=e.getY();
}
}
/**
* 对mouseClicked() 对该方法重写,读取鼠标点击的位置并实现相关功能
* obj = e.getSource() 得到当前鼠标所点位置
* g.clearRect(5, 85, getWidth()-10, getHeight()-120) 不完全清除边界,使得界面更美观
* g.drawOval(x1-radius, y1-radius, 2*radius, 2*radius) 要注意该方法是以找到椭圆的外接矩形来画椭圆的
* */
public void mouseClicked(MouseEvent e){//对该方法重写,读取鼠标点击的位置并实现相关功能

Graphics g = this.getGraphics();
obj = e.getSource();
if (obj == drPoint) {//在画笔终点处画一个点
g.clearRect(5, 85, getWidth()-10, getHeight()-120);//横、纵坐标没有完全覆盖,保证可以显示边框,更加美观
g.setColor(col);
g.fillArc(x2, y2, 10, 10, 0, 360);
} else if (obj == drLine) {//画一条线
g.clearRect(5, 85, getWidth()-10, getHeight()-120);
g.setColor(col);
g.drawLine(x1, y1, x2, y2);

} else if(obj == drCircle){//画一个圆
g.clearRect(5, 85, getWidth()-10, getHeight()-120);
g.setColor(col);
int radius = (int)Math.pow(Math.pow(x2-x1,2)+Math.pow(y2-y1,2),0.5);
g.drawOval(x1-radius, y1-radius, 2*radius, 2*radius);//要注意该方法是以找到椭圆的外接矩形来画椭圆的

} else if(obj == drTriangle){//以(x2,y2)为中心画一个三角形
g.clearRect(5, 85, getWidth()-10, getHeight()-120);
g.setColor(col);
int x[] = {x2, x2-100, x2+100};
int y[] = {y2-100, y2+100, y2+100};
g.drawPolygon(x, y, 3);

} else if(obj == drRectangle){//以(x1,y1)、(x2,y2)为对角端点画一个四边形
g.clearRect(5, 85, getWidth()-10, getHeight()-120);
g.setColor(col);
g.drawRect(x1, y1, x2-x1, y2-y1);

} else if(obj == drPentagon){//以(x2,y2)画一个五边形
g.clearRect(5, 85, getWidth()-10, getHeight()-120);
g.setColor(col);
int x[] = {x2, x2+100, x2+70, x2-70, x2-100};
int y[] = {y2-100, y2-30, y2+100, y2+100, y2-30};
g.drawPolygon(x, y, 5);

} else if(obj == drSexangle){//以(x1,y2)为中心画一个六边形
g.clearRect(5, 85, getWidth()-10, getHeight()-120);
g.setColor(col);
int x[] = {x2-60, x2+60, x2+160, x2+60, x2-60, x2-160};
int y[] = {y2-100, y2-100, y2, y2+100, y2+100, y2};
g.drawPolygon(x, y, 6);

}else if(obj == drRedHeart){//画一个红心
g.clearRect(5, 85, getWidth()-10, getHeight()-120);
g.setColor(col);
int x[] = {350, 290, 230};
int y[] = {250, 320, 250};
g.fillArc(230, 200, 70, 100, 0, 180);
g.fillArc(280, 200, 70, 100, 0, 180);
g.fillPolygon(x, y, 3);

} else if (obj == Clear) {
//清除所有画出来的图形
g.clearRect(5, 85, getWidth()-10, getHeight()-120);//不完全清除边界,使得界面更美观
paint(g);
}else if(obj == colchooser){//调色板
Color newColor = JColorChooser.showDialog(this,”调色板”,col);//调用调色板并生成新颜色
col = newColor;
}
if(obj == Information){
g.clearRect(5, 85, getWidth()-10, getHeight()-120);
g.setColor(col);
String str[] = {“小组成员:”, “Boss:龚涛”, “程序测试:郝坤坤”,
“客户体验:马亚占”, “界面美化:段自康”, “技术攻关:殷华”};
Font font = new Font(“Default”,Font.PLAIN,20);
g.setFont(font);
for(int i=0; i<6; ++i)
g.drawString(str[i], 400, 200+50*i);
}
}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mouseReleased(MouseEvent e){}
public void mouseDragged(MouseEvent e){//对给方法的重写,用来找到拖拽鼠标的重点,以及实现画笔的功能
x2=e.getX();
y2=e.getY();
Graphics g = getGraphics();
g.setColor(col);
g.fillArc(x2, y2, 5, 5, 0, 360);//画笔的实现是通过在鼠标拖拽的过程中不断画圆做到的
}
public void mouseMoved(MouseEvent e){}
//—————————————
/**
* itemStateChanged() 重写该方法通过鼠标改变组件状态
* */
public void itemStateChanged(ItemEvent e){//重写该方法通过鼠标改变组件状态
if(e.getSource()==ColChoice){//预选颜色
String name = ColChoice.getSelectedItem();

if(name==”秋橙色”){
col = new Color(225, 145, 57);
}
else if(name==”绯红色”){
col = new Color(231, 37, 32);
}
else if(name==”金盏色”){
col = new Color(245, 171, 0);
}
else if(name==”品红色”){
col = new Color(199, 0, 107);
}
}
}
/**
* JFrame.setDefaultLookAndFeelDecorated(true) 用窗口装饰美化窗口
* */
public static void main(String[] args){
JFrame.setDefaultLookAndFeelDecorated(true);//用窗口装饰美化窗口
new ShowGraphics();
}
}