1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
|
package tps;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.geom.Ellipse2D;
import javax.swing.JApplet;
import javax.swing.JPanel;
/**
*
* @author Absil Romain
*/
public class CircleApplet extends JApplet
{
public void init()
{
this.add(new DrawPanel());
}
public void destroy()
{
this.removeAll();
}
public String getAppletInfo()
{
return "Title : CircleDrawing\nAuthor : Absil Romain";
}
}
class DrawPanel extends JPanel implements MouseListener
{
private Point p1;
private Point p2;
private Point p3;
private int count;
public DrawPanel()
{
this.addMouseListener(this);
}
public void mouseClicked(MouseEvent e)
{
Point clicked = new Point(e.getX(), e.getY());
if(count == 0)
p1 = clicked;
else if(count == 1)
p2 = clicked;
else if(count == 2)
p3 = clicked;
count++;
count%=3;
repaint();
}
public void mousePressed(MouseEvent e)
{
}
public void mouseReleased(MouseEvent e)
{
}
public void mouseEntered(MouseEvent e)
{
}
public void mouseExited(MouseEvent e)
{
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
//tracer les points
g2.setColor(Color.BLACK);
if(p1 != null)
g2.drawRect(p1.x,p1.y,1,1);
if(p2 != null)
g2.drawRect(p2.x,p2.y,1,1);
if(p3 != null)
g2.drawRect(p3.x,p3.y,1,1);
//tracer une ligne quand on rajoute un point
g2.setColor(Color.RED);
if(count == 2)
g2.drawLine(p1.x, p1.y, p2.x, p2.y);
if(p1 != null && p2 != null && p3 != null)
{
int x1 = p1.x;
int x2 = p2.x;
int x3 = p3.x;
int y1 = p1.y;
int y2 = p2.y;
int y3 = p3.y;
//tracage du triangle
g2.drawLine(x1,y1,x2,y2);
g2.drawLine(x1,y1,x3,y3);
g2.drawLine(x2,y2,x3,y3);
//calcul de des coeff de dir des médiatrices des droites
//contenant p1p2 et p2p3
double m1 = -(x1 - x2 + 0.0) / (y1 - y2);
double m2 = -(x2 - x3 + 0.0) / (y2 - y3);
//int test = (int)Double.NaN;
//System.out.println(test);
//calcul des milieux de p1p2 et p2p3
int xm1 = (x1 + x2) / 2;
int ym1 = (y1 + y2) / 2;
int xm2 = (x2 + x3) / 2;
int ym2 = (y2 + y3) / 2;
//calcul de l'intersection des droites
int xcenter = (int)( (-m2 * xm2 + ym2 + m1 * xm1 - ym1)
/ (m1 - m2) );
int ycenter = (int)( (-m2 * (m1 * xm1 - ym1) + m1 *
(m2 * xm2 - ym2)) / (m2 - m1) );
//calculdu rayon
int radius = (int)(Math.sqrt( (x1 - xcenter) * (x1 - xcenter)
+ (y1 - ycenter) * (y1 - ycenter)));
//tracer le cercle
g2.setColor(Color.BLACK);
Ellipse2D circle = new Ellipse2D.Double
(xcenter-radius,ycenter-radius,2*radius,2*radius);
g2.draw(circle);
}
//reset si on a trois points
if(count == 0)
{
p1 = null;
p2 = null;
p3 = null;
}
}
} |
Partager