java.awt.Frame - How to repaint without background update

Discussion in 'Java' started by MaciekL, Aug 5, 2011.

  1. MaciekL

    MaciekL Guest

    Hi,

    I'm trying to create simple application with BufferedImage that already
    contains some graphical content.
    During "repaint" operation, created image should be
    redrawn on the screen (without additional graphics functions).

    It seems to be easy, but there is something wrong.
    During "repaint" operation the window "blinks". It seems that
    'background' is displayed before image is drawn.

    "javax.swing.JFrame" solves the issue, but I would like to
    solve this with basic "java.awt.Frame".

    Following application demonstartes the issue.

    What should be added to this program to avoid backround repainting ?

    /*--::BEG::--[TestApp.java]-------------------------------------------------*/
    import java.awt.*;
    import java.awt.image.*;
    public class TestApp extends Frame implements Runnable
    {
    BufferedImage img = new BufferedImage(640, 480,
    BufferedImage.TYPE_INT_RGB);
    public TestApp()
    {
    Graphics g = img.getGraphics();
    g.setColor(Color.RED);
    g.drawRect(0, 0, img.getWidth() - 1, img.getHeight() - 1);
    g.setColor(Color.BLUE);
    g.fillRect(1, 1, img.getWidth() - 2, img.getHeight() - 2);
    (new Thread(this)).start();
    }
    public void run()
    {
    while (true)
    {
    try { Thread.sleep(100); }
    catch (Exception e) { }
    repaint();
    }
    }
    public void paint(Graphics g)
    {
    g.drawImage(img, 50, 50, this);
    }
    public static void main(String [] args)
    {
    Frame frame = new TestApp();
    frame.setSize(740, 580);
    frame.setLocation(50, 50);
    frame.setVisible(true);
    }
    }
    /*--::EOF::--[TestApp.java]-------------------------------------------------*/

    Regards

    --
    Maciek
     
    MaciekL, Aug 5, 2011
    #1
    1. Advertising

  2. On 05/08/2011 7:15 AM, MaciekL wrote:
    > Hi,
    >
    > I'm trying to create simple application with BufferedImage that already
    > contains some graphical content.
    > During "repaint" operation, created image should be
    > redrawn on the screen (without additional graphics functions).
    >
    > It seems to be easy, but there is something wrong.
    > During "repaint" operation the window "blinks". It seems that
    > 'background' is displayed before image is drawn.
    >
    > "javax.swing.JFrame" solves the issue, but I would like to
    > solve this with basic "java.awt.Frame".


    You're getting bitten by java.awt.Frame's lack of double buffering. I'm
    not sure there's any remotely easy solution other than using JFrame.

    What is your reason for wanting to avoid JFrame?
     
    supercalifragilisticexpialadiamaticonormalizeringe, Aug 5, 2011
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Guest

    newbie: awt repaint

    Guest, Aug 7, 2003, in forum: Java
    Replies:
    2
    Views:
    729
    Guest
    Aug 7, 2003
  2. Robert Mark Bram
    Replies:
    2
    Views:
    6,032
    Robert Mark Bram
    Dec 11, 2003
  3. Ralf
    Replies:
    0
    Views:
    400
  4. MaciekL
    Replies:
    2
    Views:
    1,837
    Ian Shef
    Aug 5, 2011
  5. Maciej Labanowicz
    Replies:
    8
    Views:
    508
Loading...

Share This Page