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
| public class TestBgFrame {
public static void main(String[] args) {
JFrame frame = new JFrame("INTERNET V.ALPHA");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
class MyPanel extends JPanel {
/**
*
*/
private static final long serialVersionUID = 1L;
final ImageIcon image = new ImageIcon(TestBgFrame.class.getResource("img/background.jpg"));
private Rectangle rectangle;
public void setRectangle(int x, int y, int width, int height) {
this.rectangle=new Rectangle(x,y,width,height);
repaint();
}
public void resetRectangle() {
this.rectangle=null;
repaint();
}
protected void paintComponent(java.awt.Graphics g) {
super.paintComponent(g);
g.drawImage(image.getImage(), 0, 0, getWidth(), getHeight(), 0, 0, image.getIconWidth(), image.getIconHeight(), this);
if ( rectangle!=null ) {
g.setColor(Color.RED);
g.fillRect(evalPercent(getWidth(),rectangle.x), evalPercent(getHeight(),rectangle.y), evalPercent(getWidth(),rectangle.width), evalPercent(getHeight(),rectangle.height));
}
}
private int evalPercent(int size, int percent) {
return (int)(size*percent/100f);
};
};
final MyPanel contenuFenetre = new MyPanel();
contenuFenetre.setLayout(new BorderLayout());
JLabel titre = new JLabel ("INTERNET V.ALPHA",JLabel.CENTER);
titre.setFont(new Font("Arial",Font.BOLD,60));
contenuFenetre.add(titre,BorderLayout.NORTH);
JLabel loading = new JLabel ("LOADING...",JLabel.CENTER);
loading.setFont(new Font("Arial",Font.BOLD,90));
contenuFenetre.add(loading,BorderLayout.CENTER);
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new FlowLayout());
contenuFenetre.add(buttonPanel, BorderLayout.SOUTH);
buttonPanel.add(new JButton(new AbstractAction("Affiche rectangle") {
/**
*
*/
private static final long serialVersionUID = 1L;
public void actionPerformed(ActionEvent e) {
contenuFenetre.setRectangle(10,10,80,20); // 10 % 10 % 80 % 20 %
}
}));
buttonPanel.add(new JButton(new AbstractAction("Cache rectangle") {
/**
*
*/
private static final long serialVersionUID = 1L;
public void actionPerformed(ActionEvent e) {
contenuFenetre.resetRectangle();
}
}));
frame.pack();
frame.setContentPane(contenuFenetre);
frame.setSize(800,600);
frame.setResizable(false);
frame.setVisible(true);
}
} |
Partager