Hello,
I am trying to write a simple image viewer which will center the image if
the image is smaller than the APPLET size, but otherwise load the image as
the content of a JScrollPane. The code attached works, but has an
unfortunate side-effect which is that the image seems to flicker if the
image is smaller than the applet. It seems to repaint twice, the first
time with the image located in the upper left corner, then quickly it
appears centered. I have debug code which indicates that the
paintComponent member is only called once. I have googled this one and
come up empty handed. Any suggestions would be appreciated...
Thank you,
Michael Uman
Software Magician
/*
MODULE : ScrollPaneApp.java
PROJECT : Java Based image viewer
PROGRAMMER : Michael Uman
*/
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ScrollPaneApp extends JApplet {
String filename = "image.jpg";
JScrollPane scrollPane;
int debug = 0;
public void start() {
if (debug == 1)
System.out.println("start()");
}
public void stop() {
if (debug == 1)
System.out.println("stop()");
}
public void init() {
// System.out.println("init()");
String tmp;
// -- Get Applet parameters...
if ((tmp = getParameter("debug")) != null) {
debug = Integer.parseInt(tmp);
}
if ((tmp = getParameter("image")) != null) {
filename = tmp;
}
if (debug == 1)
System.out.println("Filename = " + filename);
try {
URL url = new URL(getCodeBase(), filename);
Image image = getImage(url);
int w = getWidth();
int h = getHeight();
scrollPane = new JScrollPane(new ImageComponent(image, w, h));
this.getContentPane().add(scrollPane);
} catch (MalformedURLException e) {
}
}
public String getAppletInfo() {
return "ScrollPaneApplet (C) 2005 Softwaremagic.net";
}
public String[][] getParameterInfo() {
String param_info[][] = {
{ "debug", "0..1", "0 = no debug, 1 = debug messages", },
{ "image", "String", "Name of image to load", },
};
return param_info;
}
}
------------------------------------------------------------------
/*
MODULE : ImageComponent.java
PROJECT : Java Based image viewer
PROGRAMMER : Michael Uman
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ImageComponent extends JComponent {
Image image;
Dimension size;
int parent_w, parent_h;
public ImageComponent(Image image, int w, int h) {
this.image = image;
this.parent_w = w;
this.parent_h = h;
MediaTracker mt = new MediaTracker(this);
mt.addImage(image, 0);
try {
mt.waitForAll();
}
catch (InterruptedException e) {
}
size = new Dimension(image.getWidth(this), image.getHeight(this));
setPreferredSize(size);
}
public void paintComponent(Graphics g) {
// System.out.println("oh paint()");
int off_x = 0,
off_y = 0;
if (parent_w > size.getWidth()) {
off_x = (int)((parent_w - size.getWidth()) / 2);
}
if (parent_h > size.getHeight()) {
off_y = (int)((parent_h - size.getHeight()) / 2);
}
// System.out.println("X offset = " + off_x + " Y offset = " + off_y);
g.drawImage(image,
off_x,
off_y,
this);
}
public Dimension getPreferredSize() {
return size;
}
}
Andrey Kuznetsov - 02 Mar 2005 23:06 GMT
> I am trying to write a simple image viewer which will center the image if
> the image is smaller than the APPLET size
this is the job for (proper implemented) JScrollPane.
see http://jgui.imagero.com/doc/com/imagero/gui/swing/MScrollPane.html

Signature
Andrey Kuznetsov
http://uio.dev.java.net Unified I/O for Java
http://reader.imagero.com Java image reader
http://jgui.imagero.com Java GUI components and utilities