Results 1 to 3 of 3
  1. #1
    louiebagz is offline Member
    Join Date
    Jun 2007
    Posts
    2
    Rep Power
    0

    Default 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!

    Java 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);
        }
    }

  2. #2
    JavaBean's Avatar
    JavaBean is offline Moderator
    Join Date
    May 2007
    Posts
    1,270
    Rep Power
    10

    Default

    You are not changing the size of the text from your JSlider. You just try to redraw the text. Try changing the size of the font as follows:

    Java Code:
        Font font = g2.getFont().deriveFont(<modified-font-size-changed-by-jslider>);
    instead of:

    Java Code:
        Font font = g2.getFont().deriveFont(20f);
    You can just have a mapping between JSlider position and font size. Based on the position of JSlider, you can assign a new font size. It should work in that case.

  3. #3
    louiebagz is offline Member
    Join Date
    Jun 2007
    Posts
    2
    Rep Power
    0

Similar Threads

  1. How can we zoom a map using JSlider
    By barney in forum AWT / Swing
    Replies: 5
    Last Post: 02-23-2009, 01:48 PM
  2. JPanel won't update
    By ibanez270dx in forum New To Java
    Replies: 3
    Last Post: 01-06-2009, 08:59 PM
  3. refresh JPanel
    By olesja in forum AWT / Swing
    Replies: 1
    Last Post: 04-16-2008, 03:58 PM
  4. Problem with JPanel
    By ibanez270dx in forum New To Java
    Replies: 2
    Last Post: 11-09-2007, 05:04 PM
  5. Replies: 5
    Last Post: 08-11-2007, 03:14 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •