Fri Feb 08, 2013 8:40 pm
import java.awt.*;
import java.applet.*;
import java.net.URL;
class PanelWithScrollable extends Canvas {
Image imgObj;
int paddingX = 0;
int paddingY = 0;
Dimension dimObj;
PanelWithScrollable(Image img, Dimension prefSize) {
imgObj = img;
dimObj = prefSize;
}
public Dimension minimumSize() {
return new Dimension(10, 10);
}
public Dimension dimObj() {
return dimObj;
}
public void paint(Graphics g) {
g.translate(-paddingX, -paddingY);
g.drawImage(imgObj, 0, 0, getBackground(), this);
}
}
public class ImageScroller extends Applet {
Scrollbar verticalScroll;
Scrollbar horziontalScroll;
PanelWithScrollable canvas;
boolean startApplet = true;
String imgObjFile = "../src/image.gif";
Dimension imgObjSize = new Dimension(720, 420);
Dimension preferredImageSize = new Dimension(400, 200);
//This method assumes this Applet is visible.
public void init() {
Image img;
if (startApplet) {
img = getImage(getCodeBase(), imgObjFile);
} else {
img = Toolkit.getDefaultToolkit().getImage(imgObjFile);
}
canvas = new PanelWithScrollable(img, preferredImageSize);
//Create horizontal scrollbar.
horziontalScroll = new Scrollbar(Scrollbar.HORIZONTAL);
//Create vertical scrollbar.
verticalScroll = new Scrollbar(Scrollbar.VERTICAL);
//Add Components to the Applet.
setLayout(new BorderLayout());
add("Center", canvas);
add("East", verticalScroll);
add("South", horziontalScroll);
validate();
//Now that we've validated, then assuming this Applet is
//visible, the canvas size is valid and we can adjust the
//scrollbars to match the image area. [CHECK]
changeSizeX();
changeSizeY();
}
public boolean handleEvent(Event evt) {
switch (evt.id) {
case Event.SCROLL_LINE_UP:
case Event.SCROLL_LINE_DOWN:
case Event.SCROLL_PAGE_UP:
case Event.SCROLL_PAGE_DOWN:
case Event.SCROLL_ABSOLUTE:
if (evt.target == verticalScroll) {
canvas.paddingY = ((Integer)evt.arg).intValue();
canvas.repaint();
}
if (evt.target == horziontalScroll) {
canvas.paddingX = ((Integer)evt.arg).intValue();
canvas.repaint();
}
}
return super.handleEvent(evt);
}
//Don't call this until the canvas size is valid.
void changeSizeX() {
int canvasWidth = canvas.size().width;
if (canvasWidth <= 0) {
System.out.println("Canvas has no width; can't resize scrollbar");
return;
}
//Shift everything to the right if we're displaying empty space
//on the right side.
if ((canvas.paddingX + canvasWidth) > imgObjSize.width) {
int newpaddingX = imgObjSize.width - canvasWidth;
if (newpaddingX < 0) {
newpaddingX = 0;
}
canvas.paddingX = newpaddingX;
}
horziontalScroll.setValues(//draw the part of the image that starts at this x:
canvas.paddingX,
//amount to scroll for a "page":
(int)(canvasWidth * 0.9),
//minimum image x to specify:
0,
//maximum image x to specify:
imgObjSize.width - canvasWidth);
//"visible" arg to setValues() has no effect after scrollbar is visible.
horziontalScroll.setPageIncrement((int)(canvasWidth * 0.9));
return;
}
//Don't call this until the canvas size is valid.
void changeSizeY() {
int canvasHeight = canvas.size().height;
if (canvasHeight <= 0) {
System.out.println("Canvas has no height; can't resize scrollbar");
return;
}
//Shift everything downward if we're displaying empty space
//on the bottom.
if ((canvas.paddingY + canvasHeight) > imgObjSize.height) {
int newpaddingY = imgObjSize.height - canvasHeight;
if (newpaddingY < 0) {
newpaddingY = 0;
}
canvas.paddingY = newpaddingY;
}
verticalScroll.setValues(//initially draw part of image starting at this y:
canvas.paddingY,
//visible arg--amount to scroll for a "page":
(int)(canvasHeight * 0.9),
//minimum image y to specify:
0,
//maximum image y to specify:
imgObjSize.height - canvasHeight);
//"visible" arg to setValues() has no effect after scrollbar is visible.
verticalScroll.setPageIncrement((int)(canvasHeight * 0.9));
return;
}
public void paint(Graphics g) {
//This method probably was called due to applet being resized.
changeSizeX();
changeSizeY();
return;
}
}
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com
Powered by phpBB © phpBB Group.