java画图板小试

    初学Java语言,感觉它功能比较强大,尤其是可视化这一方面。最近初学了一些界面开发,感觉比较有趣,特此说说我的感受。我最近在模仿系统画图板,遇上很多问题,比如函数的构造,传参,变量调用,空指针异常等让我很是头疼。经过老师的指点和几天的努力,我的画图板初有成效,能够画简单的直线,三角形,五角星,圆,鸡蛋,圆锥,还可以做调色板,文件打开,文件保存及退出程序等功能,有了小小收获,让我兴趣大增。接下来将完善画图板,给线条设置宽度,添加文字处理功能,添加更多的图形,添加图形的复制、粘贴、剪切功能,添加图形缩放功能,添加刷子及橡皮擦等功能。我将在附件中上传我的程序代码,希望各位能指点点。下面具体讲解画图板:

一。首先要实现画图板必须有窗体,还有一些按钮等组件



Java代码
 

                          

this.setTitle("我的画图板1.0版");
  // 设置画图啊不能窗体大小
  this.setSize(1000, 700);
  // 设置窗体布局
    // 创建菜单栏
  JMenuBar bar = new JMenuBar();
  // 创建文件菜单
  JMenu fileMenu = new JMenu("文件(F)");
  // 设置菜单标识
  fileMenu.setMnemonic('F');
  // 新建文件菜单项
  JMenuItem newItem = new JMenuItem("新建-

二、给窗体添加三个面板并设置布局为边框布局份额辨别用于存放菜单,按钮,画图,代码如下:

BorderLayout layout = new BorderLayout();
this.setLayout(layout);

JPanel panel_n = new JPanel();
  panel_n.setLayout(gridlayout);
  panel_n.add(bar);
  // 创建北边面板并设置属性
  JPanel panel_c = new JPanel();
  // panel_n.setLayout(layout0);
  panel_c.setPreferredSize(new Dimension(1000, 70));
  panel_c.setBackground(Color.blue);
  panel_c.setLayout(layout0);
  // 创建中间面板并设置属性
  final JPanel panel_s = new MyPanel();
  panel_s.setPreferredSize(new Dimension(1000, 550));
  panel_s.setBackground(Color.white);
   // 创建按钮组

this.add(panel_s, BorderLayout.SOUTH);
  this.add(panel_n, BorderLayout.NORTH);
  this.add(panel_c, BorderLayout.CENTER);

效果如下:

java画图板小试_第1张图片

 

 三、然后给个组件添加监听器,代码如下:

dl = new DrawListener(panel_s, g, bgr1, shapeList);

  // 给窗体加上鼠标监听器
  panel_s.addMouseListener(dl);
  panel_s.addMouseMotionListener(dl);
  color_button.addActionListener(dl);
  openItem.addActionListener(dl);

  //给子菜单添加监听器
  newItem.addActionListener(dl);
  saveItem.addActionListener(dl);
  exitItem.addActionListener(dl);
  aboutItem.addActionListener(dl);
  colorItem.addActionListener(dl);
  四、实现绘图功能,代码如下:

public void drawShape(Graphics g) {
g.setColor(color);

if (type3.equals("圆锥")) {// 绘制圆锥
for (int i = 0; i <= 50; i++) {
g.setColor(new Color(Math.abs(color.getRed() - i), i, Math
.abs(color.getBlue() - i)));
// g.fillOval(Math.min(x1, x2), Math.min(y1, y2)+i, i, i);
g.fillOval(Math.min(x1, x2) - i, Math.min(y1, y2) - i,
100 - 2 * i, 100 - 2 * i);
g.fillOval(Math.min(x1, x2), Math.min(y1, y2), 100, 100);

}
} else if (type3.equals("六面体")) {// 绘制六面体
for (int i = 1; i <= 120; i++) {
g.setColor(new Color(250 - i, 170 - i, 250 - i));
g.drawRect(Math.min(x1, x2) + i, Math.min(y1, y2) + i, 100, 120);
}
} else if (type3.equals("圆柱")) {// 绘制圆柱
for (int i = 0; i <= 90; i++) {
g.setColor(new Color(200 - i, 100 + i, 200 - i));
g.drawOval(Math.min(x1, x2) - i, Math.min(y1, y2),
Math.abs(x2 - x1), Math.abs(x2 - x1));
}
} else if (type3.equals("十字架")) {// 绘制十字架
for (int i = 0; i <= 90; i++) {
g.setColor(new Color(255 - i, i, 255 - i));
g.drawRect(Math.min(x1, x2) - i, Math.min(y1, y2) - i, 30, 30);
g.drawRect(Math.min(x1, x2) + i, Math.min(y1, y2) - i, 30, 30);
g.drawRect(Math.min(x1, x2) + i, Math.min(y1, y2) + i, 30, 30);
g.drawRect(Math.min(x1, x2) - i, Math.min(y1, y2) + i, 30, 30);
}
} else if (type3.equals("鸡蛋")) {// 绘制鸡蛋
for (int i = 0; i <= 50; i++) {
g.setColor(new Color(155 + 2 * i, 100 + 2 * i, 40 + 2 * i));
g.fillOval(Math.min(x1, x2) + i, Math.min(y1, y2), 100 - 2 * i,
160 - 2 * i);

}
} else if (type3.equals("球")) {// 绘制球
for (int i = 1; i <= 50; i++) {
g.setColor(new Color(155 + 2 * i, 120 + 2 * i, 150 + i));

g.fillOval(Math.min(x1, x2) + i, Math.min(y1, y2) + i,
105 - 2 * i, 105 - 2 * i);

}
} else if (type3.equals("四角星")) {// 绘制四角星
jp.repaint();
g.drawLine(Math.min(x1, x2), Math.min(y1, y2),
(Math.min(x1, x2) + (x1 + x2) / 2) / 2, (y1 + y2) / 2);
g.drawLine(Math.min(x1, x2), Math.max(y1, y2),
((x1 + x2) / 2 + Math.min(x1, x2)) / 2, (y1 + y2) / 2);
g.drawLine(Math.min(x1, x2), Math.max(y1, y2), (x1 + x2) / 2,
((y1 + y2) / 2 + Math.max(y1, y2)) / 2);
g.drawLine(Math.max(x1, x2), Math.max(y1, y2), (x1 + x2) / 2,
((y1 + y2) / 2 + Math.max(y1, y2)) / 2);
g.drawLine(Math.max(x1, x2), Math.max(y1, y2),
((x1 + x2) / 2 + Math.max(x1, x2)) / 2, (y1 + y2) / 2);
g.drawLine(Math.max(x1, x2), Math.min(y1, y2),
((x1 + x2) / 2 + Math.max(x1, x2)) / 2, (y1 + y2) / 2);
g.drawLine(Math.max(x1, x2), Math.min(y1, y2), (x1 + x2) / 2,
((y1 + y2) / 2 + Math.min(y1, y2)) / 2);
g.drawLine(Math.min(x1, x2), Math.min(y1, y2), (x1 + x2) / 2,
((y1 + y2) / 2 + Math.min(y1, y2)) / 2);

} else if (type3.equals("圆")) {// 绘制圆
jp.repaint();
g.drawOval(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x2 - x1),
Math.abs(x2 - x1));
} else if (type3.equals("直线")) {// 绘制直线
jp.repaint();
g.drawLine(x1, y1, x2, y2);
} else if (type3.equals("矩形")) {// 绘制矩形
jp.repaint();
g.drawRect(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x2 - x1),
Math.abs(y2 - y1));
} else if (type3.equals("正方形")) {// 绘制正方形
jp.repaint();
g.drawRect(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x2 - x1),
Math.abs(x2 - x1));
} else if (type3.equals("椭圆")) {// 绘制椭圆
jp.repaint();
g.drawOval(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x2 - x1),
Math.abs(y2 - y1));keItem.addActionListener(dl);

执行结果如图:

java画图板小试_第2张图片

五、调色板实现代码如下:

color = JColorChooser.showDialog(null, "请选择颜色", Color.black);

执行后效果如下:

java画图板小试_第3张图片

六、在制作过程中,为避免变量的各种调用,我用一个类来实现几个监听器,代码如下:

public class DrawListener implements MouseListener, MouseMotionListener,
  ActionListener {}

七、在绘制直线、矩形、圆等功能时,我不是像老师说的把前面用白色画出来,而是前面不画,只有鼠标按下时才画,释放时才保存,这样可以避免把画好的图形遮住,代码实现如下:

public void mouseDragged(MouseEvent e) {

  a = x2;
  b = y2;
  // 获取鼠标拖动是的坐标
  x2 = e.getX();
  y2 = e.getY();
  x2 = e.getX();
  y2 = e.getY();
  shape.x1 = x1;
  shape.x2 = x2;
  shape.y1 = y1;
  shape.y2 = y2;
  shape.color = color;
  shape.a = a;
  shape.b = b;
  // 鼠标拖动是绘制图形
  shape.drawShape(g);

 }

 public void mousePressed(MouseEvent e) {
  // 创建按钮模式

  ButtonModel bm1 = bgr1.getSelection();// 选择立体图
  // ButtonModel bm2 = bgr1.getSelection();//选择线性图
  String command1 = bm1.getActionCommand();
  // String command2 = bm2.getActionCommand();
  type1 = command1;
  // type2 = command2;

  // 获取鼠标光标按下时的x,y坐标
  x1 = e.getX();
  y1 = e.getY();
  shape = new Shape(jp);
  shape.color = color;

  if (type1.equals("star")) {// 绘制四角星

   shape.type3 = "四角星";

  }

  if (type1.equals("oval")) {// 绘制圆

   shape.type3 = "圆";

  }

  if (type1.equals("line")) { // 绘制直线

   shape.type3 = "直线";

  }

  if (type1.equals("rect")) {// 绘制矩形

   shape.type3 = "矩形";

  }

  if (type1.equals("squre")) { // 绘制正方形

   shape.type3 = "正方形";

  }

  if (type1.equals("tuoyuan")) {// 绘制椭圆

   shape.type3 = "椭圆";

  }

  if (type1.equals("yuanzhui")) {// 绘制圆锥

   shape.type3 = "圆锥";
  }

  if (type1.equals("cylinder")) {// 绘制圆柱

   shape.type3 = "圆柱";
  }
  if (type1.equals("hexahedron")) {// 绘制六面体

   shape.type3 = "六面体";
  }
  if (type1.equals("globe")) {// 画球
   shape.type3 = "球";
  }
  if (type1.equals("closs")) {// 绘制十字架
   shape.type3 = "十字架";
  }
  if (type1.equals("egg")) {// 绘制鸡蛋

   shape.type3 = "鸡蛋";
  }

 }
public void mouseReleased(MouseEvent e) {
  // //获取鼠标光释放时时的x,y坐标
  x2 = e.getX();
  y2 = e.getY();

  shape.x1 = x1;
  shape.x2 = x2;
  shape.y1 = y1;
  shape.y2 = y2;


  // 绘制形状
  shape.drawShape(g);
  // 将形状保存在队列中
  shapeList.add(shape);
 }

       通过这些步骤,一个简单的画图板就实现了,希望下周我能把系统画图板的基本功能实现,我会加油的!

你可能感兴趣的:(java)