Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Drawing to a JFrame From Other Classes

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    12
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Drawing to a JFrame From Other Classes

    I'm attempting to port over a very simple game I've written in another language as my very first Java project. However, I'm a bit unsure as to how I should be drawing the images I've loaded from file. The idea that every JComponent has its own drawComponent() method is totally new to me. I come from the experience where each game object manually drew itself onto a shared graphics back buffer, which was then swapped to the front after everything had been drawn.

    My first idea has been to use the JFrame I'm using as my main game window (is this even the way they're intended to be used?) to determine what needs drawing and then do so, but it's probably going to lead to a very bloated class. The ideal way, I'd imagine, would be for me to offload the code responsible for drawing each object to a class of that object, but so far my experiments with trying to have them draw to my main JFrame have all ended in failure. I'm not sure what the "correct" approach to this problem would be. I don't really have any code to show at this point because I haven't gotten past the concept/test phase.

    For a specific example: Would there be a way to have a method of a class draw a Line onto an unrelated JFrame-type class? Google hasn't turned up anything useful, but the wording for this one is a bit tricky. All of the other simple Java game examples I've found use a single class to draw everything, as well as for most of the logic.

    EDIT: Should this actually go in the Java Theory & Questions sub-forum?
    Last edited by Destro; April 18th, 2012 at 04:06 AM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Drawing to a JFrame From Other Classes

    a way to have a method of a class draw a Line onto an unrelated JFrame-type class?
    Each Swing component has a paintComponent() method that will draw in the Graphcis space it occupies in the GUI.
    That method is called when the java program thinks the component's GUI need to be drawn or in response to a call to repaint().
    The paintComponent method is passed a Graphics object that is used to draw on. If you want a method in another class to draw using that Graphics object, call the method and pass it the Graphics object.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    Destro (April 18th, 2012)

  4. #3
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Drawing to a JFrame From Other Classes

    Recommended reading: Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/Swing)

    Just to make Norm's advice (which is spot on correct) more concrete, I'll give you a little example:

    public class GamePanel extends JPanel{
       GameObject gameObject = new GameObject(250, 250); //put it in the middle
     
     
       public void paintComponent(Graphics g){
          super.paintComponent(g);
          gameObject.draw(g);
       }
     
     
       public static void main(String... args){
          JFrame frame = new JFrame("Test Game");
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          GamePanel gamePanel = new GamePanel();
          frame.add(gamePanel);
          frame.setSize(500, 500);
          frame.setVisible(true);
       }
     
    }
     
    public class GameObject{
       int x;
       int y;
     
       public GameObject(int x, int y){
          this.x = x;
          this.y = y;
       }
     
       public void draw(Graphics g){
          g.setColor(Color.GREEN);
          g.fillOval(x, y, 10, 10);
       }
    }


    Hopefully that's pretty straightforward. But usually the basics are this: you extend JPanel (or JComponent) and override paintComponent. You call the super class's paintComponent to handle the background and stuff, then you tell whatever Objects to paint themselves using the Graphics instance given to paintComponent. You then add your JPanel to a JFrame or a JApplet.

    And this is the correct forum for this question.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. The Following 2 Users Say Thank You to KevinWorkman For This Useful Post:

    Destro (April 18th, 2012), Norm (April 18th, 2012)

  6. #4
    Junior Member
    Join Date
    Apr 2012
    Posts
    12
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: Drawing to a JFrame From Other Classes

    Thanks, both of you. It seems that arguments in java are by default passed By Reference (the actual object) rather than By Value (A copy of the object). I've been subliminally trained to always think in terms of passing By Value when dealing with arguments, so it didn't even occur to me to pass along the graphics object from my main JPanel/JFrame.

  7. #5
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Drawing to a JFrame From Other Classes

    Java passes everything by value. (And returns things that way. And assigns things that way.)

    In the expression "gameObject.draw(g);" what gets passed is the value of the variable g not a reference to that variable. draw() receives a copy of the value of the caller's variable: as can be seen from the fact that nothing draw() can do can alter the value of the caller's variable.

    This isn't the default behaviour, it's the only behaviour: there are no references to variables.

    See JavaRanch Campfire - Cup Size: a Story About Variables and JavaRanch Campfire - Pass By Value, Please
    Last edited by pbrockway2; April 18th, 2012 at 11:54 PM.

  8. The Following User Says Thank You to pbrockway2 For This Useful Post:

    Destro (April 19th, 2012)

  9. #6
    Junior Member
    Join Date
    Apr 2012
    Posts
    12
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: Drawing to a JFrame From Other Classes

    Alright then, would it be appropriate to compare this to passing a copy of a pointer to an object (not an actual pointer, but the same idea), as in some other languages? Wouldn't that basically mean that regular variables are passed by value, and object are passed by reference in essence? I'm going off of what I read through the two links you took the time to find for me, and what I found in a couple books. I don't mean to ask questions that the links seem to already answer, I just want to make sure I don't have an incomplete picture of what's happening in this circumstance. Otherwise, I know exactly what I'm dealing with and should be fine.

    EDIT: To clarify where I'm coming from, in the languages I've personally dealt with, passing an object By Value and then using its member functions would have absolutely no result outside of the method because you were only altering the state of a copy. You had to either pass By Reference or pass a pointer (the same thing usually).
    Last edited by Destro; April 19th, 2012 at 05:21 PM.

  10. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Drawing to a JFrame From Other Classes

    Everything is passed by value. A copy of the address of an object is passed allowing a method to get to the object referenced by the "pointer" passed to it.
    There is no way to define an object as a variable. You always get its address in a "pointer" when you use the new statement.
    If you don't understand my answer, don't ignore it, ask a question.

  11. The Following User Says Thank You to Norm For This Useful Post:

    Destro (April 20th, 2012)

  12. #8
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Drawing to a JFrame From Other Classes

    would it be appropriate to compare this to passing a copy of a pointer to an object
    In my opinion, yes. Nonprimitive variables (and other expressions) point to things. You can give a method a copy of their value, and that method can then change the state of the thing pointed to, but *not* change the value of the caller's variable (because it only has a copy of that variable's value). Of course there is no pointer arithmetic...

    Wouldn't that basically mean that regular variables are passed by value, and object are passed by reference in essence?
    No - but the reason is a bit subtle. Variables are not passed at all: their values are. These values are known in Java by a variety of terms - references, reference values, pointers. That only values are passed around (and not variables, operations on which would be visible in the caller's context) keeps the semantics simple. There is little (any?) difference between passing, returning and assigning reference values (pointers) and doing the same things with their primitive brethren.

    passing an object By Value and then using its member functions would have absolutely no result outside of the method because you were only altering the state of a copy
    Yes - and that's a perfectly good way of doing things in other languages. But, to repeat, in Java objects *aren't* passed: reference values (pointers) are.

    You had to either pass By Reference or pass a pointer (the same thing usually)
    Java is a nice illustration of how the semantics differ. If a method gets a hold of a reference to one of the caller's variables, it can alter the value of that variable. If it only gets the value of a pointer, it can alter the state of the thing pointed to but not the value of variables in other contexts. Java doesn't have the former, but does have the latter.

    -----

    I get the impression that you aren't overly worried by this, but just want to clarify how the method calling semantics compares with others you have used. For my money, think "pointers" and you won't go far wrong.

    You should also bear in mind that not only do method calling semantics vary from language to language, but so does the terminology used to describe them! When this (passing a copy of a pointer) technique was first elaborated (in the early 1970's?) its inventors were so struck by the fact that you could alter the state of objects without having to pass that entire state to a method that they chose to call it "pass by reference" and this usage has stuck in some communities. But it does obscure a real difference between PBR and passing (a copy of the value of) a pointer: they might be available as options to allow a programmer to achieve "the same thing", but they have very different effects - that the programmer has to be aware of - on the value of variables in the caller's context. Java has chosen not to implement PBR as a way of "keeping things simple".

  13. The Following User Says Thank You to pbrockway2 For This Useful Post:

    Destro (April 27th, 2012)

Similar Threads

  1. using objects from one jFrame on a different jFrame
    By Taskeen in forum AWT / Java Swing
    Replies: 3
    Last Post: September 14th, 2011, 08:51 AM
  2. connecting jframe to another jframe
    By ajtambalo in forum Member Introductions
    Replies: 2
    Last Post: May 11th, 2011, 11:24 AM
  3. help with JFrame, drawing, and JButtons
    By Khoatic in forum AWT / Java Swing
    Replies: 1
    Last Post: November 19th, 2010, 12:34 AM
  4. [SOLVED] Drawing on JFrame
    By kbarrett1989 in forum AWT / Java Swing
    Replies: 3
    Last Post: October 31st, 2010, 03:41 AM
  5. Replies: 4
    Last Post: July 20th, 2010, 07:15 AM