重绘完整代码
有关重绘的完整代码如下:
package drawFrame; import java.awt.Color; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.Graphics; import javax.swing.JFrame; public class DrawFrame { public static void main(String[] args){ DrawFrame df = new DrawFrame(); df.showUI(); } public void showUI(){ MyFrame jf = new MyFrame(); jf.setSize(800,800); jf.setTitle("画图工具"); jf.setLocationRelativeTo(null); jf.setDefaultCloseOperation(3); //添加流式布局。 FlowLayout flow = new FlowLayout(FlowLayout.LEFT);//设置对齐方式为左对齐,在括号里面写。 //上面只是创建这个对象,接下来要set一下。 jf.setLayout(flow); DrawMouse mouse = new DrawMouse(); jf.array = mouse.arr; String a[] = {"直线","圆","矩形","多边形","三角形","曲线","点","精美的图形","分形图形","树","康托尔集","谢宾斯基三角形"}; Color ch[] = {Color.blue,Color.green}; for(int i=0;i<a.length;i++){ javax.swing.JButton bh = new javax.swing.JButton(a[i]); //新建对象?(新建这个按钮?) jf.add(bh); //在jf里面add这个bh bh.addActionListener(mouse); //bh添加动作监听器 } // javax.swing.JButton jbu6 = new javax.swing.JButton("曲线"); // jf.add(jbu6); for(int j=0;j<ch.length;j++){ javax.swing.JButton s = new javax.swing.JButton(); //新建颜色按钮? s.setBackground(ch[j]); //添加背景颜色? s.setPreferredSize(new Dimension(30,30)); //设置颜色按钮的大小 jf.add(s); //jf里面加这个s s.addActionListener(mouse); //给s添加动作监听器 } // javax.swing.JButton blue = new javax.swing.JButton(); //设置按钮背景色 // blue.setBackground(Color.BLUE); // blue.setPreferredSize(new Dimension(30, 30)); // jf.add(blue); // // javax.swing.JButton green = new javax.swing.JButton(); // green.setBackground(Color.GREEN); // green.setPreferredSize(new Dimension(30,30)); // jf.add(green); jf.setVisible(true); //设置可见 Graphics g =jf.getGraphics(); jf.addMouseListener(mouse); mouse.gr = g; jf.addMouseMotionListener(mouse); mouse.gr = g; //上面DrawMouse里面已经定义了一个mouse对象了,这里直接用mouse. //因为DrawMouse同时监听了 MouseListener,ActionListen两种动作。 // for(int j = 0;j < 6;j++){ // b[j].addActionListener(mouse); // } // jbu.addActionListener(mouse); // green.addActionListener(mouse); } }
package drawFrame; import java.awt.Color; import java.awt.Graphics; import java.awt.event.MouseListener; import java.awt.event.MouseMotionListener; import java.util.Random; import javax.swing.JButton; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseEvent; public class DrawMouse implements MouseListener, ActionListener, MouseMotionListener {
public Graphics gr; private int x1, x2, y1, y2; private int m, n, p, q; String ab; Color color; //刚才这里遇到了一个问题,在drawShape里面添加了color参数,但还是用不起来,这是因为,这里定义的color跟下面的*行的color //不是同一个东西,要怎样才能是同一个东西呢?很简单,把*行的Color color = jbu.getBackground();改成 //color = jbu.getBackground();就可以了。 Shape arr[] = new Shape[1000]; int index = 0; public void actionPerformed(ActionEvent e) { if ("".equals(e.getActionCommand())) { JButton jbu = (JButton) e.getSource(); color = jbu.getBackground(); //* gr.setColor(color); }
else { ab = e.getActionCommand(); System.out.println("ab=" + ab); } }
public void mouseDragged(MouseEvent e) { } public void mouseMoved(MouseEvent e) { } public void mouseClicked(MouseEvent e) { } public void mousePressed(MouseEvent e) { if (flag == 1) { //这是画三角形多边形的,与这次重绘无关,不用管 x1 = e.getX(); y1 = e.getY(); } } public void mouseReleased(MouseEvent e) { if (flag == 1) { x2 = e.getX(); y2 = e.getY(); } else if (ab.equals("直线")) { gr.drawLine(x1, y1, x2, y2); Shape shape = new Shape(x1,y1,x2,y2,ab,color); arr[index++] = shape; } else if (ab.equals("圆")) { m = x1 < x2 ? x1 : x2; n = y1 < y2 ? y1 : y2; gr.drawOval(m, n, Math.abs(x2 - x1), Math.abs(y2 - y1)); Shape shape = new Shape(x1,y1,x2,y2,ab,color); arr[index++] = shape; } else if (ab.equals("矩形")) { p = x1 < x2 ? x1 : x2; q = y1 < y2 ? y1 : y2; gr.drawRect(p, q, Math.abs(x2 - x1), Math.abs(y2 - y1)); Shape shape = new Shape(x1,y1,x2,y2,ab,color); arr[index++] = shape; }
} public void mouseEntered(MouseEvent e) { // System.out.println("进入"); } public void mouseExited(MouseEvent e) { // System.out.println("退出"); } }
package drawFrame; import java.awt.Graphics; import javax.swing.JButton; import java.awt.Color; public class Shape { public int x1,y1,x2,y2; String name; Color color; public int m,n; public Shape(int xa,int ya,int xb,int yb,String n,Color co){ x1 = xa;y1 = ya; x2 = xb;y2 = yb; name = n;color = co; } public void drawShape(Graphics g){ m = x1 < x2 ? x1 : x2; n = y1 < y2 ? y1 : y2; switch(name){ case "直线": g.setColor(color); g.drawLine(x1, y1, x2, y2); break; case "圆": g.setColor(color); g.drawOval(m, n, Math.abs(x2 - x1), Math.abs(y2 - y1)); break; case "矩形": g.setColor(color); g.drawRect(m, n, Math.abs(x2 - x1), Math.abs(y2 - y1)); break; } } }
package drawFrame; import java.awt.Graphics; import javax.swing.JFrame; public class MyFrame extends JFrame{ //extends这里是继承的意思,这里用了extends,在DrawFrame的showUI里就可以把JFrame改成MyFrame了 Shape array[] = null; public void paint(Graphics g){ super.paint(g); for(int i = 0;i < array.length;i++){ //array.length表示这个数组的长度 Shape shape = array[i]; if(shape != null){ shape.drawShape(g); } } } }

浙公网安备 33010602011771号