Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Delayed painting

JörgMay 17 2011 — edited May 19 2011
Hello,

I would like a label to be painted with pauses. To get an impression,
run the code as it is. The actual painting is done in the doPaint(...)-method.
Currently I pass the Graphics of the panel, which is not what I want, but at least
we see some output. The question of this "demo"-code is: Why cipher "1" is not
output?

What I originally tried is to pass the Graphics of the label - in the end several
labels will be added to the panel - but then nothing is painted at all.

Writing an own class which extends JLabel was no way out either.

So why is (a partial) output visible when the panel's graphics is passed and
why not when it is the label's graphics?
And is the whole approach valid or has simply everything to be painted on a panel?

java version is 1.6.0_24.
import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;

public class DelayedPaint {
  JFrame frame;
  JPanel panel;
  JLabel lb;


  public static void main(String args[]) {
    EventQueue.invokeLater(new Runnable() {
      public void run() {
	new DelayedPaint().createAndShowGUI();
      }
    });
  }


  void createAndShowGUI() {
    frame= new JFrame("DelayedPaint");
    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    frame.setSize(400,400);
    panel= new JPanel(new BorderLayout());
    frame.setContentPane(panel);
    frame.setVisible(true);
    Thread t = new Thread() {
      public void run() {
	draw(panel);
      }
    };
    t.start();
  }


  private void delay(int ms) {
    try {
      Thread.sleep(ms);
    }
    catch (InterruptedException e) {
      System.out.println ("Error in delay: "+ e.toString());
    }
  }


  public void doPaint(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;
// Uncomment the following code to draw an arc. The arc will not be drawn, but cipher '1' appears.
/*
    Dimension dim= frame.getSize();
    g2.draw(new Arc2D.Double(50,25, dim.height-100, dim.width-100, 0,360,
				Arc2D.OPEN));
    delay(1000);
*/
    g2.setFont(new Font("Dialog", Font.BOLD, 15));
    for (char c='1'; c<':'; c++) {
      g2.drawString(""+c, (c-48)*10, 50);
      delay(500);
//      lb.revalidate();
//      lb.repaint();
    }
  }


  private void draw(JPanel panel) {
    lb= new JLabel("Good morning", SwingConstants.CENTER);
    Dimension dim= frame.getSize();
    lb.setPreferredSize(new Dimension(dim.width, 80));
    panel.add(lb, BorderLayout.NORTH);
//    delay(2000);

    lb= new JLabel();
    lb.setPreferredSize(new Dimension(dim.height-20, dim.height-20));
    panel.add(lb, BorderLayout.CENTER);
//    doPaint(lb.getGraphics());
    doPaint(panel.getGraphics());
/*
    lb= new MyLabel();
    panel.add(lb, BorderLayout.CENTER);
*/
//    lb.revalidate();
//    lb.repaint();
  }



/*
  class MyLabel extends JLabel {
    public void paintComponent(final Graphics g) {
      super.paintComponent(g);
      Graphics2D g2 = (Graphics2D) g;
      g2.setFont(new Font("Dialog", Font.BOLD, 15));
      for (char c='1'; c<':'; c++) {
	g2.drawString(""+c, (c-48)*10, 50);
//	delay(500); // blocks EDT
      }
    }
  }
*/

}
This post has been answered by camickr on May 18 2011
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 16 2011
Added on May 17 2011
9 comments
356 views