Java_DrawLine

 
主函数所在类:
  1. package EulerOperation;
  2. import javax.swing.JFrame;
  3. public class MainFunction {
  4.     /**
  5.      * @param args
  6.      */
  7.     public static void main(String[] args) {
  8.         // TODO Auto-generated method stub
  9.         MainFrame aFrame = new MainFrame();
  10.         aFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  11.         aFrame.setVisible(true);
  12.     }
  13. }

框架类:

  1. package EulerOperation;
  2. import java.awt.*;
  3. import javax.swing.*;
  4. public class MainFrame extends JFrame {
  5.     /**
  6.      * 
  7.      */
  8.     private static final long serialVersionUID = 1L;
  9.     public MainFrame()
  10.     {
  11.         this.setTitle("EulerOperation");
  12.         this.setLocation(250,250);
  13.         this.setSize(800, 600);
  14.         
  15.         MainPanel aPanel = new MainPanel();
  16.         Container content = this.getContentPane();
  17.         content.add(aPanel);
  18.     }
  19. }

Panel类:

  1. package EulerOperation;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import java.awt.geom.*;
  5. import javax.swing.*;
  6. import java.util.*;
  7. public class MainPanel extends JPanel {
  8.     
  9.     /**
  10.      * 
  11.      */
  12.     private static final long serialVersionUID = 1L;
  13.     public MainPanel()
  14.     {
  15.     
  16.         startButton = new JButton("Start");
  17.         endButton = new JButton("End");
  18.         repaintButton = new JButton("Repaint");
  19.         
  20.         ButtonAction action = new ButtonAction();
  21.         startButton.addActionListener(action);
  22.         endButton.addActionListener(action);
  23.         repaintButton.addActionListener(action);
  24.         
  25.         this.add(startButton);
  26.         this.add(endButton);
  27.         this.add(repaintButton);
  28.         
  29.         arrayLines = new ArrayList<Line2D.Double>();
  30.         p1 = new Point2D.Double(0.0,0.0);
  31.         p2 = new Point2D.Double(0.0,0.0);
  32.         p3 = new Point2D.Double(0.0,0.0);
  33.         line = new Line2D.Double(p1,p3);
  34.         startPoint = new Point2D.Double(0.0,0.0);
  35.         endPoint = new Point2D.Double(0.0,0.0);
  36.         
  37.         start = false;
  38.         end = false;
  39.         flag = true;
  40.         
  41.         endButton.setEnabled(false);
  42.         repaintButton.setEnabled(false);
  43.         
  44.         this.addMouseListener(new MouseHandler());
  45.         this.addMouseMotionListener(new MouseMotionHandler());
  46.     }
  47.     
  48.     public void paintComponent(Graphics g)
  49.     {
  50.         super.paintComponent(g);
  51.         Graphics2D g2 = (Graphics2D)g;
  52.         
  53.         if(start || end)
  54.         {
  55.             g2.draw(line);
  56.             
  57.             for(int i=0; i<arrayLines.size(); i++)
  58.                 g2.draw(arrayLines.get(i));
  59.             if(end)
  60.                 end = false;
  61.         }
  62.     }
  63.     
  64.     
  65.     private class MouseHandler extends MouseAdapter
  66.     {
  67.         public void mousePressed(MouseEvent e)
  68.         {
  69.             if(start || end)
  70.             {
  71.                 if(flag)
  72.                 {
  73.                     Point2D p = (Point2D)e.getPoint();
  74.                     p1 = p;
  75.                     startPoint = p;
  76.                 }
  77.                 else
  78.                 {
  79.                     p1 = p2;
  80.                 }
  81.             
  82.             }
  83.         }
  84.         
  85.         public void mouseReleased(MouseEvent e)
  86.         {
  87.             if(start || end)
  88.             {
  89.                 Point2D p = (Point2D)e.getPoint();
  90.                 p2 = p;
  91.                 Line2D.Double tmpLine = new Line2D.Double(p1,p2);
  92.                 arrayLines.add(tmpLine);
  93.                 
  94.                 flag = false;
  95.                 endPoint = p;
  96.                 repaint();
  97.             }
  98.         }
  99.     }
  100.     
  101.     private class MouseMotionHandler extends MouseMotionAdapter
  102.     {
  103.         public void mouseDragged(MouseEvent e)
  104.         {
  105.             if(start || end)
  106.             {
  107.                 p3 = e.getPoint();
  108.                 line = new Line2D.Double(p1,p3);
  109.                 repaint();
  110.             }
  111.         }
  112.     }
  113.     
  114.     private class ButtonAction implements ActionListener 
  115.     {
  116.         public void actionPerformed(ActionEvent e)
  117.         {
  118.             if(e.getActionCommand().equals("Start"))
  119.             {
  120.                 start = true;
  121.                 startButton.setEnabled(false);
  122.                 endButton.setEnabled(true);
  123.                 repaintButton.setEnabled(true);
  124.             }
  125.             else if(e.getActionCommand().equals("End"))
  126.             {
  127.                 start = false;
  128.                 end = true;
  129.                 Line2D.Double tmpLine = new Line2D.Double(startPoint,endPoint);
  130.                 arrayLines.add(tmpLine);
  131.                 endButton.setEnabled(false);
  132.                 repaint();
  133.             }
  134.             else if(e.getSource()== repaintButton)
  135.             {
  136.                 start = false;
  137.                 end = false;
  138.                 flag = true;
  139.                 arrayLines.clear();
  140.                 startButton.setEnabled(true);
  141.                 endButton.setEnabled(false);
  142.                 repaint();
  143.             }
  144.         }
  145.     }
  146.     
  147.     private Point2D startPoint;
  148.     private Point2D endPoint;
  149.     private Point2D p1;
  150.     private Point2D p2;
  151.     private Point2D p3;
  152.     private Line2D line;
  153.     private boolean flag;
  154.     private boolean start;
  155.     private boolean end;
  156.     private JButton startButton;
  157.     private JButton endButton;
  158.     private JButton repaintButton;
  159.     private ArrayList<Line2D.Double> arrayLines;
  160.     
  161. }

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值