A voir également:
- JAVA GeneralPath et MouseListener
- Waptrick java football - Télécharger - Jeux vidéo
- Jeux java itel football - Télécharger - Jeux vidéo
- Java apk - Télécharger - Langages
- Java décompiler - Télécharger - Langages
- Java runtime - Télécharger - Langages
1 réponse
RESOLU :
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 "+ survole + "\n");
return hexa.contains(p.x,p.y - 25);
}
public void Inside()
{
survole = true;
zoneTexte.append("Mouse entered "+ survole + "\n");
repaint();
}
public void Outside()
{
survole = false;
zoneTexte.append("Mouse exited "+ survole + "\n");
repaint();
}
public void paintComponent( Graphics g )
{
super.paintComponent(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(demo);
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();
}
}
}
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 "+ survole + "\n");
return hexa.contains(p.x,p.y - 25);
}
public void Inside()
{
survole = true;
zoneTexte.append("Mouse entered "+ survole + "\n");
repaint();
}
public void Outside()
{
survole = false;
zoneTexte.append("Mouse exited "+ survole + "\n");
repaint();
}
public void paintComponent( Graphics g )
{
super.paintComponent(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(demo);
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();
}
}
}