简体   繁体   中英

Draw AWT/Swing components indirectly?

I wanna know... is there a way to draw the components style outside from Frame / Applet ? I'm making a application that uses OpenGL , and I need a GUI with a good look, like AWT or Swing. Yes, I know that there are many libraries like Nifty and TWL , but I really want use AWT/Swing look.

Thanks. (And sorry for my bad english, I'm brazilian.)

You can render a component to an image, even if it is not displayable (although you will have to manually set the size to the preferred size):

c.setSize(c.getPreferredSize());
BufferedImage img = new BufferedImage(c.getWidth(), c.getHeight(), 
      BufferedImage.TYPE_INT_ARGB);
Graphics2D g = image.createGraphics();
g.setComposite(AlphaComposite.Clear);
g.fillRect(0, 0, c.getWidth(), c.getHeight());
g.setComposite(AlphaComposite.SrcOver);
c.paintComponent(g);
g.dispose();

You can then use that image as a texture in OpenGL.

Of course, this will only give you a picture of the component; you won't be able to interact with it as you could with a "real" swing component.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM