画图板要“自己写”+近期学习JAVA的感受分享
<!--[if !supportLists]-->一. <!--[endif]-->开篇废话
经常看where的人会知道,开篇废话这个环节会经常出现的,我想以后也会继续存在,抱着单纯看技术博心态看的读者可以忽略这一块,这一块仅与懂生活的读者分享。从7月17日开始接触JAVA到现在已近一周有余了这段时间主要是学习了JAVA的基础知识,这个阶段是每个入门者都会经历的,动手的机会不多,这说说的动手时自己敲代码,把自己的想法通过代码的方式来实现这么一个过程。这一段主要是学习基础知识并写总结,在这两件事情上where个人本来觉得这阶段做的还可以。今天下午的画图板宣告了这一个阶段学习方式的一个终结,下一个阶段应该是动手与知识并重,甚至动手重于理解。
下午的画图板和左哥的一番话让我意识到这一点,在这感谢一下左哥,(PS:本人师从左哥)下面就是今天晚上6点到9点半,“where”的画图板(略显粗糙,会有后续跟进)从无到有的一个过程,就整个思路与大家全方位无保留做一个分享权当左哥要求的写两遍代码的第二次,对我自己来说也算是个加深印象。闲话少叙,进入正题。(当然思路也可能不妥,望大家多不吝批评指正)
<!--[if !supportLists]-->二. <!--[endif]-->画图板要“自己写”
好了,假如我们现在要做一个项目——实现一个简单画图版,从大的思路将完成以下几步就行:
1.实现窗体
2.添加监听器
3.添加按钮实现功能选择
有了这个总的思路我们来进一步做细化工作,依次将几步实现。
<!--[if !supportLists]-->1) <!--[endif]-->实现窗体
这个很简单,分这么几小步:创建窗体对象,设置窗体,显示窗体。当然还可一细分,我们就不细说了,看代码就懂了。
代码:public class Drawboard extends JFrame{
public static void main(String[] args){
Drawboard d = new Drawboard();
d.showUI();
}
public void showUI(){
//设置标题
this.setTitle("画图板");
//设置窗体大小
this.setSize(600,600);
//设置关闭窗口程序也关闭
this.setDefaultCloseOperation(3);
//设置窗体大小不可更改
this.setResizable(false);
//设置显示窗体
this.setVisible(true);
}
到这为止我们第一步工作完成,程序实现图见图一。
2)添加监听器
具体过程:1.创建一个类继承监听器并实现抽象方法
2.具体写我们要用到的方法的方法体,分析一下不难知道我们要用的方法只有mousePressed和mouseReleased,在鼠标按下时我们要一个坐标作为起点坐标,松开鼠标还要一个坐标,并且所有图形都要松开后才出现所以画图形的方法得写在松开鼠标的方法里。在此我选择先实现画直线,两点确定一天线嘛。以此测试能不能实现,如果能那添加别的画图形方法也一样的道理。
3.把这个鼠标监听器加到刚才创建的窗体对象上去。
4.测试通过后将所有画图形的方法加入mouseReleased
看代码:
public class Drawboard extends JFrame{
public static void main(String[] args){
Drawboard d = new Drawboard();
d.showUI();
}
public void showUI(){
//设置标题
this.setTitle("画图板");
//设置窗体大小
this.setSize(600,600);
//设置关闭窗口程序也关闭
this.setDefaultCloseOperation(3);
//设置窗体大小不可更改
this.setResizable(false);
//设置显示窗体
this.setVisible(true);
//创建鼠标监听器对象
mouselistener lis = new mouselistener();
this.addMouseListener(lis);
}
public class mouselistener implements MouseListener
{ /鼠标按键在组件上按下时调用
public void mousePressed(MouseEvent e){
x1=e.getX();
y1=e.getY();
}
//鼠标按钮在组件上释放时调用。
public void mouseReleased(MouseEvent e){
x2=e.getX();
y2=e.getY();
//画直线
g.drawLine(x1, y1, x2, y2);
//绘制矩形
g.drawRect(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x2-x1), Math.abs(y2-y1));
//绘制椭圆
g.drawOval(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x2-x1), Math.abs(y2-y1));
//填充矩形
g.fillRect(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x2-x1), Math.abs(y2-y1));
//圆角矩形
g.drawRoundRect(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x2-x1), Math.abs(y2-y1),12,12);
//填充椭圆
g.fillOval(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x2-x1), Math.abs(y2-y1));
//画圆弧
g.drawArc(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x2-x1), Math.abs(y2-y1),45,90);
}
public void mouseClicked(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
}
这样我们就完成了第二步,具体效果图看图二。
<!--[if !supportLists]-->三. <!--[endif]-->添加按钮实现功能选择
在这步里我们考虑的较多可以分为这么几步:
<!--[if !supportLists]-->1) <!--[endif]-->首先在窗体上加JRadioButton,注意这里是加JRadioButton而不是JButton,加的步骤前几篇有说道不赘述,代码也会体现。
<!--[if !supportLists]-->2) <!--[endif]-->用Group对象放JRadioButton对象从而实现单选
<!--[if !supportLists]-->3) <!--[endif]-->我们现在要做的是想要实现选中相应的图形JRadioButton就可以实现画出相应的图形。
这一步实现要经过:对每个按钮setActionCommand,然后通过监听器对象将命令传给接听器,监听器对命令字符串判断然后选择方法。具体过程见代码
<!--[if !supportLists]-->4) <!--[endif]-->以上弄完以后还有一个问题,画所有形状可以再整个窗体上,包括单选按钮上,这样当然不好,我们希望在一个指定板块画,所以建面板。
最终代码:import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.*;
public class Drawboard extends JFrame{
Graphics g;
public static void main(String[] args){
Drawboard d = new Drawboard();
d.showUI();
}
public void showUI(){
Graphics g ;
this.setTitle("画图板");
this.setSize(600,600);
this.setDefaultCloseOperation(3);
this.setResizable(false);
java.awt.FlowLayout fl = new java.awt.FlowLayout();
this.setLayout(fl);
JPanel p = new JPanel();
this.add(p);
// 创建一个面板
p.setPreferredSize(new Dimension(550,400));
// 设置面板颜色
p.setBackground(Color.green);
JRadioButton jr1 =new JRadioButton("直线");
jr1.setActionCommand("直线");
JRadioButton jr2 =new JRadioButton("矩形");
jr2.setActionCommand("矩形");
JRadioButton jr3 =new JRadioButton("圆角矩形");
jr3.setActionCommand("圆角矩形");
JRadioButton jr4 =new JRadioButton("椭圆");
jr4.setActionCommand("椭圆");
JRadioButton jr5 =new JRadioButton("填充矩形");
jr5.setActionCommand("填充矩形");
JRadioButton jr6 =new JRadioButton("填充椭圆");
jr6.setActionCommand("填充椭圆");
JRadioButton jr7 =new JRadioButton("圆弧");
jr7.setActionCommand("圆弧");
//创建一个group(这是个容器),并把所有单选对象放进去
ButtonGroup group = new ButtonGroup();
group.add(jr1);
group.add(jr2);
group.add(jr3);
group.add(jr4);
group.add(jr5);
group.add(jr6);
group.add(jr7);
this.add(jr1);
this.add(jr2);
this.add(jr3);
this.add(jr4);
this.add(jr5);
this.add(jr6);
this.add(jr7);
this.setVisible(true);
//创建一个画布
g = this.getGraphics();
//创建鼠标监听器对象
mouselistener lis = new mouselistener(g,group);
this.addMouseListener(lis);
}
}
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.ButtonGroup;
public class mouselistener implements MouseListener {
int x1,x2,y1,y2;
private Graphics g;
public String command;
ButtonGroup group;
public mouselistener(Graphics g,ButtonGroup group){
this.g = g;
this.group = group;
}
//鼠标按键在组件上按下时调用
public void mousePressed(MouseEvent e){
x1=e.getX();
y1=e.getY();
}
//鼠标按钮在组件上释放时调用。
public void mouseReleased(MouseEvent e){
x2=e.getX();
y2=e.getY();
String command = group.getSelection().getActionCommand();
if("直线".equals(command))
//画直线
g.drawLine(x1, y1, x2, y2);
else if("矩形".equals(command))
//绘制矩形
g.drawRect(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x2-x1), Math.abs(y2-y1));
else if("椭圆".equals(command))
//绘制椭圆
g.drawOval(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x2-x1), Math.abs(y2-y1));
else if("填充矩形".equals(command))
//填充矩形
g.fillRect(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x2-x1), Math.abs(y2-y1));
else if("圆角矩形".equals(command))
//圆角矩形
g.drawRoundRect(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x2-x1), Math.abs(y2-y1),12,12);
else if("填充椭圆".equals(command))
//填充椭圆
g.fillOval(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x2-x1), Math.abs(y2-y1));
else if("圆弧".equals(command))
//画圆弧
g.drawArc(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x2-x1), Math.abs(y2-y1),45,90);
}
public void mouseClicked(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
}
最终程序运行图见图三。
总结一下:其实本意是想把过程全部想法都分享给大家,由于时间关系,还有很多细节没说到,当然这个画图板也不是最终版,这篇贴以后有时间我会继续完善。