Scaling/zooming Swing component
- From: HGA03630@xxxxxxxxxxx (hiwa)
- Date: 30 May 2005 19:48:27 -0700
What should be the proper way for scaling/zooming predefined Swing component?
The example code below can't do it for a component, JLabel.
What's wrong with it?
------------------------------------------------------------------------
import java.awt.*;
import javax.swing.*;
public class Ztest {
public Ztest() {
JFrame frame = new JFrame();
ZoomLabel label0 = new ZoomLabel("MargaretGlancedAtHerSister'sNote");
ZoomLabel label1 = new ZoomLabel("AndPushedItOverTheBreakfastTable");
ZoomPanel zoom = new ZoomPanel();
zoom.add(label0);
JScrollPane sp = new JScrollPane(zoom);
frame.getContentPane().add(sp, BorderLayout.CENTER);
JPanel panel = new JPanel();
panel.add(label1);
frame.getContentPane().add(panel, BorderLayout.SOUTH);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBounds(0, 0, 500, 300);
frame.setVisible(true);
}
public static void main(String[] args) {
new Ztest();
}
class ZoomPanel extends JPanel {
double dblScale = 2.0;
int intScale = 2;
Dimension d;
public ZoomPanel() {
d = getPreferredSize();
setPreferredSize
(new Dimension(d.width * intScale, d.height * intScale));
}
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) ((Graphics2D)g).create();
g2.scale(dblScale, dblScale);
super.paintComponent(g2);
g2.dispose();
}
}
class ZoomLabel extends JLabel {
double dblScale = 2.0;
int intScale = 2;
private Dimension d;
public ZoomLabel(String s) {
super(s);
d = getPreferredSize();
setPreferredSize
(new Dimension(d.width * intScale, d.height * intScale));
}
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) ((Graphics2D)g).create();
g2.scale(dblScale, dblScale);
super.paintComponent(g2);
g2.dispose();
}
}
}
---------------------------------------------------------------------
.
- Prev by Date: Re: RTF Encodings
- Next by Date: Changing the order of JInternalFrames
- Previous by thread: RTF Encodings
- Next by thread: Changing the order of JInternalFrames
- Index(es):