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 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
| import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.RenderingHints;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.AffineTransform;
import java.awt.geom.Area;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Point2D;
import java.awt.image.BufferStrategy;
import java.util.Objects;
import javax.swing.JWindow;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
public class Bouboule {
/*
private static final int SIZE = 100;
private static final int EYE_SIZE = 20;
private static final int PUPIL_SIZE = 10;
*/
private static final int SIZE = 500;
private static final int EYE_SIZE = 100;
private static final int PUPIL_SIZE = 30;
public static void main(String[] args) {
class BoubouleEyes {
private Point mousepoint;
public boolean setMouse(Point location) {
if ( Objects.equals(location, mousepoint) ) return false;
mousepoint = location;
return true;
}
public void drawEye(Graphics2D g2d, int x, int y, int d) {
g2d.setColor(Color.CYAN);
g2d.fillOval(x, y, d, d);
g2d.setColor(Color.BLACK);
g2d.drawOval(x, y, d, d);
double r = d/2d;
double xc = x+r;
double yc = y+r;
double xd = mousepoint.x - xc;
double yd = mousepoint.y - yc;
double dist = mousepoint.distance(new Point2D.Double(xc,yc));
double cos = dist==0?1:xd/dist;
double sin = dist==0?0:yd/dist;
g2d.setColor(Color.BLACK);
double rr = Math.min(dist, Math.max(0, r-PUPIL_SIZE/2));
double xe = xc + cos * rr - PUPIL_SIZE/2;
double ye = yc + sin * rr - PUPIL_SIZE/2;
g2d.fillOval((int)xe, (int)ye, PUPIL_SIZE, PUPIL_SIZE);
}
}
BoubouleEyes boubouleEyes = new BoubouleEyes() {
};
Area mouth = new Area(new Ellipse2D.Double(0,0,SIZE/3d,SIZE/5d));
double dec = SIZE/20d;
mouth.subtract(new Area(new Ellipse2D.Double(0,-dec,SIZE/3d-dec,SIZE/5d-dec)));
AffineTransform mouthPosition = AffineTransform.getTranslateInstance((SIZE-mouth.getBounds().getWidth())/2, SIZE-mouth.getBounds().getHeight()*2);
Area nose = new Area(new Ellipse2D.Double(0,0,SIZE/10d,SIZE/12d));
dec = SIZE/80d;
nose.subtract(new Area(new Ellipse2D.Double(0,-dec,SIZE/10d,SIZE/12d-dec)));
AffineTransform nosePosition = AffineTransform.getTranslateInstance((SIZE-nose.getBounds().getWidth())/2, SIZE/2d+nose.getBounds2D().getHeight()/4);
JWindow window = new JWindow() {
private static final int E4 = EYE_SIZE/4;
@Override
public void paint(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g.setColor(Color.RED);
g.fillOval(0, 0, SIZE, SIZE);
int y = getHeight()/2-EYE_SIZE;
boubouleEyes.drawEye(g2d, E4, y, EYE_SIZE);
boubouleEyes.drawEye(g2d, getWidth()-E4-EYE_SIZE, y, EYE_SIZE);
g2d.setStroke(new BasicStroke(5));
g.setColor(Color.BLACK);
g.drawOval(0, 0, SIZE, SIZE);
AffineTransform transform = g2d.getTransform();
g2d.transform(nosePosition);
g2d.fill(nose);
g2d.setTransform(transform);
g2d.transform(mouthPosition);
g2d.draw(mouth);
}
};
Area windowShape = new Area(new Ellipse2D.Double(0,0,SIZE,SIZE));
windowShape.subtract(new Area(mouthPosition.createTransformedShape(mouth)));
window.setShape(windowShape);
window.setOpacity(0.5f);
window.setSize(SIZE,SIZE);
window.setAlwaysOnTop(true);
window.setLocationRelativeTo(null);
window.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
System.exit(0);
}
});
boubouleEyes.setMouse(MouseInfo.getPointerInfo().getLocation());
window.setVisible(true);
window.createBufferStrategy(2);
BufferStrategy strategy = window.getBufferStrategy();
Timer timer = new Timer(33, e-> {
Point point = MouseInfo.getPointerInfo().getLocation();
SwingUtilities.convertPointFromScreen(point, window);
if ( boubouleEyes.setMouse(point) ) {
do {
Graphics graphics = strategy.getDrawGraphics();
window.paint(graphics);
graphics.dispose();
} while (strategy.contentsRestored());
strategy.show();
}
});
timer.start();
}
} |
Partager