1

That when painted for the first time, it is not painted correctly. However, when I minimize the frame and restore it, it is painted correctly.

How will I fix this? I have tried repaint().

Here is the code

import javax.swing.JComponent;
import javax.swing.JFrame;

import com.sun.jna.Function;
import com.sun.jna.Native;
import com.sun.jna.NativeLibrary;
import com.sun.jna.Structure;
import com.sun.jna.platform.win32.WinDef.HWND;
import com.sun.jna.platform.win32.WinNT.HRESULT;

/**
 * @author ex0b1t
 *
 */
public class Aero {

    public void enableAeroEffect(JFrame frame) {        

        NativeLibrary dwmapi = NativeLibrary.getInstance("dwmapi");
        HWND aeroFrameHWND = new HWND(Native.getWindowPointer(frame));

        MARGINS margins = new MARGINS();
        margins.cxLeftWidth = -1;
        margins.cxRightWidth = -1;
        margins.cyBottomHeight = -1;
        margins.cyTopHeight = -1;

        Function extendFrameIntoClientArea = dwmapi
                .getFunction("DwmExtendFrameIntoClientArea");
        HRESULT result = (HRESULT) extendFrameIntoClientArea.invoke(
                HRESULT.class, new Object[] { aeroFrameHWND, margins });
        if (result.intValue() != 0)
            System.err.println("Call to DwmExtendFrameIntoClientArea failed.");

        frame.getRootPane().setDoubleBuffered(false);
        frame.getRootPane().setOpaque(false);

        if (frame.getRootPane().getContentPane() instanceof JComponent) {
            JComponent content = (JComponent) frame.getRootPane().getContentPane();
            content.setOpaque(false);
            content.setDoubleBuffered(false);
        }       
    }

    /**
     * @author ex0b1t
     *
     */
    public class MARGINS extends Structure implements Structure.ByReference {
        public int cxLeftWidth;
        public int  cxRightWidth;
        public int  cyTopHeight;
        public int  cyBottomHeight;
    }   
}



import javax.swing.JFrame;

/**
 * @author ex0b1t
 *
 */

public class MediaManager extends JFrame {

    private static final long serialVersionUID = -8440221168382362270L;

    public MediaManager() {     
        setTitle("Media Manager");
        setSize(800, 600);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    /**
     * @param args
     */
    public static void main(String[]args){
        MediaManager mediamanager = new MediaManager();
        mediamanager.setVisible(true);
        new Aero().enableAeroEffect(mediamanager);  
        mediamanager.repaint();
    }
}

Thank in advance.

eeerahul
  • 1,629
  • 4
  • 27
  • 38
ex0b1t
  • 1,292
  • 1
  • 17
  • 25

2 Answers2

1

What do you mean when you say it is not correctly painted? I had a similar problem where when my frame was painted for the first time the contentPane was totally black. When i minimized the frame and then maximizing it,it was painted correctly (but not always- there were times i had to do it 2 or 3 times.) It turned out it was the drivers of my graphics card. I reinstalled them and it worked fine! (I had a Randeon)

also try

           SwingUtilities.invokeLater(new Runnable(){
                   @Override
                   public void run(){
                      mediamanager.setVisible(true);
                      new Aero().enableAeroEffect(mediamanager);  
                      mediamanager.repaint();
                   }
           }

Threads and Swing

http://java.sun.com/products/jfc/tsc/articles/threads/threads1.html

SwingUtilities.invokeLater

Community
  • 1
  • 1
C.LS
  • 1,319
  • 2
  • 17
  • 35
1

setting the extended state of the frame to iconified and back to normal will paint the frame correctley. however when i add a component to the frame the backround is regrawn incorectley.

frame.setExtendedState(JFrame.ICONIFIED);
frame.setExtendedState(JFrame.NORMAL);
ex0b1t
  • 1,292
  • 1
  • 17
  • 25