[JPanel] Affichage d'une image et scrolling
Salut à tous :D
je suis débutant en java (5 mois de combat) ;)
Je souhaite afficher une image avec Java. La surface représentant l'image étant assez grande, il me faut intégrer des barres de défilement à droite et à la base du conteneur. J'ai pu les intégrer mais je n'ai pas le curseur pour pourvoir défiler en bas et à droite.
Voici mon code :
Code:
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
| import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class ImagePanel extends JPanel {
public ImagePanel() {
image = Toolkit.getDefaultToolkit().getImage("pg1.png");
MediaTracker tracker = new MediaTracker(this);
tracker.addImage(image,0);
try { tracker.waitForID(0); }
catch (InterruptedException e) {}
}
public void paintComponent (Graphics g) {
super.paintComponent(g);
Dimension d = getSize();
int clientWidth = d.width;
int clientHeight = d.height;
int imageWidth = image.getWidth(this);
int imageHeight = image.getHeight(this);
g.drawImage(image,0,0,this);
}
private Image image;
}
class ImageFrame extends JFrame {
public ImageFrame() {
setTitle("Angles d'Euler pour un système cartésien");
setSize(800,800);
Component composantVisualisé = new ImagePanel();
JScrollPane sp = new JScrollPane(composantVisualisé,ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
Container contentPane = getContentPane();
contentPane.add(new ImagePanel());
contentPane.setVisible(true);
contentPane.add(sp,"Center");
}
}
public class ImageTest {
public static void main (String[] args) {
JFrame frame = new ImageFrame();
frame.show();
}
} |
Merci beaucoup pour votre aide ...
++ :D
[ Modéré par Bulbo ]
Ajout d'un tag dans le titre
:arrow: Les Règles du Forum