日期:2014-05-20 浏览次数:20884 次
import java.awt.*; import javax.swing.*; import java.awt.event.*; class DrawPic extends JFrame implements MouseListener,MouseMotionListener{ int x1,x2,y1,y2; boolean line,circle,rect; static CheckboxGroup cbg ; DrawPic(){ addMouseListener(this); x1 = 0; x2 = 0; y1 = 0; y2 = 0; line = false; circle = false; rect = false; } public void go(){ setLayout(new GridLayout(3,1)); cbg = new CheckboxGroup(); add(new Checkbox("line",cbg,true)); add(new Checkbox("rect",cbg,false)); add(new Checkbox("circle",cbg,false)); } public static void main(String args[]){ DrawPic draw = new DrawPic(); draw.setSize(400,300); //draw.go(); draw.setVisible(true); } public void mouseClicked(MouseEvent e){ System.out.println("Mouse Clicked"); } public void mouseReleased(MouseEvent e){ x2 = e.getX(); y2 = e.getY(); /*if(cbg.getSelectedCheckbox().getLabel().equals("line")) line = true; else if(cbg.getSelectedCheckbox().getLabel().equals("rect")) rect = true; else if(cbg.getSelectedCheckbox().getLabel().equals("circle")) circle = true;*/ repaint(); line = false; circle = false; rect = false; } public void mouseEntered(MouseEvent e){} public void mouseExited(MouseEvent e){} public void mousePressed(MouseEvent e){ x1 = e.getX(); y1 = e.getY(); } public void mouseDragged(MouseEvent e){ } public void mouseMoved(MouseEvent e){} public void paint(Graphics g){ System.out.println("painting.."); line = true; g = this.getContentPane().getGraphics(); if(line) g.drawLine(x1,y1,x2,y2); else if(circle) g.drawOval(x1,y1,x2-x1,y2-y1); else if(rect) g.drawRect(x1,y1,x2-x1,y2-y1); } }
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package test; import java.awt.*; import javax.swing.*; import java.awt.event.*; class DrawPic extends JFrame implements MouseListener,MouseMotionListener{ int x1,x2,y1,y2; boolean line,circle,rect; static CheckboxGroup cbg ; DrawPic(){ addMouseListener(this); x1 = 0; x2 = 0; y1 = 0; y2 = 0; line = false; circle = false; rect = false; setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); } public void go(){ setLayout(new GridLayout(3,1)); JPanel pnl = new JPanel(); add(pnl); cbg = new CheckboxGroup(); pnl.add(new Checkbox("line",cbg,true)); pnl.add(new Checkbox("rect",cbg,false)); pnl.add(new Checkbox("circle",cbg,false)); } public static void main(String args[]){ DrawPic draw = new DrawPic(); draw.setSize(400,300); draw.go(); draw.setVisible(true); } public void mouseClicked(MouseEvent e){ System.out.println("Mouse Clicked"); } public void mouseReleased(MouseEvent e){ x2 = e.getX(); y2 = e.getY(); line = false; circle = false; rect = false; if(cbg.getSelectedCheckbox().getLabel().equals("line")) line = true; else if(cbg.getSelectedCheckbox().getLabel().equals("rect")) rect = true; else if(cbg.getSelectedCheckbox().getLabel().equals("circle")) circle = true; repaint(); } public void mouseEntered(MouseEvent e){} public void mouseExited(MouseEvent e){} public void mousePressed(MouseEvent e){ x1 = e.getX(); y1 = e.getY(); } public void mouseDragged(MouseEvent e){ } public void mouseMoved(MouseEvent e){} public void paint(Graphics g){ System.out.println("painting.."); //line = true; g = this.getContentPane().getGraphics(); if(line) g.drawLine(x1,y1,x2,y2); else if(circle) g.drawOval(x1,y1,x2-x1,y2-y1); else if(rect) g.drawRect(x1,y1,x2-x1,y2-y1); else{ g.drawLine(x1,y1,x2,y2); } } }