如上画板的效果图:
第一先做好界面。
1。布局:左边面板,中间面板,底部面。setLayout(new BorderLayout()),便可布局,分位,东,南,西,北
2.左边布局流式布局。setLayout(new FlowLayout());
代码如下:
这里写代码片
public class PaneleLeft extends Jpanel{
String commd = null;
String[] arr = { "airbrush", "brush", "color_picker", "curve",
"dot_rect", "eraser", "fill", "line", "magnifier", "oval",
"pencil", "polygon", "rect", "round_rect", "star", "word"};
public JpanelLeft(){
//设置左边面板的大小
Dimension de = new Dimension(60,400);
//按钮的大小
Dimension de1 = new Dimension(25,25);
setPreferredSize(de);
//加监听用的是匿名内部类不是给面板在监听,是给里面的组件按钮加监听
ActionListener actionlisten = new ActionListener(){
//重写父类方法
public void actionPerformed(ActionEvent e){
//获取监听命令,
commd = e.getActionCommand();
}
}
}
for(int i=0;i<arry.length;i++){
JButton button = new JButton();
button.setIcon(new ImageIcon("images/"+arr[i]+".jpg"));
//给按钮设置命令
button.setActionCommand(arr[i]);
add(button);
button.addAcitonListener(actionlisten);
}
}
画板的中间面板的设置,及部分功能的实现如下图:
package cn.huaxin.draw.layer;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.Random;
import javax.swing.JPanel;
public class JpanelCenter extends JPanel {
public Graphics2D graphics;
public JpanelLeft jpanelLeft;
public int count;
int startx, starty, polygonMiddlex, polygonmiddley, polyStartx, polyStarty,
endx, endy;
public JpanelCenter(Graphics2D g, JpanelLeft jpanelLeft1) {
graphics = g;
jpanelLeft = jpanelLeft1;
this.setBackground(Color.white);
MouseMotionListener mouseMotionlistener = new MouseMotionListener() {
// 鼠标拖动事件
public void mouseDragged(MouseEvent e) {
// System.out.println("鼠标拖动");
int x = e.getX();
int y = e.getY();
// 铅笔
if (jpanelLeft.commd.equals("pencil")) {
graphics.drawLine(startx, starty, x, y);
startx = x;
starty = y;
} else if (jpanelLeft.commd.equals("airbrush")) {// 喷桶
for (int i = 0; i <= 30; i++) {
int ran = new Random().nextInt(8) - 4;
int ran1 = new Random().nextInt(8) - 4;
graphics.drawLine(x + ran, y + ran1, x + ran, y + ran1);
startx = x;
starty = y;
}
} else if (jpanelLeft.commd.equals("brush")) {// 刷子
graphics.drawLine(startx, starty, x, y);
startx = x;
starty = y;
} else if (jpanelLeft.commd.equals("eraser")) {
graphics.drawLine(startx, starty, x, y);
startx = x;
starty = y;
}
}
public void mouseMoved(MouseEvent e) {
}
};
MouseListener mouse = new MouseListener() {
public void mouseClicked(MouseEvent e) {
// System.out.println("点击了");
int mousecount = e.getClickCount();
if ("polygon".equals(jpanelLeft.commd)) {
if (mousecount == 1) {
graphics.drawLine(polygonMiddlex, polygonmiddley,
e.getX(), e.getY());
polygonMiddlex = e.getX();
polygonmiddley = e.getY();
} else if (mousecount == 2) {
graphics.drawLine(polyStartx, polyStarty,
polygonMiddlex, polygonmiddley);
count = 0;
}
}
}
// 鼠标按下
public void mousePressed(MouseEvent e) {
startx = e.getX();
starty = e.getY();
// graphics.setColor(Color.BLACK);
if (jpanelLeft.commd.equals("polygon")) {
if (count == 0) {
polyStartx = startx;
polyStarty = starty;
}
} else if ("eraser".equals(jpanelLeft.commd)) {
graphics.setColor(Color.WHITE);
}
}
// 鼠标释放
public void mouseReleased(MouseEvent e) {
endx = e.getX();
endy = e.getY();
// 画线
if (jpanelLeft.commd.equals("line")) {
graphics.drawLine(startx, starty, endx, endy);
} else if (jpanelLeft.commd.equals("oval")) {// 椭圆
graphics.drawOval(Math.min(startx, endx),
Math.min(starty, endy), Math.abs(endx - startx),
Math.abs(endy - starty));
} else if (jpanelLeft.commd.equals("rect")) {// 矩形
graphics.drawRect(Math.min(startx, endx),
Math.min(starty, endy), Math.abs(endx - startx),
Math.abs(endy - starty));
} else if (jpanelLeft.commd.equals("polygon")) {// 多边形
if (count == 0) {
graphics.drawLine(startx, starty, endx, endy);
polygonMiddlex = endx;
polygonmiddley = endy;
count++;
}
} else if (jpanelLeft.commd.equals("round_rect")) {
graphics.drawRoundRect(Math.min(startx, endx),
Math.min(starty, endy), endx - startx, endy
- starty, 100, 100);
}
}
public void mouseExited(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
};
this.addMouseMotionListener(mouseMotionlistener);
this.addMouseListener(mouse);
}
}