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
|
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
/**
*
* @author Stéphane D.
*/
public class MCD4 extends JPanel{
/*---------------------------------------*/
/*- c r e a t e A n d S h o w G U I ( ) -*/
/*---------------------------------------*/
private static void createAndShowGUI() {
JFrame frame = new JFrame("MCD4");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Dimesion de la frame.
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
//Create and set up the content pane.
// JComponent newContentPane = new MCD4();
// newContentPane.setOpaque(true);
// frame.setContentPane(newContentPane);
//Ajoute l'espace client
MCD4JPanel jpnl1 = new MCD4JPanel();
jpnl1.setBorder(new LineBorder(Color.red));
jpnl1.setBackground(Color.black);
jpnl1.setPreferredSize(new Dimension(2000, 2000));
//Associe l'espace client avec le JScrollPane.
JScrollPane jscrlp = new JScrollPane(jpnl1);
jscrlp.setAutoscrolls(true);
jscrlp.setPreferredSize(new Dimension(screen.width, screen.height));
jscrlp.getViewport().setPreferredSize(new Dimension(screen.width, screen.height));
//newContentPane.add(jscrlp);
frame.setContentPane(jscrlp);
frame.pack();
frame.setVisible(true);
}
/*---------------------*/
/*- M a i n ( . . . ) -*/
/*---------------------*/
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
/*-----------------------------------*/
/*- C l a s s M C D 4 J P a n e l -*/
/*-----------------------------------*/
class MCD4JPanel extends JPanel {
public void paintComponent(Graphics g) {
//Pour que JPanel soit paint de la couleur définit
//par la méthode setBackground(...).
super.paintComponent(g);
//g2d servira à dessiner la graduation.
Graphics2D g2d = (Graphics2D) g;
Dimension d = getSize();
// Graduation Vertical.
for(int i=0; i<d.getHeight(); i++) {
if( ((i+1) % 100) == 0) { //Ecrire rn rouge les centaines.
g2d.setColor(Color.red);
g2d.drawLine(20, i, 60, i);
g2d.drawString(""+(i+1), 80, i);
}
else if( ((i+1) % 50) == 0) { //Ecrire en bleu les ciquantaines.
g2d.setColor(Color.white);
g2d.drawLine(20, i, 30, i);
g2d.drawString(""+(i+1), 50, i);
}
}
// Graduation horizontal.
// pas de graduation puisqu'il n'y a pas d'ascensseur horizontal
// qui apparait. why?
g2d.setColor(Color.orange);
g2d.drawString("d.getWidth : " + d.getWidth(), 400, 400);
}
} |
Partager