Posted By

s1w on 09/28/11


Tagged

performance swing graphics Trick Rate win7 flickering aero repaint


Versions (?)

Resizing Swing in Win7+Aero (trick to minimalize flickering)


 / Published in: Java
 

URL: http://siwego.net/dev/corner.png

This trick improves repaint rate in Win7+Aero: setting resizable to null, and providing own resize hook. Its aint perfect, but still alot better.. check my example:

  1. /*
  2.  * resizing swing trick in Win7+Aero demo
  3.  * @author: s1w_ /pj`s3826
  4. */
  5. import java.awt.event.*;
  6. import java.awt.*;
  7. import javax.swing.event.*;
  8. import javax.swing.*;
  9.  
  10. class ResizeHookDemo extends JDialog {
  11. private final static int width = 580, height = 350;
  12. private final JFileChooser fc;
  13. private java.awt.geom.GeneralPath gp;
  14.  
  15. public ResizeHookDemo() {
  16. super((JDialog)null, "Choose File", true);
  17.  
  18. fc = new JFileChooser() {
  19.  
  20. @Override
  21. public void paint(Graphics g) {
  22. super.paint(g);
  23. int w = getWidth();
  24. int h = getHeight();
  25. g.setColor(new Color(150, 150, 150, 200));
  26. g.drawLine(w-7, h, w, h-7);
  27. g.drawLine(w-11, h, w, h-11);
  28. g.drawLine(w-15, h, w, h-15);
  29.  
  30. gp = new java.awt.geom.GeneralPath();
  31. gp.moveTo(w-17, h);
  32. gp.lineTo(w, h-17);
  33. gp.lineTo(w, h);
  34. gp.closePath();
  35. }
  36.  
  37. };
  38. fc.addActionListener(new ActionListener() {
  39.  
  40. public void actionPerformed(ActionEvent e) {
  41. if (e.getActionCommand().equals("CancelSelection")) {
  42. setVisible(false);
  43. // action...
  44. }
  45. else if (e.getActionCommand().equals("ApproveSelection")) {
  46. setVisible(false);
  47. // action...
  48. }
  49. }
  50. });
  51.  
  52. private Point startPos = null;
  53.  
  54. public void mousePressed(MouseEvent e) {
  55. if (gp.contains(e.getPoint()))
  56. startPos = new Point(getWidth()-e.getX(), getHeight()-e.getY());
  57. }
  58.  
  59. public void mouseReleased(MouseEvent mouseEvent) {
  60. startPos = null;
  61. }
  62.  
  63. public void mouseMoved(MouseEvent e) {
  64. if (gp.contains(e.getPoint()))
  65. setCursor(Cursor.getPredefinedCursor(Cursor.SE_RESIZE_CURSOR));
  66. else
  67. setCursor(Cursor.getDefaultCursor());
  68. }
  69.  
  70. public void mouseDragged(MouseEvent e) {
  71. if (startPos != null) {
  72.  
  73. int dx = e.getX() + startPos.x;
  74. int dy = e.getY() + startPos.y;
  75.  
  76. setSize(dx, dy);
  77. repaint();
  78. }
  79. }
  80. };
  81.  
  82. fc.addMouseMotionListener(resizeHook);
  83. fc.addMouseListener(resizeHook);
  84. fc.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 20));
  85. add(fc);
  86.  
  87. setResizable(false);
  88.  
  89. setMinimumSize(new Dimension(width, height));
  90. setDefaultCloseOperation(HIDE_ON_CLOSE);
  91. setLocationRelativeTo(null);
  92. }
  93.  
  94. public static void main(String args[]) {
  95. System.out.println("Starting demo...");
  96. SwingUtilities.invokeLater(new Runnable() {
  97.  
  98. @Override
  99. public void run() {
  100. new ResizeHookDemo().setVisible(true);
  101. }
  102. });
  103. }
  104. }

Report this snippet  

You need to login to post a comment.