hello i'm new to java programming and when does a componentResized get called? does it get called when your program first starts up? because in my code and i didn't add the "rect" in the center of the screen, until the window got resized. but whenever i start my program, the "rect" is added to the middle of the screen. i'm sorry if i sound vague but thank u so much for taking a look at this. hope u have a great day!
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import acm.graphics.GCanvas;
import acm.graphics.GRect;
public class MyCanvas extends GCanvas implements ComponentListener {
public MyCanvas() {
addComponentListener(this);
rect = new GRect(WIDTH, HEIGHT);
rect.setFilled(true);
}
public void update() {
removeAll();
add(rect, (getWidth() - WIDTH) / 2, (getHeight() - HEIGHT) / 2);
}
public void componentResized(ComponentEvent e) {
add(rect, 20, 20);
update();
}
public void componentMoved(ComponentEvent e) {}
public void componentShown(ComponentEvent e) {}
public void componentHidden(ComponentEvent e) {}
private int WIDTH = 30;
private int HEIGHT = 30;
private GRect rect;
}