Bonjour,
Pour m'amuser et pour experimenté les FileFilter, j'ai écrit un petit programme qui affiche une image choisie via un JFileChooser. Je déssine l'image sur un JPanel (ça marche bien) mais je ne sais pas comment récupérer la taille de l'image. En effet, si elle est trop grande (plus grande que l'écran) je met le JPanel dans un JScrollPane. Cependant, la façon dont je récupère la taille des images est visiblement foireuse. Voici une partie du code :
Voila, les méthodes donnant la taille de l'image sont dans MonPanel. Elles ne fonctionne visiblement pas correctement.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 public class ImageViewer extends JFrame { private String currentImg; private static final int MIN_SIDE = 150; private static final long serialVersionUID = 1L; private static final String TITRE = "Simple image viewer"; private static final int MAX_WIDTH = Toolkit.getDefaultToolkit().getScreenSize().width; private static final int MAX_HEIGH = Toolkit.getDefaultToolkit().getScreenSize().height; public static void main (String[]args) { new ImageViewer(); } public ImageViewer() { super(TITRE); this.currentImg = ""; this.addComponents(); this.definirPropriete(); } ... private void showImage() { if (this.currentImg == null) return; this.getContentPane().removeAll(); MonPanel paneau = new MonPanel(); this.setSize(paneau.getNeededSize()); System.out.println(paneau.needHorizontalScrolling() || paneau.needVerticalScrolling()); if (paneau.needHorizontalScrolling() || paneau.needVerticalScrolling()) this.add(this.getScrollPane(paneau)); else this.add(paneau); this.setVisible(true); } private JScrollPane getScrollPane(MonPanel paneau) { JScrollPane pane = new JScrollPane(paneau); JScrollBar bare = new JScrollBar(); bare.setUnitIncrement(35); if (paneau.needVerticalScrolling()) pane.setVerticalScrollBar(bare); if (paneau.needHorizontalScrolling()) pane.setHorizontalScrollBar(bare); return pane; } private void definirPropriete() { this.setSize(300,400); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setLocationRelativeTo(this); this.setVisible(true); } private class MonPanel extends JPanel { private Image toDraw; private static final long serialVersionUID = 1L; public MonPanel() { this.toDraw = Toolkit.getDefaultToolkit().getImage(currentImg); } private Dimension getNeededSize() { int width = this.toDraw.getWidth(this); int height = this.toDraw.getHeight(this); return new Dimension(width < MIN_SIDE ? MIN_SIDE : width, height < MIN_SIDE ? MIN_SIDE : height); } private boolean needVerticalScrolling() { return toDraw.getHeight(ImageViewer.this) > MAX_HEIGH; } private boolean needHorizontalScrolling() { return toDraw.getWidth(ImageViewer.this) > MAX_WIDTH; } protected void paintComponent(Graphics g) { super.paintComponent(g); int x = (this.getWidth() - toDraw.getWidth(this))/ 2; int y = (this.getHeight() - toDraw.getHeight(this))/ 2; g.drawImage(toDraw, x, y, this); } } }
Partager