Trying to get a window to paint on itself when it loads
This turned out to be more difficult than i thought it would. I'm just doing some learning and poking around with the draw function. What I want is for a program to launch a window that contains, say, a rectangle. The size of that rectangle will be scaled on the size of the window (i.e. if the window is 1000px and the rectangle is set to scale at 90%, the rectangle will be 900px). I have the math figured out on how to center it and determine it's size, but through the use of stubs, i have found that using the object.getWidth() and height, etc, is returning 0 every time, completely b0rking the math for sizing.
After thinking about it for a while, i'm assuming that it is because it is trying to obtain the width and height from an object that is still being constructed. To my own logic, as long as you do the math after the build parts of the constructor, it should be fine, but alas -- i appear to be wrong.
I tried creating separate classes that creates a frame and one that makes the drawing. The drawing object accepts a JFrame as p开发者_StackOverflow社区art of the constructor and attempts to draw on it. I instantiate each one separately, frame first, then drawing object (sending the frame object to it), but that doesnt seem to work either.
Any suggestions?
EDIT:
Per Andrew's suggestion. Here is my panel object to be added to the window
package scaling_test;
import java.awt.*;
import javax.swing.*;
public class MyDrawing extends JPanel
{
public MyDrawing() throws HeadlessException
{
this.setVisible(true);
}
public void paintComponent(Graphics g)
{
super.paintComponents(g);
//set scaling
int usrScale = 90;
int scaleRef = (100 - usrScale) / 2;
int xStart = this.getWidth() * (scaleRef / 100);
int yStart = this.getHeight() * (scaleRef / 100);
int width = (usrScale / 100) * this.getWidth();
int height = (usrScale / 100) * this.getHeight();
//draw square outline
g.setColor(Color.green);
g.fillRect(xStart, yStart, width, height);
}
}
And this is the window adding that jpanel object:
package scaling_test;
import java.awt.*;
import javax.swing.*;
public class DrawThis extends JFrame
{
MyDrawing drawing;
public DrawThis() throws HeadlessException
{
drawing = new MyDrawing();
this.add(drawing);
}
}
And the Launcher
package scaling_test;
public class ScaleTest
{
public static void main(String[] args)
{
DrawThis program = new DrawThis();
program.setBounds(250, 250, 800, 600);
program.setVisible(true);
}
}
Updated panel (still doesnt work):
package scaling_test;
import java.awt.*;
import javax.swing.*;
public class MyDrawing extends JPanel
{
public MyDrawing() throws HeadlessException
{
}
public void paintComponent(Graphics g)
{
super.paintComponents(g);
//set scaling
int usrScale = 90;
int scaleRef = (100 - usrScale) / 2;
int xStart = this.getWidth() * scaleRef / 100;
int yStart = this.getHeight() * scaleRef / 100;
int width = usrScale * this.getWidth() / 100;
int height = usrScale * this.getHeight() / 100;
//draw square outline
g.setColor(Color.green);
g.fillRect(xStart, yStart, width, height);
}
}
Why would you use the getGraphics(...) method when the paintComponent(...) method is passed the Graphics object? Also, you should be invoking super.paintComponent(g) as the first statement in your method. I don't know why you commented it out.
Read the section from the Swing tutorial on Custom Painting for working examples.
If you need more help then post your SSCCE so we don't keep guessing what your actual code looks like.
Edit:
Did you add any display statements to see if the values used in the drawRect(...) method make any sense? Don't assume your math forumulas are correct. For one thing you need to understand how integer calculations work:
// int width = (usrScale / 100) * this.getWidth();
// int height = (usrScale / 100) * this.getHeight();
int width = usrScale * this.getWidth() / 100;
int height = usrScale * this.getHeight() / 100;
In the commented out code (usrScale / 100) = 0 since that value is converted to an integer before the multiplication is done.
Do the calculations in the paintComponent(Graphics)
method of a JPanel
that is added to the JFrame
. The paintComponent()
method is only called once the component is 'realized' on-screen.
It's a little tricky without seeing your code, but one option is to setPreferredSize(x, y); on the JFrame, then call pack(); Afterwards, you can do your checks for window size.
精彩评论