先看下实现后的效果:
![](https://i-blog.csdnimg.cn/blog_migrate/66b40e7a4ddef7e6542dcb0e48b5114f.jpeg)
<!--StartFragment -->
要完成这东西,首先界面的东西是不能少的
public class DrawBorder extends JFrame{
public static void main(String [] args)
{
DrawBorder db=new DrawBorder();
db.initUI();
}
public void initUI()
{
this.setTitle("画图板");
this.setSize(600,500);
//居中
this.setLocationRelativeTo(null);
//不可调大小
this.setResizable(false);
//创建监听对象
DrawListener dl=new DrawListener();
到此停顿一下下,这里DrawBorder继承了JFrame,所以this就是指DrawBorder窗体了。
接着往下,用JPanel组件将整个窗体瓜分为三部分:西边(功能区)、南边(颜色区)、中间(画图区)
//西边
JPanel westPanel=new JPanel();
westPanel.setBackground(Color.GRAY);
westPanel.setPreferredSize(new Dimension(65,0));
//一个字符串数组来保存图片名
String []shapeType={
"pencil","eraser","line","oval","rect","roundrect","five-pointed star","polygon","立体棋"
};
//遍历数组,将图片添加到按钮上
for (int i=0;i<shapeType.length;i++)
{
ImageIcon im=new ImageIcon("image/"+shapeType[i]+".png");
JButton jb=new JButton(im);
jb.setPreferredSize(new Dimension(24, 24));
//按钮动作命令值
jb.setActionCommand(shapeType[i]);
jb.addActionListener(dl);
westPanel.add(jb);
}
this.add(westPanel,BorderLayout.WEST);
//南边(颜色)
//与西边的差不多
JPanel southPanel=new JPanel();
southPanel.setPreferredSize(new Dimension(0, 100));
southPanel.setBackground(Color.CYAN);
southPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
this.add(southPanel,BorderLayout.SOUTH);
Color[]colorType={
Color.BLACK,Color.RED,Color.BLUE,Color.GREEN,new Color(12,23,34)
};
for (int i=0;i<colorType.length;i++)
{
JButton jb=new JButton();
jb.setPreferredSize(new Dimension(24, 24));
jb.setBackground(colorType[i]);
//给按钮添加监听器
jb.addActionListener(dl);
southPanel.add(jb);
}
//中间
JPanel centerPanel=new JPanel();
centerPanel.setBackground(Color.WHITE);
this.add(centerPanel);
this.setDefaultCloseOperation(3);
this.setVisible(true);
//可见之后获取画笔,否则空指针异常
Graphics g=centerPanel.getGraphics();
//调用set函数,将画笔传到监听器中
dl.setG(g);
//添加监听器
centerPanel.addMouseListener(dl);
centerPanel.addMouseMotionListener(dl);
}
接下来就是监听器里的内容了,大概是以下内容:
//获取传来的画笔之用
public void setG(Graphics g)
{
this.g=g;
g.setColor(color);
}
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand()!=null&&!e.getActionCommand().equals(""))
{
shapetype=e.getActionCommand();
}else
{
JButton jb=(JButton)e.getSource();
color=jb.getBackground();
}
}
public void mouseClicked(MouseEvent e) {
//
if(flag==1)
{
x2=e.getX();
y2=e.getY();
g.drawLine(x1, y1, x2, y2);
x1=x2;
y1=y2;
}
int count=e.getClickCount();
if(count==2)
{
g.drawLine(x0, y0, x1, y1);
flag=0;
}
}
public void mousePressed(MouseEvent e) {
if(flag!=1)
{
x1=e.getX();
y1=e.getY();
x0=x1;
y0=y1;
}
g.setColor(color);
}
public void mouseReleased(MouseEvent e) {
x2=e.getX();
y2=e.getY();
if(shapetype.equals("line"))
{
//画直线
g.drawLine(x1, y1, x2, y2);
}
else if(shapetype.equalsIgnoreCase("rect"))
{
//画矩形
g.drawRect(x1, y1, Math.abs(x2-x1),Math.abs(y2-y1));
}
else if(shapetype.equals("oval"))
{
//画椭圆或者圆
g.drawOval(x1, y1, Math.abs(x1-x2), Math.abs(y1-y2));
}
else if (shapetype.equals("roundrect"))
{
//画圆角矩形
g.drawRoundRect(x1, y1, Math.abs(x1-x2),Math.abs(y1-y2) , 20,20);
}
else if(shapetype.equals("five-pointed star"))
{
//画简陋的五角星
int width=Math.abs(x1-x2);
int height=Math.abs(y1-y2);
g.drawLine(x1+width/2, y1-height/5, x1+(2*width)/5, y1+height/5);
g.drawLine(x1+(2*width)/5, y1+height/5, x1, y1+height/5);
g.drawLine(x1, y1+height/5, x1+width/3, y1+(4*height)/9);
g.drawLine(x1+width/3, y1+(4*height)/9, x1+width/5, y1+height);
g.drawLine(x1+width/5, y1+height, x1+width/2, y1+(4*height)/5);
g.drawLine(x1+width/2, y1+(4*height)/5, x1+(4*width)/5, y1+height);
g.drawLine(x1+(4*width)/5, y1+height, x1+(2*width)/3, y1+(3*height)/5);
g.drawLine(x1+(2*width)/3, y1+(3*height)/5, x1+width, y1+height/5);
g.drawLine(x1+width, y1+height/5, x1+(3*width)/5, y1+height/5);
g.drawLine(x1+(3*width)/5, y1+height/5,x1+width/2, y1-height/5);
}
else if(shapetype.equals("polygon"))
{
//多边形
flag=1;
g.drawLine(x1, y1, x2, y2);
x1=x2;
y1=y2;
}
else if (shapetype.equals("立体棋"))
{ //画出多个圆,使其产生立体感
for(int i=0;i<50;i++)
{
Color c=new Color(200+i,200+i, 200+i);
g.setColor(c);
g.fillOval(x2+i, y2+i, 100-2*i, 100-2*i);
}
}
}
public void mouseDragged(MouseEvent e) {
x2=e.getX();
y2=e.getY();
if(shapetype.equals("eraser"))
{
//橡皮的实现
//与铅笔一样,只不过这是与背景颜色相同,且更粗的铅笔
g2=(Graphics2D)g;
g2.setColor(Color.WHITE);
g2.setStroke(new BasicStroke(10));
g.drawLine(x1, y1, x2, y2);
x1=x2;
y1=y2;
g2=(Graphics2D)g;
g2.setColor(Color.BLACK);
g2.setStroke(new BasicStroke(1));
}else if(shapetype.equals("pencil"))
{
//铅笔的实现
g.drawLine(x1, y1, x2, y2);
x1=x2;
y1=y2;
}
}
总而言之,引用胡哥的话,技术是很好学的(虽然我现在感觉不到)。我想慢慢的我会感觉到的