Page 2 of 2 FirstFirst 12
Results 21 to 31 of 31
Like Tree2Likes

Thread: Problem with Recangle Array

  1. #21
    nicephotog is offline Member
    Join Date
    Mar 2012
    Posts
    41
    Rep Power
    0

    Default Re: Problem with Recangle Array

    oops! sorry about that , i apoligise for being so hasty and asleep at it
    Graphics g = componentToPaintOnto.getGraphics();
    Second because its a JFrame and you want to paint on it, it is not actually a good idea although you can do that.
    with the paint() method there you would call it in the constructor.

    However,make a JPanel (be sure the object type and variable declarations are made globally(at the top e.g. JLabel mainlabel;))

    JLabel mainlabel = new JLabel(); // JLabels are for painting on
    BorderLayout bout=new BorderLayout(); // the BorderLayout will pull open to max at the borders the JLabel "the way the JFrame pulls open the initial panel" for size. setBackground(new Color(0,255,0)) of your JPanel and some colour again on the JLabel will help you see if it has expanded the panel. If not you can try a borderlayout on the JFrame to find if it will do that for it
    // normally you must supply a setPreferredSize(new Dimension()) in SWING and setMinimumSize(new Dimension()) and then setSize() on any component.
    mainpanel = new JPanel();
    mainpanel.setLayout(bout);
    mainpanel.add(mainlabel,BorderLayout.CENTER); // "BorderLayout.CENTER" is an alias method of writing a "constant field value", most classes have them but may not need to be set. In this one it does
    this.getContentPane.add(mainpanel); // how to add a JPanel to a JFrame in SWING also "this" is the reference to Main() the JFrame class

    now you have it set up to paint onto so move your listeners and such onto the JLabel
    because you paint onto it
    Graphics g = mainlabel.getGraphics(); // because it extends from Component class that has the getGraphics method

    After you need to change any painting on the JLabel you do the AWT draw methods then call
    mainlabel.repaint();


    Painting on a higher component is not easy or wise and often involves overriding paint() as you did but makes calling repaint() on the component difficult and is done that way because paint() should also have been called in your constructor and is probably the only time painting would be required, for a game you need to change things and repaint after changing so use a JLabel.

    remove
    Main m = new Main();
    and replace it with just
    new Main();
    in the
    public static void main(String Args[]){.... method.
    This is because it should not be static . the previous way there would make all of its sibling variables "static" for "m"(and is also only in scope as a variable to the main method called "local scope") which you neither need or is any good in what your doing, it would be no end of trouble to you. if you want to refer to the initial main class JFrame you use the keyword in here "this". In a class it is passed to you can e.g. Otherclass other = new Otherclass(JFrame mainframe=this, int rew,String somestring) to pass it on.



    THAT SHOULD HAVE BEEN
    g.setColor(Color.WHITE);
    g.fillRect(x,y,containerBoxes[i].getWidth(), containerBoxes[i].getHeight());
    g.drawRect(x,y,containerBoxes[i].getWidth(), containerBoxes[i].getHeight());

    you do realise you will need to send x,y int to the methods after you have created them with the loop
    but, another look at the API docs(was looking at the new 7 API docs , not as easy to read)
    reveals you can do what you were doing
    containerBoxes[i].x = xsznumber; //to assign it
    containerBoxes[i].y = ysznumber; //to assign it
    containerBoxes[i].width; //to assign it
    containerBoxes[i].height; //to assign it
    g.fillRect(containerBoxes[i].x,containerBoxes[i].y,containerBoxes[i].width,containerBoxes[i].height); // however "g" is the component to paint onto
    warning: x,y,width and height variables of class rectangle are possibly "static" class fields (sometimes the API docs are not completely written/finnished for a class - very rare).
    Those variable are usually "class fields" static variables.
    In the new context non static the compiler will throw an error saying
    "cannot reference a non static field in a static context"
    you cure each variable of being static by casting to its own type to use it.
    e.g.
    g.fillRect(((int)containerBoxes[i].x),((int)containerBoxes[i].y),((int)containerBoxes[i].width),((int)containerBoxes[i].height)); // however "g" is the component to
    Last edited by nicephotog; 03-26-2012 at 04:17 AM.

  2. #22
    nicephotog is offline Member
    Join Date
    Mar 2012
    Posts
    41
    Rep Power
    0

    Default Re: Problem with Recangle Array

    And the above was a bit hasty too i see. This bit here is about how to use "this" to pass a reference of the class to another because above was too grubby and fast since you appear to not actually know this stuff.

    example call to another class
    Otherclass other = new Otherclass(this(),arew,astring); // this = the variable inside the original Main class

    example constructor in another class
    Main mainframe;// global variable in another class
    Otherclass(Main mainframe, int rew,String somestring){
    //...constructor internal detail...
    this.mainframe=mainframe; // this = the Main variable inside this "other" class
    }

    And i for got to assign these in the above but you know how to do that
    X containerBoxes[i].width --------- ; //to assign it
    X containerBoxes[i].height --------; //to assign it

    And finally, you won't need to understand much about "static" class modifier, but the below warning only occurs(and in context) if there is a mixture of static with non static
    variables, classes, and methods can be "static".
    "cannot reference a non static field in a static context" if that occurs because the variable "is static" cast it.
    Generally you should not use a static version of anything until you really must. It doesn't actually apply here, just get rid of the static context by not using a reference to the main class and call it recursively as i showed you above.

  3. #23
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Problem with Recangle Array

    No! To the original poster, do not listen to nicephotog's well meaning but misguided advice, and do not use getGraphics() on a component if you want stable graphics. The minute you repaint the application, that Graphics object becomes unworkable, and anything drawn with it disappears. Listen to the advice given by Norm.

  4. #24
    nicephotog is offline Member
    Join Date
    Mar 2012
    Posts
    41
    Rep Power
    0

    Default Re: Problem with Recangle Array

    Quote Originally Posted by Fubarable View Post
    No! To the original poster, do not listen to nicephotog's well meaning but misguided advice, and do not use getGraphics() on a component if you want stable graphics. The minute you repaint the application, that Graphics object becomes unworkable, and anything drawn with it disappears. Listen to the advice given by Norm.
    chuckle! , chuckle! chuckle!

    This is painting and my intelectual license and its had over 400 downloads at least in one month this has had 64 after 7 days of exposure on this site
    SidewinderPhotoColourBalancer EBLA2-C17 2 Download - Freeware Files.com - Graphics Category
    here's its home page Nicephotogs SidewinderPhotoColourBalancer
    You need a Graphics or Graphics2D(extends Graphics) object to paint anything

  5. #25
    DarrylBurke's Avatar
    DarrylBurke is online now Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,240
    Rep Power
    19

    Default Re: Problem with Recangle Array

    Rant all you want, but using getGraphics() of a component is not the right way to do custom painting.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #26
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,963
    Rep Power
    8

    Default Re: Problem with Recangle Array

    Darryl's right. Although there are times that calling getGraphics() is the way to go (such as with active rendering), those times are very infrequent and should only be attempted by people who really understand Swing. I'd say a high percentage of the questions related to Swing here are because people are doing active rendering wrong when they don't even need to be doing it at all.

    So, telling the OP (who is already confused about some basics) to use active rendering is like telling somebody trying to learn how to drive that the solution is to get a faster car. It's not helpful, and it says more about your inexperience than it does the OP's inexperience.

    We appreciate you're trying to help, but judging from your often incorrect advice and your desire to prove yourself by bragging about 400 whole downloads, I would say that you don't quite have the experience required to offer real help.

    I recommend you take a closer look at the basic tutorials, especially this one (Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/Swing)), before you try to "help" again.
    Last edited by KevinWorkman; 03-29-2012 at 02:26 PM.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #27
    nicephotog is offline Member
    Join Date
    Mar 2012
    Posts
    41
    Rep Power
    0

    Default Re: Problem with Recangle Array

    So, telling the OP (who is already confused about some basics) to use active rendering
    That is almost what he has done, it near succeeded, he may as well add that one line for the component reference and he must have that for fillrect, drawrect, setcolor.

    and should only be attempted by people who really understand Swing.
    Rubbish!!! it is there to use and for people who wish to use Java as Sun Microsystems designed it and Oracle as promotes and uses it.

    I recommend you take a look at the API docs and you will find in both AWT and SWING the components return a java.awt.Graphics class for painting on most if not all components and that is what Sun Microsystems designed it to have and to do. It rarely returns a Graphics2D class(which i add is for drawing a "Shape" class, a less defined geometric figure).

    nb: it has over 400 downloads becuase it operates in the real world and i know what i'm doing.
    OK, so i was burned out the last few days, but that all.

  8. #28
    DarrylBurke's Avatar
    DarrylBurke is online now Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,240
    Rep Power
    19

    Default Re: Problem with Recangle Array

    Quote Originally Posted by nicephotog View Post
    It rarely returns a Graphics2D class
    Get your facts straight before pronouncing your expertise.

    400 downloads doesn't automagically change bad advice into good.

    db
    Fubarable and KevinWorkman like this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  9. #29
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,963
    Rep Power
    8

    Default Re: Problem with Recangle Array

    Quote Originally Posted by nicephotog View Post
    It rarely returns a Graphics2D class... i know what i'm doing.
    This is what I mean. You're absolutely wrong, and this is an easy assumption to test.

    You're also wrong when you say that the OP was doing active rendering- in fact, he was not. He was painting to an image, and then painting that image using the standard paint methods. Telling him to go from that, which he was already confused about, to active rendering, is pretty terrible advice.

    By the way, what's with all the underline and bold? Your posts are hard enough to follow without randomly switching between fonts.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  10. #30
    nicephotog is offline Member
    Join Date
    Mar 2012
    Posts
    41
    Rep Power
    0

    Default Re: Problem with Recangle Array

    He wanted to know how to fit all the rectangles onto the app window , here's how to get the user screen size after starting the app (then write a calculation to fit and show them).
    This is how much i know, and i apologised for being excessively tired,
    and it remains awt and swing(as the API docs show and prove) "do rarely return from a components methods a Graphics2D" only a Graphics object (cast it to a Graphics2D if you need one - simple).
    Java Code:
    java.awt.Toolkit swdlabtk = (Toolkit)this.getToolkit(); // this JFrame - the Main frame
    // you don't need to get a java.awt.GraphicsConfiguration object - just get screen size from the java.awt.Component returned toolkit
    java.awt.Dimension scrdm=(java.awt.Dimension)[B]swdlabtk.getScreenSize()[/B]; // length x breadth of screen
    int wdloc=new java.lang.Double(scrdm.getWidth())).intValue(); // screen width converted to int
    int htloc=new java.lang.Double(scrdm.getHeight())).intValue(); // screen height converted to int

  11. #31
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,963
    Rep Power
    8

    Default Re: Problem with Recangle Array

    Quote Originally Posted by nicephotog View Post
    and it remains awt and swing(as the API docs show and prove) "do rarely return from a components methods a Graphics2D" only a Graphics object (cast it to a Graphics2D if you need one - simple).
    You're contradicting yourself, and you don't even know it- that's why I'm suggesting that you might not have the experience to really help people, and maybe if the moderators let you know something, you'd do yourself a favor by listening instead of getting defensive about it.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Page 2 of 2 FirstFirst 12

Similar Threads

  1. problem with array
    By adrian110288 in forum New To Java
    Replies: 2
    Last Post: 02-09-2012, 03:15 PM
  2. Replies: 0
    Last Post: 05-12-2011, 07:04 PM
  3. array problem
    By jabo in forum New To Java
    Replies: 2
    Last Post: 03-31-2010, 09:54 AM
  4. array problem
    By wats in forum New To Java
    Replies: 1
    Last Post: 12-12-2007, 07:08 AM
  5. array problem
    By Albert in forum Advanced Java
    Replies: 2
    Last Post: 07-01-2007, 01:13 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •