Help: JSlider to adjust Text on JPanel

hi guys, I'd like to ask for some help... I want to use a JSlider to adjust the size of a text that is rendered on a JPanel (to give a zooming in / out effect) ... please give inputs... thanks
[221 byte] By [_louiebagza] at [2007-11-27 9:01:34]
# 1

"How to Use Sliders":

http://java.sun.com/docs/books/tutorial/uiswing/components/slider.html

If you need further help then you need to create a "Short, Self Contained, Compilable and Executable, Example Program (SSCCE)",

see http://homepage1.nifty.com/algafield/sscce.html,

that demonstrates the incorrect behaviour, because I can't guess exactly what you are doing based on the information provided.

Don't forget to use the "Code Formatting Tags",

see http://forum.java.sun.com/help.jspa?sec=formatting,

so the posted code retains its original formatting.

camickra at 2007-7-12 21:31:18 > top of Java-index,Desktop,Core GUI APIs...
# 2
i have provided a link so you can see the expected output. http://www.geocities.com/louiebagz/files/jslider.jpgthanks...
_louiebagza at 2007-7-12 21:31:18 > top of Java-index,Desktop,Core GUI APIs...
# 3
That's not how it usually goes. You start doing it yourself, and if you get stuck, you ask a specific question.
kirillga at 2007-7-12 21:31:18 > top of Java-index,Desktop,Core GUI APIs...
# 4

guys,

this is the code i have currently made... currently the JSlider doesn't work as expected...

please help...

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

}

}

_louiebagza at 2007-7-12 21:31:18 > top of Java-index,Desktop,Core GUI APIs...