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
|
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.geom.*;
import java.awt.image.*;
public class polyML4 extends JPanel
{
private Graphics2D g2D;
private GeneralPath hexa = new GeneralPath();
private BasicStroke contour = new BasicStroke(4);
private static TextArea zoneTexte;
private JFrame frame;
private boolean survole;
public polyML4()
{
survole = false;
int[][] xy = new int[2][6];
int first_x = 200;
int first_y = 200;
int cote = 100;
xy = coordonnees(first_x, first_y, cote);
hexa.moveTo(xy[0][0], xy[1][0]);
hexa.lineTo(xy[0][1], xy[1][1]);
hexa.lineTo(xy[0][2], xy[1][2]);
hexa.lineTo(xy[0][3], xy[1][3]);
hexa.lineTo(xy[0][4], xy[1][4]);
hexa.lineTo(xy[0][5], xy[1][5]);
hexa.closePath();
}
int[][] coordonnees(int x, int y, int cote)
{
int[][] xy = new int[2][6];
xy[0][0] = x;
xy[1][0] = y;
xy[0][1] = xy[0][0];
xy[1][1] = xy[1][0] - cote;
xy[0][2] = xy[0][1] - (int)( Math.cos(60) * cote );
xy[1][2] = xy[1][1] + (int)( Math.sin(60) * cote );
xy[0][3] = xy[0][2] - (int)( Math.cos(60) * cote );
xy[1][3] = xy[1][1];
xy[0][4] = xy[0][3];
xy[1][4] = xy[1][0];
xy[0][5] = xy[0][4] + (int)( Math.cos(60) * cote );
xy[1][5] = xy[1][4] - (int)( Math.sin(60) * cote );
return xy;
}
public boolean IsInside(Point p)
{
zoneTexte.append("Test point\n");
return hexa.contains(p.x,p.y);
}
public void Inside()
{
zoneTexte.append("Mouse entered\n");
survole = true;
//this.repaint();
}
public void Outside()
{
zoneTexte.append("Mouse exited\n");
survole = false;
//this.repaint();
}
public void paintComponent( Graphics g )
{
g2D = (Graphics2D) g;
zoneTexte.append("paintComponent\n");
g2D.setColor(Color.WHITE);
g2D.fill(hexa);
if(survole)
{
g2D.setColor(Color.BLUE);
}
else
{
g2D.setColor(Color.BLACK);
}
g2D.setStroke(contour);
g2D.draw(hexa);
}
public static void main(String[] args)
{
polyML4 demo = new polyML4();
Selector selector = new Selector(demo);
zoneTexte = new TextArea(5, 20);
zoneTexte.setEditable(false);
JFrame frame = new JFrame();
frame.addMouseMotionListener(selector);
frame.getContentPane().add(new polyML4());
frame.getContentPane().add("South",zoneTexte);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600,600);
frame.setVisible(true);
}
}
class Selector extends MouseMotionAdapter
{
polyML4 polyML4;
Point target;
public Selector(polyML4 pol)
{
polyML4 = pol;
target = new Point();
}
public void mouseMoved(MouseEvent e)
{
target = e.getPoint();
if(polyML4.IsInside(target))
{
polyML4.Inside();
}
else
{
polyML4.Outside();
}
}
} |
Partager