java 画板初步实现
package Doubipanel;import java.awt.BorderLayout;import java.awt.Color;import java.awt.Dimension;import java.awt.Graphics;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JPanel;public class DoubiPanelMain {private Color color=Color.BLACK;public String str="Line";public static void main(String[] args) {DoubiPanelMain dp=new DoubiPanelMain();dp.initUi();}//private PMouseLisener mouselisener;//初始化窗口方法public JFrame initUi(){JFrame frame=new JFrame("逗 比 画 板");//创建动作监听类对象BtActionListener btal=new BtActionListener(this);//设置窗口大小frame.setSize(690,500);//设置窗口大小不可改变frame.setResizable(false);//设置窗体位置居中frame.setLocationRelativeTo(null);;//设置关闭frame.setDefaultCloseOperation(3);//创建事件监听类对象//调用添加创建北边面板的方法creatPanelN(frame,btal);//调用添加创建中间面板的方法JPanel panelC= creatPanelC(frame);//设置窗口可见frame.setVisible(true);//创建绘制图形的对象Graphics g=panelC.getGraphics();//创建鼠标事件监听类对象PMouseLisener mouselisener=new PMouseLisener(g,this);panelC.addMouseListener(mouselisener);panelC.addMouseMotionListener(mouselisener);return frame;}//获得actionlistener的名称的方法public String getStr(){return str;}public Color getColor(){return color;}public void setColor(Color color){this.color=color;}//添加北画板的方法public JPanel creatPanelN(JFrame frame,BtActionListener btal){JPanel panelN=new JPanel();panelN.setBackground(new Color(1,250,250));panelN.setPreferredSize(new Dimension(0,75));String [] bt1= {"Line", "Rect", "Oval", "RoundRect", "Polygon","pencil","brush","eraser","color","spraygun"};for (int i=0;i<10;i++){JButton bt=new JButton(bt1[i]);bt.setActionCommand(bt1[i]);bt.addActionListener(btal);panelN.add(bt);}//将面板添加到窗口上frame.add(panelN,BorderLayout.NORTH);return panelN;}//中间面板的方法public JPanel creatPanelC(JFrame frame){JPanel panelC=new JPanel();panelC.setBackground(Color.white);//panelC.addMouseListener(mouselisener);frame.add(panelC,BorderLayout.CENTER);return panelC;}}package Doubipanel;import java.awt.Color;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JColorChooser;public class BtActionListener implements ActionListener{//构造方法String str;DoubiPanelMain dp;public BtActionListener(DoubiPanelMain dp){this.dp=dp;}public void actionPerformed(ActionEvent e) {/*if (e.getActionCommand().equals("Line")){}else if(e.getActionCommand().equals("Rect")){}else if(e.getActionCommand().equals("RoundRect")){}else if(e.getActionCommand().equals("Polygon")){}*/ dp.str=e.getActionCommand(); if (dp.str.equals("color")) { dp.setColor(JColorChooser.showDialog(null,"colorchooser",Color.black)); }}}package Doubipanel;import java.awt.Color;import java.awt.Graphics;import java.awt.event.MouseEvent;import java.awt.event.MouseMotionListener;import java.util.Random;import javax.swing.event.MouseInputListener;public class PMouseLisener implements MouseInputListener,MouseMotionListener{ private int x,y,x1,x2,y1,y2,x0,y0;//private int x[],y[];private int i=0,j=0;private Graphics g; private DoubiPanelMain dp;//构造方法public PMouseLisener (Graphics g,DoubiPanelMain dp){this.g=g;this.dp=dp;}public void mouseEntered(MouseEvent arg0) {//System.out.println(x1+" "+y1);} //记录鼠标按下public void mousePressed(MouseEvent e) {g.setColor(dp.getColor());if(dp.str.equals("Polygon") ){if(i==0){System.out.println("坑爹");i=1;x=x0=x1=e.getX();y=y0=y1=e.getY();System.out.println(x1+" "+y1);}}else {x1=e.getX();y1=e.getY();System.out.println(x1+" "+y1);}}public void mouseDragged(MouseEvent e) {if(dp.str.equals("pencil")){x2=e.getX();y2=e.getY();g.drawLine(x1, y1, x2, y2);y1=y2;x1=x2;}else if(dp.str.equals("spraygun")){x2=e.getX();y2=e.getY();Random random=new Random();for (int l=1;l<=30;l++){int j=random.nextInt(11)-5;int i=random.nextInt(11)-5;g.drawLine(x2+i, y2+j, x2+i, y2+j);}}else if(dp.str.equals("brush")){x2=e.getX();y2=e.getY();for(i=-2;i<=2;i++){g.drawLine(x1, y1+i, x2, y2+i);}for(i=-2;i<=2;i++){g.drawLine(x1+i, y1, x2+i, y2);}x1=x2;y1=y2;}else if (dp.str.equals("eraser")){g.setColor(Color.white);x2=e.getX();y2=e.getY();for(i=-5;i<=5;i++){g.drawLine(x1, y1+i, x2, y2+i);}for(i=-5;i<=5;i++){g.drawLine(x1+i, y1, x2+i, y2);}x1=x2;y1=y2;}else {//此段代码原本想实现结果预显功能的- -但失败了/*x2=e.getX();y2=e.getY();if (dp.str.equals("Line")){//if((x!=x2 || y!= y2) ){g.setColor(Color.WHITE);g.drawLine(x1,y1,x,y);g.setColor(dp.getColor());g.drawLine(x1,y1,x2,y2);x=x2;y=y2;}else if(dp.str.equals("Rect")){if((x!=x2 || y!= y2) ){g.setColor(Color.WHITE);g.drawRect(x1>=x? x:x1, y1>=y?y:y1, Math.abs(x1-x), Math.abs(y1-y));g.setColor(dp.getColor());}g.drawRect(x1>=x2? x2:x1, y1>=y2?y2:y1, Math.abs(x1-x2), Math.abs(y1-y2));x=x2;y=y2;}else if(dp.str.equals("Oval")){g.setColor(Color.WHITE);g.drawOval(x1>=x? x:x1, y1>=y?y:y1, Math.abs(x1-x), Math.abs(y1-y));g.setColor(dp.getColor());g.drawOval(x1>=x2? x2:x1, y1>=y2?y2:y1, Math.abs(x1-x2), Math.abs(y1-y2));x=x2;y=y2;}else if(dp.str.equals("RoundRect")){g.setColor(Color.WHITE);g.drawRoundRect(x1>=x? x:x1, y1>=y?y:y1, Math.abs(x1-x), Math.abs(y1-y),25,25);g.setColor(dp.getColor());g.drawRoundRect(x1>=x2? x2:x1, y1>=y2?y2:y1, Math.abs(x1-x2), Math.abs(y1-y2),25,25);x=x2;y=y2;}*/}} //记录鼠标释放public void mouseReleased(MouseEvent e) {x2=e.getX();y2=e.getY();if(dp.str.equals("Polygon")){//if(i==1&&j==0){//j++;//x[j+1]=e.getX();//y[j+1]=e.getY();x2=e.getX();y2=e.getY();g.drawLine(x1, y1, x2, y2);if(x1==x2&& y1==y2) q++;x1=x2;y1=y2;//}}else {x2=e.getX();y2=e.getY();if (dp.str.equals("Line")){g.drawLine(x1, y1, x2, y2);}else if(dp.str.equals("Rect")){g.drawRect(x1>=x2? x2:x1, y1>=y2?y2:y1, Math.abs(x1-x2), Math.abs(y1-y2));}else if(dp.str.equals("Oval")){g.drawOval(x1>=x2? x2:x1, y1>=y2?y2:y1, Math.abs(x1-x2), Math.abs(y1-y2));}else if(dp.str.equals("RoundRect")){g.drawRoundRect(x1>=x2? x2:x1, y1>=y2?y2:y1, Math.abs(x1-x2), Math.abs(y1-y2),25,25);} }}private int q=0;@Overridepublic void mouseClicked(MouseEvent e) {if(dp.str.equals("Polygon")){x2=e.getX();y2=e.getY();g.drawLine(x1, y1, x2, y2);if(q==2){g.drawLine(x0,y0,x2,y2);q=0;i=0;}}}@Overridepublic void mouseExited(MouseEvent arg0) {// TODO Auto-generated method stub}@Overridepublic void mouseMoved(MouseEvent arg0) {// TODO Auto-generated method stub}}
?