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
| public class DemoDrawOverPanel extends JPanel {
public DemoDrawOverPanel() {
}
@Override
protected void paintChildren(Graphics g) {
Dimension size = getSize();
g.setColor(Color.GREEN);
int width=(int)(Math.min( size.getWidth(), size.getHeight())/4);
int squareWidth=2*width/3;
int squareOffset=(width-squareWidth)/2;
for( int x=0; x<size.getWidth(); x+=width) {
for( int y=0; y<size.getHeight(); y+=width) {
g.fillRect(x+squareOffset, y+squareOffset, squareWidth, squareWidth); // ces carrés seront dessinés derrière
}
}
super.paintChildren(g);
g.setColor(Color.RED);
int circleWidth=width/2;
int circleOffset=(width-circleWidth)/2;
for( int x=0; x<size.getWidth(); x+=width) {
for( int y=0; y<size.getHeight(); y+=width) {
g.fillOval(x+circleOffset, y+circleOffset, circleWidth, circleWidth); // ces cercles seront dessinés devant
}
}
}
private final static int GRIDX=30;
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(Toolkit.getDefaultToolkit().getScreenSize());
frame.setLocationRelativeTo(null);
final DemoDrawOverPanel panel = new DemoDrawOverPanel();
panel.setLayout(new GridLayout(GRIDX, GRIDX));
for(int i=GRIDX*GRIDX;i>0; i--) {
JLabel label = new JLabel("Texte");
panel.add(label);
}
frame.getContentPane().add(panel);
frame.setVisible(true);
}
} |