java - Trying to get a window to paint on itself when it loads -


this turned out more difficult thought would. i'm doing learning , poking around draw function. want program launch window contains, say, rectangle. size of rectangle scaled on size of window (i.e. if window 1000px , rectangle set scale @ 90%, rectangle 900px). have math figured out on how center , determine it's size, through use of stubs, have found using object.getwidth() , height, etc, returning 0 every time, b0rking math sizing.

after thinking while, i'm assuming because trying obtain width , height object still being constructed. own logic, long math after build parts of constructor, should fine, alas -- appear wrong.

i tried creating separate classes creates frame , 1 makes drawing. drawing object accepts jframe part of constructor , attempts draw on it. instantiate each 1 separately, frame first, drawing object (sending frame object it), doesnt seem work either.

any suggestions?

edit:

per andrew's suggestion. here panel object added 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 window adding 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 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 use getgraphics(...) method when paintcomponent(...) method passed graphics object? also, should invoking super.paintcomponent(g) first statement in method. don't know why commented out.

read section swing tutorial on custom painting working examples.

if need more post sscce don't keep guessing actual code looks like.

edit:

did add display statements see if values used in drawrect(...) method make sense? don't assume math forumulas correct. 1 thing need 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 commented out code (usrscale / 100) = 0 since value converted integer before multiplication done.


Comments

Popular posts from this blog

php - What is the difference between $_SERVER['PATH_INFO'] and $_SERVER['ORIG_PATH_INFO']? -

fortran - Function return type mismatch -

queue - mq_receive: message too long -