Why drawImage(...) an off-screen image cause flicking

Hi,
I tried to develop simple animation effects with the bufferred images but I found noticeable flicking. The codes I posted below were the modified version of the example code in the book Java2D.
Can someone help me out? I don't understand why flicking occurs.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public abstract class ApplicationFrame extends Frame implements ActionListener {
  protected Graphics2D offscreenGraphics2D = null;
  Timer drawtimer = new Timer(35, this);
  float alphavalue = (float)0.0;
  public ApplicationFrame() { this("ApplicationFrame v1.0"); }
  public ApplicationFrame(String title) {
    super(title);
    createUI();
  protected void createUI() {
    setSize(500, 400);
    center();
    addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        dispose();
        System.exit(0);
  public void center() {
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    Dimension frameSize = getSize();
    int x = (screenSize.width - frameSize.width) / 2;
    int y = (screenSize.height - frameSize.height) / 2;
    setLocation(x, y);
    drawtimer.start();
import java.awt.*;
import java.awt.geom.*;
import java.awt.event.*;
import javax.swing.*;
public class TransparentText {
  public static void main(String[] args) {
    ApplicationFrame f = new ApplicationFrame("TransparentText v1.0") {
      private int mNumberOfLines = 25;
      private Color[] mColors = { Color.red, Color.green, Color.blue };
      protected Image offscreenImage = null;
      public void actionPerformed(ActionEvent e) {
        alphavalue = alphavalue + (float)0.0033;
        if (alphavalue >= (float)0.9) {
           drawtimer.stop();
           return;
        repaint();       
      public void paint(Graphics g) {
//        System.out.println("paint() w/ alpha "+alphavalue);
        Graphics2D g2 = (Graphics2D)g;
        if (offscreenGraphics2D == null) {
           offscreenImage = createImage(500, 400);
           offscreenGraphics2D = (Graphics2D)offscreenImage.getGraphics();
        // Set the rendering quality.
        offscreenGraphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
               RenderingHints.VALUE_ANTIALIAS_ON); // this will affect the display of Chinese characters
        // Paint a red rectangle.
        Rectangle2D r = new Rectangle2D.Double(50, 50, 150, 100);
        offscreenGraphics2D.setPaint(Color.red);
        offscreenGraphics2D.fill(r);
        // Set a composite with transparency.
        Composite currentcomposite = offscreenGraphics2D.getComposite();
        Composite c = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alphavalue);
        offscreenGraphics2D.setComposite(c);
        // Draw some blue text.
        offscreenGraphics2D.setPaint(Color.blue);
        offscreenGraphics2D.setFont(new Font("Times New Roman", Font.PLAIN, 72));
        offscreenGraphics2D.drawString("Composite", 25, 130);
        offscreenGraphics2D.setComposite(currentcomposite); // restore it
        g2.drawImage(offscreenImage, 0, 0, this);
    f.setSize(400, 200);
    f.center();
    f.setVisible(true);
}

It's because the update() method first paints the component in its background colour, then calls paint(). Override it to just call paint(), ie
public void update(Graphics g)
    paint(g);
}And using the paint method directly in an anonymous frame is a pretty unpleasant way to do it - better to create a subclass of JPanel, override the paint method in there and use it in the JFrame (either set it as the content pane or add it to it).

Similar Messages

  • Why is my Green Screen Image Overlaying Over The Video?

    So, at first, I thought it was the lighting, or my green screen wasn't unwrinkled enough.  I'd made some other videos in the past without any problem, so, figured, it was fine.  Instead I get this
    What's happening, what can I do to fix it, and how do I start getting the software working right?  Thank You!

    looks for me, that poor lil' iMovie interprets the blue gradient at some values as 'green'.
    or vice versa
    The consumer tool iMovie offers, for convenience, no manual calibration of a color-key ... just auto-mode.
    Try with another replacement, for a start in red…

  • Why are images off screen now?

    Since upgrading my ipad2 to ios6 some of my applications are "off screen" now, and appear shifted to far to the left side of the screen far enough to not be readable. One example is Fluent News. . .  Any others having this problem..

    Start Firefox in <u>[[Safe Mode]]</u> to check if one of the extensions is causing the problem (switch to the DEFAULT theme: Firefox (Tools) > Add-ons > Appearance/Themes).
    *Don't make any changes on the Safe mode start window.
    *https://support.mozilla.com/kb/Safe+Mode
    *https://support.mozilla.com/kb/Troubleshooting+extensions+and+themes
    See also:
    *http://kb.mozillazine.org/Corrupt_localstore.rdf

  • Imac screen image size fluctuates then turns off

    imac screen image size fluctuates then turns off. I have an Imac G3 (slot drive/Grape). It is running on OS 9.2 and I believe has 288 MB Ram.It started acting very odd yesterday. First, I noticed that the image on the screen had shrunken in on the sides and was elongated up and down. I restarted and when it came up the image still wasn't right, but it wasn't as out of proportion. I ran norton and first aid which both said everything was fine. By this time the image had stretched out to its normal size. I printed a couple of things and shut it down. This morning I started it up to have a look and the image was all shrunken and stretched again. The image started pulsating then the mac shut off. A few minutes later it started up again on its own so I turned it off. Until yesterday my Mac had been working just fine. Any ideas about what this is or what to do about it? Thanks for the help.
    Charlotte

    The fact that it is starting up on its own either means that it is scheduled to, or that there is some problem with the power supply, which as far as I can tell, would cause the monitor to do that.

  • Some website's which i have never visited before randomly open in a new tab. this causes the mouse image to flicker. How do I block these sites from opening?

    This is an example of one of the sites that pops up in a new tab without me having visited it before wanting it to open causing the mouse image to flicker.
    https://www.wgt.com/signup.aspx?utm_source=adsim&utm_medium=1187-7263303&utm_campaign=adsim

    Do a malware check with some malware scanning programs on the Windows computer.<br />
    You need to scan with all programs because each program detects different malware.<br />
    Make sure that you update each program to get the latest version of their databases before doing a scan.<br /><br />
    *http://www.malwarebytes.org/mbam.php - Malwarebytes' Anti-Malware
    *http://www.superantispyware.com/ - SuperAntispyware
    *http://www.microsoft.com/security/scanner/en-us/default.aspx - Microsoft Safety Scanner
    *http://www.microsoft.com/windows/products/winfamily/defender/default.mspx - Windows Defender: Home Page
    *http://www.safer-networking.org/en/index.html - Spybot Search & Destroy
    *http://www.lavasoft.com/products/ad_aware_free.php - Ad-Aware Free
    See also:
    *"Spyware on Windows": http://kb.mozillazine.org/Popups_not_blocked

  • Any ideas as to why my Iphone 4s' screen turns on and then off at random while just sitting next to me?

    Any ideas as to why my Iphone 4s' screen turns on and then off at random while just sitting next to me?

    No alerts. That's what I thought at first but no banners or anything indicating an alert of any kind.

  • Why is my iPod screen grey and unresponsive? Won't turn off too.

    Why is my IPod screen grey and won't turn off using restart button?

    as to why its gray, its either a glitch or something else. you need to turn it off and plug it into itunes and restore it. then sync it.

  • Why a long and narrow image (400x2200)mm is cut off while printing on Epson 9880 using Adobe Illustrator?

    Why a long and narrow image (400x2200)mm is cut off while printing on Epson 9880 using Adobe Illustrator?

    3.2 you can make multiple clocks, each clock can have a different alarm.

  • ISO 7 why can't I use my photos as my home screen images anymore!? They don't crop. They zoom all awkwardly!! Please fix this!!!!

    The new design is great but why the change in homescreen images?!? I can't use my Instagram photos as home screen anymore?!? Why?
    And this OS is slooowwwww!

    Thank you for replying! I think it's rather annoying of them to change it. It was an aawso feature i loved and enjoyed.
    I hope it gets changed back! This super zoom is unnecessary. Give me back my black borders! Lol

  • My screen image is off center

    My screen image are slighly off the monitor and I can't find how to re-postion it. I just started with windows 7. What should I do?

    The images position can be adjusted by using the buttons in lth lower right hand corner of the monitor itself.

  • Why are my windows sliding off screen?

    So, I was on my Mac Book Pro doing work and I flick between different windows (FCP, Finder, Safari etc) using the cmd - Tab function. All works fine and all the windows stay on the screen.
    However, I was doing something in finder when all of a sudden all the toher windows slid off scree to the left. Now whenever I use Finder the other widows slide off screen and when I tab back to use the other windows finder slides off to the right! I just want them all to be on the screen at the same time again!!
    Any ideas?

    Not sure if you have solved your issue yet. But I had the same issue. Disable/trash iSnap.That's the culprit.

  • Why is my iPhone screen not turning off?

    Why is my iPhone screen not turning off?

    Sorry, my previous post was meant for another thread.
    You can try resetting your iPhone by simultaneously pressing and holding the Home and Sleep/Wake buttons until you see the Apple Logo. This can take up to 15 seconds so be patient and don't release the buttons until the logo appears.
    Try again to see if the problem persists.
    Message was edited by: Alfred DeRose

  • HT5455 Can long periods with no screen saver cause permanent image persistence or other damage to my IPS retina display?

    Can long periods of login screen idle or no screen saver cause permanent image persistence (or other damage for that sake) to my IPS retina display?

    Image persistance on Apple displays:
    http://support.apple.com/kb/HT5455
    Apple say this about LCD screen burn in:
    http://support.apple.com/kb/HT2807
    which includes suggestions for curing that effect.
    Alternatively:
    How to fix screen burn and/or stuck pixels on an LCD monitor:
    JScreenFix
    http://www.jscreenfix.com/basic.php

  • Help.....my ipod keeps flicking off and going to the power off screen, help.....my ipod keeps flicking off and going to the power off screen

    help...my ipod keeps returning to the power off screen and keeps flickering?

    Try:
    - iOS: Not responding or does not turn on
    - Also try DFU mode after try recovery mode
    How to put iPod touch / iPhone into DFU mode « Karthik's scribblings
    - If not successful and you can't fully turn the iOS device fully off, let the battery fully drain. After charging for an least an hour try the above again.
    - If still not successful that usually indicates a hardware problem and an appointment at the Genius Bar of an Apple store is in order.
    Apple Retail Store - Genius Bar       

  • Layers Moving Off Screen When I click Around

    Hi everyone, I am experiencing an annoying issue with Photoshop CS5 12.0.4. In my normal and quick workflow I notice that at random times when I am in and out of the layers and onto the canvas, sometimes layers go sailing off screen eventhough I am not moving them. Its as if PS is tracking my movements off screen and just blasting the layers all around. I saw the problems last week sometime on the forum, but I can't seem to find the thread again. Anyone have any clue what the heck could be causing this?
    Running windows 7 x64, PS x64, I also have a Logoitech G9X high res mouse.

    Do you mean that the entire image pans out of sight, or further than you wanted it to?    It sounds like the flick pan feature that came in with CS4.  You hold down the space bar to pan, flick the screen with the left mouse button, and it keeps on going.  Is that what you mean? If yes, then left click the screen to stop the pan.
    Hmmm...  Reading your post again, I am getting the impression that you know your way round the app, and it is nothing so simple.

Maybe you are looking for