Results 1 to 4 of 4
  1. #1
    samson is offline Member
    Join Date
    Mar 2007
    Posts
    16
    Rep Power
    0

    Default Applet flickering

    I have a simple applet using frames and canvas, when I change the image inside the applet the screen flicker, How can this be avoided ?

  2. #2
    goldhouse is offline Senior Member
    Join Date
    Mar 2007
    Posts
    134
    Rep Power
    0

    Default

    try using JApplet instead of applet assuming you are using double bufferised strategy

  3. #3
    marco is offline Member
    Join Date
    Jul 2007
    Posts
    23
    Rep Power
    0

    Default

    if it is a .gif image it will make it flicker

  4. #4
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    //  <applet code="AppletImageTest" width="400" height="400"></applet>
    import java.applet.Applet;
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.image.BufferedImage;
    import java.io.*;
    import java.net.*;
    import javax.imageio.ImageIO;
    
    public class AppletImageTest extends Applet implements ActionListener {
        AppletImageCanvas canvas = new AppletImageCanvas();
        FileDialog fileDialog;
    
        public void init() {
            ScrollPane scrollPane = new ScrollPane();
            scrollPane.add(canvas);
            setLayout(new BorderLayout());
            add(scrollPane);
            add(getUIPanel(), "Last");
        }
    
        public void actionPerformed(ActionEvent e) {
            showDialog();
            String folder = fileDialog.getDirectory();
            String fileName = fileDialog.getFile();
            if(folder != null && fileName != null) {
                File file = new File(folder, fileName);
                System.out.println("file = " + file.getPath());
                BufferedImage image = loadImage(file);
                canvas.setImage(image);
                canvas.getParent().validate();
            }
        }
    
        private void showDialog() {
            fileDialog = new FileDialog(new Frame());
            // Filter does not work in windows os - see api.
            fileDialog.setFilenameFilter(new ImageFileFilter());
            fileDialog.pack();
            fileDialog.setVisible(true);
        }
    
        private BufferedImage loadImage(File file) {
            BufferedImage image = null;
            try {
                URL url = file.toURL();
                image = ImageIO.read(url);
            } catch(MalformedURLException e) {
                System.out.println("URL error: " + e.getMessage());
            } catch(IOException e) {
                System.out.println("IO error: " + e.getMessage());
            }
            return image;
        }
    
        private Panel getUIPanel() {
            Button button = new Button("open");
            button.addActionListener(this);
            Panel panel = new Panel();
            panel.add(button);
            return panel;
        }
    
        public static void main(String[] args) {
            Applet applet = new AppletImageTest();
            Frame f = new Frame();
            f.addWindowListener(closer);
            f.add(applet);
            f.setSize(400,400);
            f.setLocation(200,200);
            applet.init();
            f.setVisible(true);
        }
    
        private static WindowListener closer = new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        };
    }
    
    class AppletImageCanvas extends Canvas {
        BufferedImage image;
    
        public void paint(Graphics g) {
            if(image != null) {
                int x = (getWidth() - image.getWidth())/2;
                int y = (getHeight() - image.getHeight())/2;
                g.drawImage(image, x, y, this);
            }
        }
    
        public Dimension getPreferredSize() {
            if(image == null)
                return new Dimension(100,100);
            return new Dimension(image.getWidth(), image.getHeight());
        }
    
        public void setImage(BufferedImage image) {
            this.image = image;
            invalidate();
            repaint();
        }
    }
    
    class ImageFileFilter implements FilenameFilter {
        final static String BMP  = "bmp";
        final static String GIF  = "gif";
        final static String JPG  = "jpg";
        final static String JPEG = "jpeg";
        final static String PNG  = "png";
     
        public boolean accept(File dir, String name) {
            String ext = getExtension(name);
            return ext.equals(BMP)  || ext.equals(GIF) || ext.equals(JPG) ||
                   ext.equals(JPEG) || ext.equals(PNG);
        }
    
        private String getExtension(String fileName) {
            int dot = fileName.lastIndexOf(".");
            if(dot > -1 && dot < fileName.length()-1)
                return fileName.substring(dot+1).toLowerCase();
            return null;
        }
    }
    Edit: add these two lines
    Java Code:
                canvas.getParent().validate();
            invalidate();
    to validate scrollPane after setting image.
    Last edited by hardwired; 09-22-2007 at 01:17 AM. Reason: add validation code

Similar Threads

  1. Basic Applet
    By jkswebsite in forum Java Applets
    Replies: 4
    Last Post: 01-13-2008, 09:14 PM
  2. applet lifecycle
    By babua_87 in forum Java Applets
    Replies: 1
    Last Post: 12-04-2007, 10:42 PM
  3. First Applet HELP????
    By nvidia in forum New To Java
    Replies: 0
    Last Post: 08-13-2007, 10:11 PM
  4. Applet
    By kapoorje in forum Java Applets
    Replies: 0
    Last Post: 07-24-2007, 04:06 PM
  5. Replies: 4
    Last Post: 06-08-2007, 06:15 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •