主函数所在类:
- package EulerOperation;
- import javax.swing.JFrame;
- public class MainFunction {
- /**
- * @param args
- */
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- MainFrame aFrame = new MainFrame();
- aFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- aFrame.setVisible(true);
- }
- }
框架类:
- package EulerOperation;
- import java.awt.*;
- import javax.swing.*;
- public class MainFrame extends JFrame {
- /**
- *
- */
- private static final long serialVersionUID = 1L;
- public MainFrame()
- {
- this.setTitle("EulerOperation");
- this.setLocation(250,250);
- this.setSize(800, 600);
- MainPanel aPanel = new MainPanel();
- Container content = this.getContentPane();
- content.add(aPanel);
- }
- }
Panel类:
- package EulerOperation;
- import java.awt.*;
- import java.awt.event.*;
- import java.awt.geom.*;
- import javax.swing.*;
- import java.util.*;
- public class MainPanel extends JPanel {
- /**
- *
- */
- private static final long serialVersionUID = 1L;
- public MainPanel()
- {
- startButton = new JButton("Start");
- endButton = new JButton("End");
- repaintButton = new JButton("Repaint");
- ButtonAction action = new ButtonAction();
- startButton.addActionListener(action);
- endButton.addActionListener(action);
- repaintButton.addActionListener(action);
- this.add(startButton);
- this.add(endButton);
- this.add(repaintButton);
- arrayLines = new ArrayList<Line2D.Double>();
- p1 = new Point2D.Double(0.0,0.0);
- p2 = new Point2D.Double(0.0,0.0);
- p3 = new Point2D.Double(0.0,0.0);
- line = new Line2D.Double(p1,p3);
- startPoint = new Point2D.Double(0.0,0.0);
- endPoint = new Point2D.Double(0.0,0.0);
- start = false;
- end = false;
- flag = true;
- endButton.setEnabled(false);
- repaintButton.setEnabled(false);
- this.addMouseListener(new MouseHandler());
- this.addMouseMotionListener(new MouseMotionHandler());
- }
- public void paintComponent(Graphics g)
- {
- super.paintComponent(g);
- Graphics2D g2 = (Graphics2D)g;
- if(start || end)
- {
- g2.draw(line);
- for(int i=0; i<arrayLines.size(); i++)
- g2.draw(arrayLines.get(i));
- if(end)
- end = false;
- }
- }
- private class MouseHandler extends MouseAdapter
- {
- public void mousePressed(MouseEvent e)
- {
- if(start || end)
- {
- if(flag)
- {
- Point2D p = (Point2D)e.getPoint();
- p1 = p;
- startPoint = p;
- }
- else
- {
- p1 = p2;
- }
- }
- }
- public void mouseReleased(MouseEvent e)
- {
- if(start || end)
- {
- Point2D p = (Point2D)e.getPoint();
- p2 = p;
- Line2D.Double tmpLine = new Line2D.Double(p1,p2);
- arrayLines.add(tmpLine);
- flag = false;
- endPoint = p;
- repaint();
- }
- }
- }
- private class MouseMotionHandler extends MouseMotionAdapter
- {
- public void mouseDragged(MouseEvent e)
- {
- if(start || end)
- {
- p3 = e.getPoint();
- line = new Line2D.Double(p1,p3);
- repaint();
- }
- }
- }
- private class ButtonAction implements ActionListener
- {
- public void actionPerformed(ActionEvent e)
- {
- if(e.getActionCommand().equals("Start"))
- {
- start = true;
- startButton.setEnabled(false);
- endButton.setEnabled(true);
- repaintButton.setEnabled(true);
- }
- else if(e.getActionCommand().equals("End"))
- {
- start = false;
- end = true;
- Line2D.Double tmpLine = new Line2D.Double(startPoint,endPoint);
- arrayLines.add(tmpLine);
- endButton.setEnabled(false);
- repaint();
- }
- else if(e.getSource()== repaintButton)
- {
- start = false;
- end = false;
- flag = true;
- arrayLines.clear();
- startButton.setEnabled(true);
- endButton.setEnabled(false);
- repaint();
- }
- }
- }
- private Point2D startPoint;
- private Point2D endPoint;
- private Point2D p1;
- private Point2D p2;
- private Point2D p3;
- private Line2D line;
- private boolean flag;
- private boolean start;
- private boolean end;
- private JButton startButton;
- private JButton endButton;
- private JButton repaintButton;
- private ArrayList<Line2D.Double> arrayLines;
- }