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 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
| import java.awt.Color;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.Rectangle2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRootPane;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
public class Rapporteur extends JFrame {
private static int POINT_WIDTH = 3;
private static int MARGIN = 35;
private static int SPACING = 10;
public static void main(String[] args) {
new Rapporteur();
}
public Rapporteur() {
setUndecorated(true);
setTitle("Rapporteur");
getRootPane().setWindowDecorationStyle(JRootPane.FRAME);
getContentPane().add(new GradePane());
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBackground(createTransparentColor(Color.BLACK, 0f));
setSize(300,300);
setLocationRelativeTo(null);
setVisible(true);
}
private static Color createTransparentColor(Color color, float transparency) {
return new Color(color.getRed(), color.getGreen(), color.getBlue(), (int)(transparency*255));
}
private static class GradePane extends JPanel {
private Point mousePoint;
public GradePane() {
setBackground(createTransparentColor(Color.ORANGE,0.3f));
MouseAdapter mouseAdapter = new MouseAdapter() {
@Override
public void mouseDragged(MouseEvent e) {
mousePoint = null;
repaintWindow();
}
@Override
public void mouseMoved(MouseEvent e) {
mousePoint = e.getPoint();
repaintWindow();
}
@Override
public void mouseExited(MouseEvent e) {
mousePoint = null;
repaintWindow();
}
};
addMouseListener(mouseAdapter);
addMouseMotionListener(mouseAdapter);
}
protected void repaintWindow() {
SwingUtilities.getWindowAncestor(this).repaint();
}
@Override
public void paint(Graphics g) {
super.paint(g);
Rectangle bounds = getBounds();
g.setColor(Color.BLACK);
Point center = new Point((int)bounds.getCenterX(), (int)bounds.getCenterY());
g.drawLine(center.x, MARGIN, center.x, getHeight()-MARGIN);
int height = getHeight()/2-MARGIN;
for(int i=0; i<height; i+=SPACING) {
g.drawLine(center.x-POINT_WIDTH, center.y+i, center.x+POINT_WIDTH, center.y+i);
g.drawLine(center.x-POINT_WIDTH, center.y-i, center.x+POINT_WIDTH, center.y-i);
}
g.drawLine(MARGIN, center.y, getWidth()-MARGIN, center.y);
int width = getWidth()/2-MARGIN;
for(int i=0; i<width; i+=SPACING) {
g.drawLine(center.x+i, center.y-POINT_WIDTH, center.x+i, center.y+POINT_WIDTH);
g.drawLine(center.x-i, center.y-POINT_WIDTH, center.x-i, center.y+POINT_WIDTH);
}
drawString((Graphics2D)g, "400", new Rectangle(0,0,getWidth(),MARGIN-1), SwingConstants.CENTER, SwingConstants.BOTTOM);
drawString((Graphics2D)g, "100", new Rectangle(getWidth()-MARGIN+1,0,MARGIN-1,getHeight()), SwingConstants.LEFT, SwingConstants.CENTER);
drawString((Graphics2D)g, "200", new Rectangle(0,getHeight()-MARGIN-1,getWidth(),MARGIN-1), SwingConstants.CENTER, SwingConstants.TOP);
drawString((Graphics2D)g, "300", new Rectangle(0,0,MARGIN-1,getHeight()), SwingConstants.RIGHT, SwingConstants.CENTER);
if ( mousePoint!=null ) {
// éventuellement il faudrait projeter le point sur la bordure de la fenêtre
drawLine(g, center, mousePoint, getWidth(), getHeight());
//g.drawLine(center.x, center.y, mousePoint.x, mousePoint.y);
}
}
private void drawLine(Graphics g, Point c, Point p, int width, int height) {
// y = c.y + ((p.y - c.y)/(p.x - c.x))*(x - c.x)
// y = ((x-c.x)/(p.x-c.x)) * (p.y-c.y) + c.y;
if ( p.x==c.x ) {
int y;
if ( p.y<c.y ) {
y = 0;
}
else {
y = height;
}
g.drawLine(c.x, c.y, c.x, y);
}
else if ( p.y==c.y ) {
int x;
if ( p.x<c.x ) {
x = 0;
}
else {
x = width;
}
g.drawLine(c.x, c.y, x, c.y);
}
else if ( p.x> c.x ){
int x = width;
int y = ((x-c.x)/(p.x-c.x)) * (p.y-c.y) + c.y;
g.drawLine(c.x, c.y, x, y);
}
else {
int x = 0;
int y = ((x-c.x)/(p.x-c.x)) * (p.y-c.y) + c.y;
g.drawLine(c.x, c.y, x, y);
}
}
private static void drawString(Graphics2D g2d, String string, Rectangle bounds, int halign, int valign) {
FontMetrics fontMetrics = g2d.getFontMetrics();
Rectangle2D textBounds = fontMetrics.getStringBounds(string, g2d);
double textWidth = textBounds.getWidth();
double x,y;
switch (halign) {
case SwingConstants.LEFT:
x = bounds.getX();
break;
case SwingConstants.RIGHT:
x = bounds.getX() + bounds.getWidth() - textWidth;
break;
case SwingConstants.CENTER:
default:
x = bounds.getX()
+ (bounds.getWidth() - textWidth) / 2;
break;
}
switch (valign) {
case SwingConstants.TOP:
y = bounds.getY() + fontMetrics.getAscent() - fontMetrics.getDescent();
break;
case SwingConstants.BOTTOM:
y = bounds.getY()
+ bounds.getHeight() - fontMetrics.getDescent() ;
break;
case SwingConstants.CENTER:
default:
y = bounds.getY()
+ bounds.getHeight()/ 2 - ( ( fontMetrics.getAscent() + fontMetrics.getDescent() ) / 2 ) + fontMetrics.getAscent() ;
break;
}
g2d.drawString(string, (int)x, (int)y);
}
}
} |
Partager