Can you render components swing through a Graphics object?
I have a structure like this in my class:
public void render(Graphics g) {
//Render stuff
}
Is it possible to create an object swing, and for that object to render i using the Graphics object?
sure, absolutely possible.
public class MyClass extends JComponent {
//...
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
render(g);
}
}
I take it, you want to draw, but not to the screen. One can create a BufferedImage
(or read a background image) and create a Graphics2D object with which to draw.
File imgFile = new File("scenery.png");
BufferedImage img = ImageIO.read(imgFile);
Graphics2D g = img.createGraphics();
render(g);
g.dispose();
ImageIO.write(img, "png", imgFile);
Not to forget the g.dispose()
to releast native drawing state.
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.