1.在这主要是通过swing,awt组件对一些基本图形的绘制,在这里主要是实现了,直线,矩形,三角形等一些基本图形,以及颜色的选择,和部分图形的填充,稍后的优化版也会第一时间发布。 package Cl.Adraw; import javax.swing.*; import java.awt.*; public class Drawui { //1.绘制界面 public void intUI(){ JFrame jf=new JFrame(); //设立主窗体大小 jf.setSize(900,900); //设置窗体名称 jf.setTitle("绘画工具"); //设置流动式布局 jf.setLocationRelativeTo(null); java.awt.FlowLayout flow=new java.awt.FlowLayout(); jf.setLayout(flow); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //设置窗体背景颜色 jf.getContentPane().setBackground(Color.white); //按钮的设定与选择 JButton jbu=new JButton("直线"); java.awt.Dimension ds=new java.awt.Dimension(100,40); jbu.setPreferredSize(ds); jf.add(jbu); JButton jbu1=new JButton("矩形"); java.awt.Dimension ds1=new java.awt.Dimension(100,40); jbu1.setPreferredSize(ds1); jf.add(jbu1); JButton jbu2=new JButton("三角形"); java.awt.Dimension ds2=new java.awt.Dimension(100,40); jbu2.setPreferredSize(ds2); jf.add(jbu2); //设置选择背景颜色 JButton jbu3=new JButton(); jbu3.setBackground(Color.red); Dimension ds3=new Dimension(100,40); jbu3.setPreferredSize(ds3); jf.add(jbu3); //设置选择背景颜色 JButton jbu4=new JButton(); jbu4.setBackground(Color.green); Dimension ds4=new Dimension(100,40); jbu4.setPreferredSize(ds4); jf.add(jbu4); //设置选择背景颜色 JButton jbu5=new JButton(); jbu5.setBackground(Color.yellow); Dimension ds5=new Dimension(100,40); jbu5.setPreferredSize(ds5); jf.add(jbu5); //设置鼠标点击时候,跟随其移动的轨迹 JButton jbu6=new JButton("鼠标作画"); java.awt.Dimension ds6=new java.awt.Dimension(100,40); jbu6.setPreferredSize(ds6); jf.add(jbu6); //设置窗体可见 jf.setVisible(true); //从窗体中获取画笔 Graphics g=jf.getGraphics(); //监听器的设置 //先给窗体添加鼠标监听器 Drawmouse mouse=new Drawmouse(); jf.addMouseListener(mouse); //添加鼠标拖动监听器 jf.addMouseMotionListener(mouse); //给按钮也分别添加监听器 jbu.addActionListener(mouse); jbu1.addActionListener(mouse); jbu2.addActionListener(mouse); jbu3.addActionListener(mouse); jbu4.addActionListener(mouse); jbu5.addActionListener(mouse); jbu6.addActionListener(mouse); mouse.gr=g; //把Shape数组从DrawMouse类传递给MyPanel // centerPanel.shapeArr=mouse.shapeArr; } public static void main(String[] args) { Drawui ui=new Drawui(); ui.intUI(); } } package Cl.Adraw; import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Drawmouse implements MouseListener, ActionListener,MouseMotionListener { public Graphics gr; public int x1,y1,x2,y2,x3,y3,x4,y4; public String name; public int flag=0;//位置进行标记 public void actionPerformed(ActionEvent e){ if("".equals(e.getActionCommand())){ //获取当前事件源 JButton jbu=(JButton) e.getSource(); //获取按钮上的背景颜色,并设置给画笔 Color color=jbu.getBackground(); gr.setColor(color); }else { //获取按钮内容 name=e.getActionCommand(); } System.out.println("name:"+name); } //对接口中的方法进行重写 public void mouseClicked(MouseEvent e){ System.out.println("点击"); x3=e.getX(); y3=e.getY(); if("三角形".equals(name)&&flag==1){ gr.drawLine(x1,y1,x3,y3); gr.drawLine(x2,y2,x3,y3); flag-=1; } } //对接口中的方法进行重写 public void mousePressed(MouseEvent e){ System.out.println("按下"); //获取当前坐标值 if(flag==0){ x1=e.getX(); y1=e.getY(); } } //对接口中的方法进行重写 public void mouseReleased(MouseEvent e) { System.out.println("松开"); //获取当前坐标值 if (flag == 0) { x2 = e.getX(); y2 = e.getY(); } //开始判别图形标识信息 if ("直线".equals(name)) { //画线开始 gr.drawLine(x1, y1, x2, y2); } if ("矩形".equals(name)) { gr.drawRect(x1, y1, Math.abs(x2 - x1), Math.abs(y2 - y1)); gr.fillRect(x1, y1, Math.abs(x2 - x1), Math.abs(y2 - y1)); } if ("三角形".equals(name) && flag == 0) { gr.drawLine(x1, y1, x2, y2); flag += 1; } } //轨迹直线的设定 public void mouseDragged(MouseEvent e){ gr.setColor(Color.white); gr.drawLine(x1,y1,x4,y4); x4=e.getX(); y4=e.getY(); gr.setColor(Color.black); gr.drawLine(x1,y1,x4,y4); } public void mouseMoved(MouseEvent e){ } public void mouseEntered(MouseEvent e){ System.out.println("进入"); } public void mouseExited(MouseEvent e){ System.out.println("退出"); } }
以上给出了源代码以及标注出相关注释,效果放最后