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
| public class AbsoluteImagePanel extends JPanel {
private final Timer timer;
private double angle;
public AbsoluteImagePanel() {
super();
setLayout(null);
timer = new Timer(30, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
moveImages();
}
});
}
private final static double ANGLE = Math.PI/20;
private final static double PIx2 = 2*Math.PI;
private void moveImages() {
Point point = getMousePosition();
if ( point==null ) {
point = MouseInfo.getPointerInfo().getLocation();
SwingUtilities.convertPointFromScreen(point, this);
}
double cx = getWidth()/2d;
double cy = getHeight()/2d;
double speed = Point.distance(cx, cy, point.x, point.y)/50;
angle+=ANGLE*speed;
if ( angle>PIx2 ) angle -= PIx2;
for(Component component : getComponents()) {
Rectangle bounds = component.getBounds();
double x = bounds.getCenterX();
double y = bounds.getCenterY();
double dist = Point.distance(x, y, cx, cy);
x = cx + dist*Math.sin(angle);
y = cy + dist*Math.cos(angle);
component.setLocation((int)(x-bounds.getWidth()/2),(int)(y-bounds.getHeight()/2));
}
}
public void startAnimation() {
timer.start();
}
public void addImage(Image image, int x, int y, int width, int height) {
JLabel label = new JLabel(new ImageIcon(image));
label.setBounds(x, y, width, height);
add(label);
setComponentZOrder(label, 0); // pour que le dernier s'affiche au dessus
}
@Override
public void paint(Graphics g) {
super.paint(g);
g.setColor(Color.RED);
g.setFont(getFont().deriveFont(Font.BOLD));
if ( getLayout()==null ) {
g.drawString("Pas de layout manager", 5 , g.getFontMetrics().getHeight() );
}
else {
g.drawString("Avec " + getLayout().getClass().getSimpleName(), 5, g.getFontMetrics().getHeight());
}
}
public static void main(String[] args) throws IOException {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final AbsoluteImagePanel panel = new AbsoluteImagePanel();
frame.getContentPane().add(panel);
final Random random = new Random();
final Image image = ImageIO.read(new File("suricate.png"));
for(int i=0; i<10; i++) {
addImage( panel, image, random );
}
panel.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
switch(e.getButton()) {
case MouseEvent.BUTTON1:
addImage( panel , image, random );
panel.repaint();
break;
case MouseEvent.BUTTON3:
if ( panel.getLayout()==null ) {
panel.setLayout(new FlowLayout());
panel.revalidate();
panel.repaint();
}
else {
panel.setLayout(null);
for(Component component : panel.getComponents()) {
Rectangle bounds = randomBounds(random);
component.setBounds(bounds);
}
panel.repaint();
}
break;
}
}
});
frame.setSize(300, 200);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
panel.startAnimation();
}
private static void addImage(AbsoluteImagePanel panel, Image image, Random random) {
Rectangle rectangle = randomBounds(random);
panel.addImage(image, rectangle.x, rectangle.y, rectangle.width, rectangle.height);
}
private static Rectangle randomBounds(Random random) {
int x = random.nextInt(200);
int y = random.nextInt(150);
int width = 10 + random.nextInt(90);
int height = 10 + random.nextInt(90);
return new Rectangle(x, y, width, height);
}
} |
Partager