Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 26th, 2012  |  syntax: None  |  size: 1.33 KB  |  hits: 12  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Why is getSize() not working for me here and why the flicker when resizing?
  2. import java.awt.Color;
  3. import java.awt.Graphics2D;
  4. import java.awt.image.BufferStrategy;
  5. import javax.swing.JFrame;
  6. import javax.swing.JPanel;
  7.  
  8. public class BSTest extends JFrame {
  9.     BufferStrategy bs;
  10.     DrawPanel panel = new DrawPanel();
  11.  
  12.     public BSTest() {
  13.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  14.         setSize(800,420);
  15.         setLocationRelativeTo(null);
  16.         setIgnoreRepaint(true);
  17.         setVisible(true);
  18.         createBufferStrategy(2);
  19.         bs = getBufferStrategy();
  20.         panel.setIgnoreRepaint(true);
  21.         add(panel);
  22.         panel.drawStuff();
  23.     }
  24.  
  25.     public class DrawPanel extends JPanel {
  26.         public void drawStuff() {
  27.             while(true) {
  28.                 try {
  29.                     Graphics2D g = (Graphics2D)bs.getDrawGraphics();
  30.                     g.setColor(Color.BLACK);
  31.                     System.out.println("W:"+getSize().width+", H:"+getSize().height);
  32.                     g.fillRect(0,0,getSize().width,getSize().height);
  33.                     bs.show();
  34.                     g.dispose();
  35.                     Thread.sleep(20);
  36.                 } catch (Exception e) { System.exit(0); }
  37.             }
  38.         }
  39.      }
  40.  
  41.     public static void main(String[] args) {
  42.         BSTest bst = new BSTest();
  43.     }
  44. }