Window dimension Gcanvas noob question in java
I am a high school student taking cs106a at Stanford via video.
For my current assignme开发者_JAVA百科nt I have to add GObjects and position them relative to the size of the window.
I am currently trying to get the width of the window using the command
I am a high school student taking cs106a at Stanford via video.
For my current assignment I have to add GObjects and position them relative to the size of the window. The Gcanvas is created as one class and implemented in another.
The header and constructor of the class are as follows:
import acm.graphics.*;
import java.awt.event.*;
import java.util.*;
import java.awt.*;
public class NameSurferGraph extends GCanvas
implements NameSurferConstants, ComponentListener {
/**
* Creates a new NameSurferGraph object that displays the data.
*/
public NameSurferGraph() {
addComponentListener(this);
drawGrid();
}
I am currently trying to get the width of the window using the command within the drawGrid() method. Using the command:
int width = getWidth();
however width = 0
One thing that could be causing this: this is one of the first programs I have written using multiple classes.
Thanks for the help!
Since this is being called in from the constructor, it looks unlikely that a width has been set. I haven't used GCanvas, but the java.awt.Canvas.getWidth returns 0 until a width has been set.
精彩评论