Ellipse

  • Uploaded by: Hoàng Hải
  • 0
  • 0
  • November 2019
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Ellipse as PDF for free.

More details

  • Words: 273
  • Pages: 3
import java.awt.*; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.event.MouseMotionAdapter; import java.awt.geom.CubicCurve2D; import java.awt.geom.Ellipse2D; import java.util.Vector; import javax.swing.JComponent; import javax.xml.ws.Endpoint; public class ViewPane extends JComponent{ final int MAX_POINTS = 20; final int POINT_RADIUS =4; private Point[] endPoints = new Point[MAX_POINTS]; private int num = 0; private Point selectedpoint, secondPoint; private Boolean showpoint = true; public ViewPane(){ addMouseListener(new MouseHandel()); addMouseMotionListener(new MouseMotionHandel()); addKeyListener(new Keyhander()); setFocusable(true); } public static final Color[] PAINT = { Color.blue, Color.red, Color.black }; private int colorPaintIndex = 0; protected void paintComponent(Graphics g){ super.paintComponent(g); Graphics2D g2D=(Graphics2D)g; g2D.setPaint(PAINT[colorPaintIndex]); for(int i=0;i < num -1; i+= 2) { Point p1 = endPoints[i], p2= endPoints[i+1]; Ellipse2D.Double a = new Ellipse2D.Double(p1.x, p1.y, p2.x p1.x, p2.y-p1.y); g2D.draw(a); g2D.fill(a); } if(showpoint)

{ for(int i =0; i< num;i++) { Point p = endPoints[i]; g2D.drawOval(p.x - POINT_RADIUS,p.yPOINT_RADIUS,2*POINT_RADIUS,2*POINT_RADIUS); } } } private class MouseHandel extends MouseAdapter{ public void mousePressed(MouseEvent e){ if(e.getButton()== MouseEvent.BUTTON1){ if(num < MAX_POINTS){ endPoints[num++]= e.getPoint(); repaint(); } } else if(e.getButton()== MouseEvent.BUTTON3){ Point mousepoint = e.getPoint(); selectedpoint = secondPoint = null; for(int i=0; i< num; i++){ if(endPoints[i].distance(mousepoint) <= POINT_RADIUS){ selectedpoint = endPoints[i]; if(e.isControlDown()){ i=(i%2==0)? i+1:i-1; selectedpoint = endPoints[i]; } return ; } } } } } private class MouseMotionHandel extends MouseMotionAdapter{ public void mouseDragged(MouseEvent e){ if(showpoint){ if(selectedpoint != null){ if(e.isControlDown() && secondPoint != null){ int dx=e.getX() - selectedpoint.x, dy =e.getY()-selectedpoint.y; selectedpoint.translate(dx, dy); secondPoint.translate(dx, dy); }

else selectedpoint.move(e.getX(), e.getY()); repaint(); } } } } private class Keyhander extends KeyAdapter{ public void keyPressed (KeyEvent e){ switch(e.getKeyCode()) { case KeyEvent.VK_F1: showpoint =! showpoint; repaint(); break; case KeyEvent.VK_F2: colorPaintIndex = (colorPaintIndex+1)% PAINT.length; repaint(); break; } } public void keyTyped(KeyEvent e){ char inputChar = e.getKeyChar(); if(inputChar == '\b'){ } } } }

Related Documents

Ellipse
April 2020 3
Ellipse
November 2019 1
Ellipse
November 2019 1
Ellipse-22
July 2019 23
The Gravitational Ellipse
December 2019 0
Ellipse Construction Method
November 2019 0

More Documents from ""