Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / March 2005

Tip: Looking for answers? Try searching our database.

Flashing image in JScrollPane

Thread view: 
Michael Uman - 02 Mar 2005 22:04 GMT
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



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.