View Single Post
  #1 (permalink)  
Old 06-28-2007, 05:43 PM
louiebagz louiebagz is offline
Member
 
Join Date: Jun 2007
Posts: 2
louiebagz is on a distinguished road
How To:Use a JSlider to adjust Text size in a JPanel
hi guys,

need some help on this.

im working on a program that uses a JSlider... Some texts are rendered on a JPanel... I want to give a zooming in/out effect by using the JSlider to change the size of the text...

I have provided the link for the expected interaction.
http://www.geocities.com/louiebagz/files/jslider.jpg

Below is the code I have...

maybe you can help... thanks!

Code:
/* * TextSlider.java * * Created on June 28, 2007, 7:28 PM * * To change this template, choose Tools | Template Manager * and open the template in the editor. */ import java.awt.*; import java.awt.font.*; import java.awt.geom.Rectangle2D; import javax.swing.*; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JSlider; import javax.swing.SwingConstants; import javax.swing.event.ChangeEvent; import javax.swing.event.ChangeListener; /** * * @author louiebagz */ public class TextSlider extends JFrame{ private TextSpace ssfp; private JSlider zoomSlider; private JScrollPane scrollPane; public TextSlider() { super("Text Processing Demo"); Container container = getContentPane(); container.setLayout(new BorderLayout(5, 5)); ssfp = new TextSpace(); scrollPane = new JScrollPane(ssfp); //container.add(new JScrollPane(imagePanel), BorderLayout.CENTER); container.add(scrollPane, BorderLayout.CENTER); zoomSlider = new JSlider(SwingConstants.VERTICAL, 1, 10, 1); zoomSlider.setMajorTickSpacing(1); zoomSlider.setPaintTicks(true); zoomSlider.addChangeListener(new ChangeListener() { public void stateChanged(ChangeEvent e) { ssfp.revalidate(); repaint(); } }); container.add(zoomSlider, BorderLayout.EAST); pack(); setVisible(true); } private class TextSpace extends JPanel { String[] lines = { "Hello World", "How are you today?", "More text here", "Fine...", "And you???" }; Dimension size = new Dimension(400,240); float x1 = 0; boolean firstTime = true; protected void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D)g; g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); int x0 = 60; int y0 = 40; Font font = g2.getFont().deriveFont(20f); g2.setFont(font); FontRenderContext frc = g2.getFontRenderContext(); if(firstTime) x1 = determineSize(font, frc, x0, y0); float y = y0; for(int j = 0; j < lines.length; j++) { String s = lines[j]; float width = (float)font.getStringBounds(s, frc).getWidth(); LineMetrics lm = font.getLineMetrics(s, frc); float height = lm.getAscent() + lm.getDescent(); float sx = x0; float sy = y + lm.getAscent(); g2.setPaint(Color.black); g2.drawString(s, sx, sy); //g2.setPaint(Color.green); x1 = (x0+width+25 > x1) ? x0+width+25 : x1; //g2.fill(new Rectangle2D.Float(x1, y, width, height)); y +=height; } } private float determineSize(Font font, FontRenderContext frc, int x0, int y0) { Dimension d = new Dimension(); float y = y0; float x = 0; for(int j = 0; j < lines.length; j++) { String s = lines[j]; float width = (float)font.getStringBounds(s, frc).getWidth(); LineMetrics lm = font.getLineMetrics(s, frc); float height = lm.getAscent() + lm.getDescent(); if((2*x0+width) > x) x = 2*x0+width; y +=height; if(x+width+x0 > d.width) d.width = (int)(x+width+x0); if(y+y0> d.height) d.height = (int)(y+y0); } size.width = Math.max(d.width, size.width); size.height = Math.max(d.height, size.height); revalidate(); firstTime = false; return x; } public Dimension getPreferredSize() { return size; } } public static void main(String args[]) { TextSlider app = new TextSlider(); app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }
Reply With Quote
Sponsored Links