画图板总结

学习JAVA已有两个月的时间了,虽然是学习软件工程,但是在这方面一点基础没有。经过左哥,龙哥,熊哥的教导。现在基本能把仿XP的简单画板做好。
首先说一下,在做画板方面遇到了很大的问题。刚开始匿名内部类和内部类把我脑子高的一团糟,现在终于可以理清了。
先说一下内部类:
class ButtonListener implements ActionListener{

public void actionPerformed(ActionEvent e) {
dl.color=javax.swing.JColorChooser.showDialog(null, "颜色选择", Color.BLACK);
}}
}
匿名内部类:
ActionListener bl = new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
color = javax.swing.JColorChooser.showDialog(null, "简单画图板选择颜色", Color.black);
}
};
jb.addActionListener(bl);
}
}
画图板,顾名思义,就是指一个板。首先它有,颜色选择,工具选择,画布,菜单栏等简单几个部分组成。
首先先建立一个画板的对象,继承JPanel类。
public class DrawBoard extends JFrame{
Graphics g;
public static void main(String args[]){
DrawBoard db=new DrawBoard();
db.shown();
}
public void shown(){
this.setTitle("简单的画图板");
this.setSize(600,500);
this.setDefaultCloseOperation(3);
this.setResizable(false);
this.setLayout(new BorderLayout());
JMenuBar jmb=new JMenuBar();
this.setJMenuBar(jmb);
String[] a={"文件","颜色","帮助"};
String[][] b={{"新建","打开","保存","退出"},{"编辑颜色"},{"帮助主题","关于"}};
for(int i=0;i<a.length;i++){
JMenu jm=new JMenu(a[i]);
jmb.add(jm);
for(int j=0;j<b[i].length;j++){
JMenuItem jmi=new JMenuItem(b[i][j]);
jm.add(jmi);
}
this.setVisible(true);
}


public class DrawingPanel extends JPanel {
/**
* 构造方法
*/
public DrawingPanel() {
init();// 调用初始化面板的方法
}

// 实例化一个JPanel类的对象属性,用来获取画布
private JPanel panel = new JPanel();

/**
* 获取JPanel对象
*/
public JPanel getJPanel() {
return panel;
}
private void init() {
// 设置DrawingPanel的布局为流式布局布局
this.setLayout(new FlowLayout(FlowLayout.LEFT));
this.setBackground(Color.LIGHT_GRAY);
// 实例化一个JPanel类的对象
JPanel panel = new JPanel();
panel.setBackground(Color.WHITE);
panel.setPreferredSize(new Dimension(400,300));
// 将panel添加到DrawingPanel上
this.add(panel);
}


}
在建立一个颜色选择面板,和工具选择面板,都加入到整个画板中.这里就不一一的写上。

注意工具栏中要建立按钮,
Colorpanel colorpanel=new Colorpanel();
this.add(colorpanel,BorderLayout.SOUTH);

Toolspanel tp=new Toolspanel();
this.add(tp,BorderLayout.WEST);

Drawpanel dp=new Drawpanel();
this.add(dp,BorderLayout.CENTER);


做画板最重要的是建立监听器。因为你把画板都建立好了,它不可能在上面进行相关操作,所以重要的还是要建立监听器加入到相对应的画板上。这里的监听器包括,MouseListener,ActionListener.
简单的画图板,鼠标监听器,用到的只有mousePressed和mouseReleased.
建立一个DrawListener继承MouseListener

[/code]public class DrawListener implements MouseListener,MouseMotionListener {
int x1,y1,x2,y2;
Graphics g;
ButtonGroup bg;
ButtonGroup bg1;
String command;
String command1;
Color color=Color.red;
public DrawListener(Graphics g,ButtonGroup bg,ButtonGroup bg1){
this.g=g;
this.bg=bg;
this.bg1=bg1;
}
public void mousePressed(MouseEvent e) {
g.setColor(color);
x1=e.getX();
y1=e.getY();}//鼠标按下时
public void mouseReleassed(MouseEvent e) {
x2=e.getX();
y2=e.getY();
if(command.equals("line")){
g.drawLine(x1,y1,x2,y2);
}//鼠标释放时,一般在鼠标释放时进行绘图操作,这里以直线为代表。
要把监听器添加到要画图的面板panel中。注意建立监听器时,所用的构造函数,在建立对象时,一定要把实参带入。
DrawListener dl = new DrawListener(colorpanel,tp,panel);
//给面板添加鼠标监听器方法
panel.addMouseListener(dl);
panel.addMouseMotionListener(dl);
在选择工具时,要把监听器添加到按钮上。这时的监听器为ActionListener,
[code="java"]
ActionListener al=new ActionListener(){

public void actionPerformed(ActionEvent e) {
command=e.getActionCommand();
}

};
bt.add(al);//把监听器加入到按钮上
这个时候就可以利用DrawListener来进行绘图操作。
这个时候就算是把图形画好,那么当窗体移动式,绘好的图也会消失。那么这里就需要用到数组的重绘。这里我们就利用简单的二维数组来保存像素点,来进行重绘。
此时要对所画图形整个区域(画板)进行截屏操作

public BufferedImage onScreen(){
//获取画布相对于画板的屏幕坐标
point=jp.getLocationOnScreen();
System.out.println(point); // 实例化一个封转矩形类的对象Rectangle rect = new Rectangle((int)point.getX(), (int) point.getY(),
jp.getWidth(), jp.getHeight());
// 进行截屏操作
BufferedImage image =ro.createScreenCapture(rect);
return image;
}
}


根据坐标获取颜色区域的像素点

/**
* 获取图像上的像素点
*/
private void getColor(){
BufferedImage image =onScreen();
System.out.println(image.getWidth()+image.getHeight());
//开始遍历image图片
for(int i=0;i<image.getWidth();i++){
for(int j=0;j<image.getHeight();j++){
//将对应坐标点颜色值取出存入到二维数组中。
Drawpanel.a[i][j] = image.getRGB(i, j);
}
}
}

最后在drawPanel重绘图形。根据获取的像素点数组中的值来进行每个像素点的绘图。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值