Results 1 to 4 of 4
Thread: Applet flickering
- 07-17-2007, 06:37 AM #1
Member
- Join Date
- Mar 2007
- Posts
- 16
- Rep Power
- 0
- 07-17-2007, 06:40 AM #2
Senior Member
- Join Date
- Mar 2007
- Posts
- 134
- Rep Power
- 0
try using JApplet instead of applet assuming you are using double bufferised strategy
- 08-13-2007, 05:44 PM #3
Member
- Join Date
- Jul 2007
- Posts
- 23
- Rep Power
- 0
if it is a .gif image it will make it flicker
- 09-21-2007, 10:51 PM #4
Edit: add these two linesJava 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; } }
to validate scrollPane after setting image.Java Code:canvas.getParent().validate(); invalidate();Last edited by hardwired; 09-22-2007 at 01:17 AM. Reason: add validation code
Similar Threads
-
Basic Applet
By jkswebsite in forum Java AppletsReplies: 4Last Post: 01-13-2008, 09:14 PM -
applet lifecycle
By babua_87 in forum Java AppletsReplies: 1Last Post: 12-04-2007, 10:42 PM -
First Applet HELP????
By nvidia in forum New To JavaReplies: 0Last Post: 08-13-2007, 10:11 PM -
Applet
By kapoorje in forum Java AppletsReplies: 0Last Post: 07-24-2007, 04:06 PM -
Applet, To center text and To open I engage in a dialog in an Applet
By Marcus in forum Java AppletsReplies: 4Last Post: 06-08-2007, 06:15 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks