1

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

Edited by pyTony: fixed formatting

2
Contributors
2
Replies
5
Views
10 Years
Discussion Span
Last Post by m0om0o85
0

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

Edited by happygeek: fixed formatting

This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.