hi..

the code below are in working condition.. means can be compiled & run.. when drag the scrollbar, the "Welcome To Java" string message will be move left, right or up, down depend on which scrollbar u drag..

just wanna ask if the "Welcome To Java" text be changed to image?? so instead of the text shifting.. its the image tt is moving.. can anyone help me.. ??

import java.applet.*;
import java.awt.*;
import java.awt.event.*;


public class ScrollbarDemo extends Applet implements AdjustmentListener
{
//declare scrollbars
Scrollbar scbHort, scbVert;


//declare a canvas
ScrollMessageCanvas c;


public static void main(String[] args)
{
ScrollbarDemo f = new ScrollbarDemo();


f.setSize(300,200);
f.setVisible(true);
}


public ScrollbarDemo()
{
setLayout(new BorderLayout());


//create scrollbars
scbVert = new Scrollbar();
scbVert.setOrientation(Scrollbar.VERTICAL);
scbHort = new Scrollbar();
scbHort.setOrientation(Scrollbar.HORIZONTAL);


add("Center", c = new ScrollMessageCanvas());
add("East", scbVert);
add("South", scbHort);


//register listener for the scrollbars
scbHort.addAdjustmentListener(this);
scbVert.addAdjustmentListener(this);
}


public void adjustmentValueChanged(AdjustmentEvent e)
{
if(e.getSource() == scbHort)
{
/*getValue() and getMaximumValue() return int, but for better precision, use double*/
double value = scbHort.getValue();
double maximumValue = scbHort.getMaximum();
double newX = (value*c.getSize().width/maximumValue);
c.setX((int)newX);
}


else if(e.getSource() == scbVert)
{
/*getValue() and getMaximumValue() return int, but for better precision, use double*/
double value = scbVert.getValue();
double maximumValue = scbVert.getMaximum();
double newY = (value*c.getSize().height/maximumValue);
c.setY((int)newY);
}
}
}


class ScrollMessageCanvas extends Canvas
{
private int x = 10;
private int y = 20;
private String message = "Welcome to Java!";


public ScrollMessageCanvas()
{
repaint();
}


public void setX(int x)
{
this.x = x;
repaint();
}


public void setY(int y)
{
this.y = y;
repaint();
}


public void paint(Graphics g)
{
g.drawString(message, x, y);
}
}
Member Avatar for iamthwee

Please use code tags. Look at the watermark when you type stuff in.

wads code tag???

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.