Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 06-28-2007, 05:43 PM
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); } }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 06-29-2007, 04:58 PM
JavaBean's Avatar
Moderator
 
Join Date: May 2007
Posts: 1,272
JavaBean is on a distinguished road
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:

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

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.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 07-01-2007, 08:37 AM
Member
 
Join Date: Jun 2007
Posts: 2
louiebagz is on a distinguished road
Thank you for the inputs...
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
refresh JPanel olesja AWT / Swing 1 04-16-2008 04:58 PM
How can we zoom a map using JSlider barney AWT / Swing 3 01-04-2008 09:49 AM
JPanel won't update ibanez270dx New To Java 2 11-19-2007 05:42 AM
Problem with JPanel ibanez270dx New To Java 2 11-09-2007 06:04 PM
Edit JPanel Text During Runtime...from another class bdn1404 New To Java 5 08-11-2007 04:14 AM


All times are GMT +3. The time now is 10:04 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org