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
| public class TestPane extends JPanel {
public TestPane() {
super(true);
setPreferredSize(new Dimension(300, 300));
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D)g;
final double xm = getWidth()/2d;
final double ym = getHeight()/2d;
g.setColor(Color.LIGHT_GRAY);
g.drawOval(0, 0, getWidth(), getHeight());
g2d.setColor(Color.RED);
{
final Path2D path = new Path2D.Double();
path.moveTo(0,ym);
path.lineTo(10, ym-10);
path.lineTo(10, ym+10);
path.closePath();
g2d.fill(path);
}
{
final Path2D path = new Path2D.Double();
path.moveTo(getWidth(),ym);
path.lineTo(getWidth()-10, ym-10);
path.lineTo(getWidth()-10, ym+10);
path.closePath();
g2d.fill(path);
}
{
final Path2D path = new Path2D.Double();
path.moveTo(xm,0);
path.lineTo(xm-10, 10);
path.lineTo(xm+10, 10);
path.closePath();
g2d.fill(path);
}
{
final Path2D path = new Path2D.Double();
path.moveTo(xm,getHeight());
path.lineTo(xm-10, getHeight()-10);
path.lineTo(xm+10, getHeight()-10);
path.closePath();
g2d.fill(path);
}
}
public static void main(String[] args) {
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.add(new JScrollPane(new TestPane()));
frame.setSize(200, 200);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
} |
Partager