Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-17-2007, 07:37 AM
Member
 
Join Date: Mar 2007
Posts: 16
samson is on a distinguished road
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 ?
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-17-2007, 07:40 AM
Senior Member
 
Join Date: Mar 2007
Posts: 136
goldhouse is on a distinguished road
try using JApplet instead of applet assuming you are using double bufferised strategy
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 08-13-2007, 06:44 PM
Member
 
Join Date: Jul 2007
Posts: 23
marco is on a distinguished road
if it is a .gif image it will make it flicker
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 09-21-2007, 11:51 PM
Senior Member
 
Join Date: Jul 2007
Posts: 1,144
hardwired is on a distinguished road
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
Code:
canvas.getParent().validate(); invalidate();
to validate scrollPane after setting image.

Last edited by hardwired : 09-22-2007 at 02:17 AM. Reason: add validation code
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Basic Applet jkswebsite Java Applets 4 01-13-2008 10:14 PM
applet lifecycle babua_87 Java Applets 1 12-04-2007 11:42 PM
First Applet HELP???? nvidia New To Java 0 08-13-2007 11:11 PM
Applet kapoorje Java Applets 0 07-24-2007 05:06 PM
Applet, To center text and To open I engage in a dialog in an Applet Marcus Java Applets 4 06-08-2007 07:15 AM


All times are GMT +3. The time now is 09:48 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org