Java drawing board supplemental features (a)

    We can be modified to the drawing board has been completed, the main today is to add some new features.

A draw curve

    The idea of ​​drawing curves and draw a line similar to the need to use MouseMotionListener.

  • First, add a button to "curve" At this point, we added advantage for circulation buttons may show up, just add to the textArr [] in.
// 添加组件(按钮)
String textArr[] = {"改变粗细", "直线","曲线", "点", "迭代" ,"平面山水画","地貌图"};
for (String text : textArr) {
	JButton btn = new JButton(text);
	Dimension botton = new Dimension(100, 30);
	btn.setPreferredSize(botton);
	ui.add(btn);
	btn.addActionListener(mousel);
}
  • Set up listeners to note, before using MouseListener, even if we will MouseMotionListener and MouseListener are implemented in the same class, but also added separately when you add a listener to the form or component
MyListener mousel = new MyListener();
ui.addMouseListener(mousel);
ui.addMouseMotionListener(mousel);
  • MouseDragged method listeners realize, this listener as long as the mouse down, it would have been circulating, no additional add a loop.
public void mouseDragged(MouseEvent e) {
	if (bottonText.equals("曲线")) {
		g.fillOval(e.getX(), e.getY(), r, r);
	}
}

Output

curve
    Although realized the function curve drawing, there are still problems. MouseDragged returns the coordinates value too slow, if we speed up the rendering speed, intermittent case of the curve will appear.

Second, changing the thickness of the line and point

    We use the method to draw fillOval point and the portion of the line, these patterns, we can change the width of the line and point by varying the radius r.

if (r > 20)
	r -= 20;
else
	r += 5;

We controlled by varying radius r and the maximum setting change interval, so that a plurality of gear change can be achieved.

Output

Thickness
    Next, I will try to use the GridBagLayout class to achieve a relatively smooth slider to adjust the size.

Third, add a menu bar

    We do just add menu bar component, implementation of functionality will be placed behind the blog.
    Requires that JMenuBar, JMenu and JMenuItem three classes.

Menu Bar
    Add button and the same way, we use a for loop to add the same component, to facilitate future changes.

//添加菜单栏
JMenuBar menubar = new JMenuBar();
ui.setJMenuBar(menubar);
//设置菜单和子菜单文本
String MenuTextArr[] = { "文件", "编辑", "帮助" };
String MenuItem[][] = { { "新建文件", "打开文件", "保存文件", "关闭文件" }, { "清空", "撤销" }, { "使用说明", "制作人员" } };
//添加菜单和子菜单
for (int i=0;i<MenuTextArr.length;i++) {
	JMenu menu = new JMenu(MenuTextArr[i]);
	//将子菜单添加到对应菜单上		
	for(int j=0;j<MenuItem[i].length;j++) {
		JMenuItem subMenu=new JMenuItem(MenuItem[i][j]);
		subMenu.addActionListener(mousel);
		menu.add(subMenu);
	}
	//将菜单添加到菜单栏上
	menubar.add(menu);
}

summary

    There are many more features and improvements, it will be mentioned in later blog.

Released nine original articles · won praise 6 · views 283

Guess you like

Origin blog.csdn.net/qq_43348021/article/details/104504927