Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Adding Scrollbars to an image

Status
Not open for further replies.

Addola

Programmer
May 9, 2001
23
SA
hi all,

I am to make an Image viewer and I made so much on it but I faced a problem that I didn't know who to add scrollbars so that when the image is zoomed or is more than the size of the frame two horizntal and vertical scrollbar will apear. I couldn't do that!!. Here is a sample but watch it the file is not there you gotta but one and thhe frame is unclosable(u have to ctrl+alt+del and close it) :)

==============================================
import java.awt.*;

public class ScrollTheImage extends Frame{

Image im = Toolkit.getDefaultToolkit().getImage("anImage.gif");


//constructor
ScrollTheImage() {
super("Addola");
setSize(500,500);
show();
}
public void paint(Graphics g){
g.drawImage(im,50,50,this);
}
public static void main (String[]args){
new ScrollTheImage();
}
}




please find me the easiest way to make it possible.
regards,
Addola
 
how about creating a JScrollPane component.

theoretically if your image goes beyond the size of the scrollpane, scrollbars automatically appear.

As for the program not ending properly it might be because you need to add a window event listener to listen for when the window is closed.
 
Hi Addola,
I'm not good with graphics things in java, but this could be one sollution, I used ScrollPane.
I wasn't able to get Image-dimensions before paint()-method (It first returned -1 for width and height), but maybe you manage to handle it.


import java.awt.*;
public class ScrollingImage extends Frame
{
public static void main(String[] argv)
{
new ScrollingImage();
}

public ScrollingImage()
{
setLayout(new BorderLayout());
ScrollPane sp =
new ScrollPane(ScrollPane.SCROLLBARS_ALWAYS);

Image im = Toolkit.getDefaultToolkit().getImage("banner.gif");
ImageCanvas ic = new ImageCanvas(im);
sp.add(ic);

this.add(sp, BorderLayout.CENTER);
this.setSize(300,300);
this.show();
System.out.println(ic.getWidth());
} // <-- ScrollingImage

class ImageCanvas extends Canvas
{
private Image image;

public ImageCanvas(Image i)
{
image = i;
// If I get the image size here I get w=-1 h=-1 weird...
this.setSize(300,300);
}

public void paint(Graphics g)
{
int w = image.getWidth(this);
int h = image.getHeight(this);

if(w > 0 && h > 0)
{
resize(w,h);
}

if (image != null)
{
g.drawImage(image, 0, 0, this);
}
} // <--paint
} // <-- class ImageCanvas

}

I Hope this helps you.
 
The previous piece of code needs 'some' improvements... ;)
For example it calls resize() each time paint()-method is called.
 
Thenks Vedo I'll try to integrate my application with your code to see what will happen :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top