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!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

eliminate last bit of flicker during Frame re-sizing

843807Aug 7 2010 — edited Aug 9 2010
The following code prototypes screen flicker. The only flicker (noticeable to me at least) is while the Frame is being re-sized.

Is there a technique to eliminate flicker during Frame resizing? Given how the paint(Graphics g) method works (at least how I think it works), I don't think there is...? When I start using light-weight components, I wonder if the flickering while re-sizing will disappear? Will that be something to look for when testing JComponent instead of Canvas? As this is my first serious attempt at AWT, any general comments about how the code is written is welcomed.
public class Main {
  private static final Frame frame = new Frame("Alpha GUI v1.3");
  static {
    frame.setSize(800, 600);
    frame.addWindowListener(new MyWindowListener());
  }

  public static void main(String[] args) {
    Four four = new Four();
    frame.add(four);
    frame.setVisible(true);
    new Thread(four).start();
  }
      // I don't like the looks of anonymous classes yet. too confusing to debug right now.
      static class MyWindowListener extends WindowAdapter implements WindowListener  {
        @Override
        public void windowClosing(WindowEvent ev) { System.exit(1); }
      }
}
public class Four extends Canvas implements Runnable {
  private final Canvas canvas;
  private final StrokeJP[] strokes = new StrokeJP[4];

  Four() {
    strokes[0] = new Stroke1();
    strokes[1] = new Stroke2();
    strokes[2] = new Stroke3();
    strokes[3] = new Stroke4();
    canvas = this;
  }

  @Override
  public void run() {
    for(int i = 0; i < 500; i++) {
      try { Thread.currentThread().sleep(10); } catch(InterruptedException err) { }
      EventQueue.invokeLater(strokes[0]);
      EventQueue.invokeLater(strokes[1]);
      EventQueue.invokeLater(strokes[2]);
      EventQueue.invokeLater(strokes[3]);
    }
  }

  @Override
  public void paint(Graphics g) {
    Image img = createImage(getSize().width, getSize().height);
    Graphics og = img.getGraphics();
    strokes[0].paintFull(og);
    strokes[1].paintFull(og);
    strokes[2].paintFull(og);
    strokes[3].paintFull(og);

    g.drawImage(img, 0, 0, null);
  }
        // Still designing a shared parent class for all strokes
        private class Stroke1 extends StrokeJP {
          private BasicStroke stroke = new BasicStroke(10, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
          private final float x1 = 10;
          private final float y1 = 10;
          private float x2 = x1; private float x3 = x1;
          private float y2 = y1; private float y3 = y1;

          @Override
          public void run() {
            paintIncrimental(canvas.getGraphics());
            x2++;
          }
          public void paintIncrimental(Graphics g) {
            Graphics2D g2 = (Graphics2D) g;
            g2.setStroke(stroke);
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            g2.draw(new Line2D.Float(x3, y3, x2, y2));
            x3 = x2;
          }
          public void paintFull(Graphics g) {
            Graphics2D g2 = (Graphics2D) g;
            g2.setStroke(stroke);
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            g2.draw(new Line2D.Float(x1, y1, x2, y2));
          }
        }

        // Still designing a shared parent class for all strokes
        private class Stroke2 extends StrokeJP {
          private BasicStroke stroke = new BasicStroke(10, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
          private final float x1 = 10;
          private final float y1 = 10;
          private float x2 = x1; private float x3 = x1;
          private float y2 = y1; private float y3 = y1;

          @Override
          public void run() {
            paintIncrimental(canvas.getGraphics());
            x2++;
            y2++;
          }
          public void paintIncrimental(Graphics g) {
            Graphics2D g2 = (Graphics2D) g;
            g2.setStroke(stroke);
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            g2.draw(new Line2D.Float(x3, y3, x2, y2));
            x3 = x2;
            y3 = y2;
          }
          public void paintFull(Graphics g) {
            Graphics2D g2 = (Graphics2D) g;
            g2.setStroke(stroke);
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            g2.draw(new Line2D.Float(x1, y1, x2, y2));
          }
        }
        // ...................
}
// Working on this class, but would like closure on all flickering issues before proceeding.
public abstract class StrokeJP implements Runnable {
    abstract void paintIncrimental(Graphics g);
    abstract void paintFull(Graphics g);
}
I paint incrimentally when possible, and also double buffer. Are there other techniques?

Edited by: rerf on Aug 8, 2010 1:49 AM

Edited by: rerf on Aug 8, 2010 7:18 AM

Comments

aJohny

Inside you loop you could get the file name using

textfiles.getName();

You could filter with the file extension.

Cheers

AJ

2801625

Can you explain what you mean by filter with the file extension.

aJohny
Answer

You have mentioned you want to read only text files.

If you are referring to some specific set of extentions, you can filter based on that.

ex: you want to read only .txt files, you can add an if condition as below :

          if(textfiles.getName().endsWith(".txt")) {

              // Add your code here

          }

Cheers

AJ

Marked as Answer by 2801625 · Sep 27 2020
2801625

I was able to read only the text files in the directory checking the extension using textfiles.getname and textfiles.endswith.

Thanks for the help AJ.

     for (File textfiles : files) {

     if (textfiles.isFile() && textfiles.getName().endsWith("txt")) {

aJohny

If all good, Please close the thread by marking the helpful and correct answers.

1 - 5
Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Sep 6 2010
Added on Aug 7 2010
3 comments
1,915 views