when i resize the window it resizes properly but the canvas that I'm using to hold the image doesn't resize with it.
heres the code:
image = new BufferedImage(gc.width,gc.height,BufferedImage.TYPE_INT_RGB);
canvas = new Canvas();
Dimension s = new Dimension((int)(gc.width*gc.scale),(int)(gc.height*gc.scale));
canvas.setPreferredSize(s);
canvas.setMaximumSize(s);
canvas.setMinimumSize(s);
frame = new JFrame(gc.title);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(canvas,BorderLayout.CENTER);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setResizable(true);
frame.setVisible(true);
canvas.createBufferStrategy(2);
bs = canvas.getBufferStrategy();
g = bs.getDrawGraphics();
}
public void update() {
Dimension s = frame.getSize();
canvas.setPreferredSize(s);
canvas.setMaximumSize(s);
canvas.setMinimumSize(s);
g.drawImage(image,0, 0, canvas.getWidth(), canvas.getHeight(),null);
bs.show();
}
help is appreciated.
How do you invoke update?
You should implement and add a ComponentListener (or ComponentAdapter) to the JFrame, in the componentResized method, you invoke update. Something like:
this.addComponentListener(new ComponentAdapter(){
componentResized(ComponentEvent e){
//UPDATE YOUR CANVAS HERE
}
});
Another way is overriding update(Graphic) in your JFrame:
public class MyWindow extends JFrame{
@Override
public void update(Graphics g){
super.update(g);
//UPDATE YOUR CANVAS HERE
}
}
I'd suggest switching to JavaFX.
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.