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

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

  1. MaciekL

    MaciekL Guest

    solved:

    Adding additonal update method makes that Background is not displayed

    public void update(Graphics g) { paint(g); }

    Maciek

    --
    Regards

    On 2011-08-05 13:15, MaciekL wrote:
    > 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);
    > }
    > }
     
    MaciekL, Aug 5, 2011
    #1
    1. Advertising

  2. On 8/5/2011 4:45 AM, MaciekL wrote:
    > solved:
    >
    > Adding additonal update method makes that Background is not displayed
    >
    > public void update(Graphics g) { paint(g); }
    >
    > Maciek
    >


    That's because update() clears the background.

    I too am curious why you want to use AWT instead of Swing.

    --

    Knute Johnson
     
    Knute Johnson, Aug 5, 2011
    #2
    1. Advertising

  3. MaciekL

    Ian Shef Guest

    Knute Johnson <> wrote in news:j1h2au$h7k$1@dont-
    email.me:

    > On 8/5/2011 4:45 AM, MaciekL wrote:
    >> solved:
    >>
    >> Adding additonal update method makes that Background is not displayed
    >>
    >> public void update(Graphics g) { paint(g); }
    >>
    >> Maciek
    >>

    >
    > That's because update() clears the background.


    ....and, for heavyweight components such as Frame, repaint(...) causes update
    (...) to be called.

    See "Painting in AWT and Swing" at
    http://java.sun.com/products/jfc/tsc/articles/painting/

    (but be aware that later versions of Java have reduced some of the
    restrictions regarding mixing lightweight and heavyweight components).
     
    Ian Shef, Aug 5, 2011
    #3
    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:
    728
    Guest
    Aug 7, 2003
  2. Robert Mark Bram
    Replies:
    2
    Views:
    6,028
    Robert Mark Bram
    Dec 11, 2003
  3. Ralf
    Replies:
    0
    Views:
    399
  4. MaciekL
    Replies:
    1
    Views:
    928
    supercalifragilisticexpialadiamaticonormalizeringe
    Aug 5, 2011
  5. Maciej Labanowicz
    Replies:
    8
    Views:
    506
Loading...

Share This Page