File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Using Graphics in a BorderLayout Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Using Graphics in a BorderLayout" Watch "Using Graphics in a BorderLayout" New topic
Author

Using Graphics in a BorderLayout

Brett Freedman
Greenhorn

Joined: Feb 26, 2010
Posts: 13

Hello! I'm an experienced programmer but very new to Java. I'm working on a basketball game applet. The code is below. When the button is clicked, the ball is moved randomly.

I would like to use a BorderLayout instead of a FlowLayout and place the images/graphics in the Center Region, the button in the South Region. I think I need to use a Panel but can't figure out how to do it. Can anyone show me how to do this?

Thanks!

Brett Freedman
Greenhorn

Joined: Feb 26, 2010
Posts: 13

I've made some progress on this using a Panel. However I haven't figured out how to make the panel update the x and y coordinates and redraw when the button is clicked. Could anyone help?

Here are the image files that are used:
http://www.b1231.com/classichoops/accuratecourtdiagram.jpg
http://www.b1231.com/classichoops/basketball.gif

Thanks!



Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2157
    
    7
I've made some progress on this using a Panel.


Yes, your original approach of overriding paint() and update() is only used with AWT and NOT Swing. Overriding paintComponent() of a JPanel (or JComponent) is the proper approach.

I haven't figured out how to make the panel update the x and y coordinates and redraw when the button is clicked


Brett Freedman
Greenhorn

Joined: Feb 26, 2010
Posts: 13

I haven't figured out how to make the panel update the x and y coordinates and redraw when the button is clicked




Thanks! I also need to update the x and y variables to move the ball. I'm passing them as parameters when I create the DrawingPanel object. Maybe they should be somewhere in my Test class that the DrawingPanel can see. Or do I need to create a new method in DrawingPanel to move the ball?
Brett Freedman
Greenhorn

Joined: Feb 26, 2010
Posts: 13

Also I removed the doublebuffering to make it easier for me to understand. When I put the doublebuffering back, does it go in the Test class or DrawingPanel?
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2157
    
    7
Or do I need to create a new method in DrawingPanel to move the ball?


Yes, that would be the better approach.

When I put the doublebuffering back


Swing is already double buffered, you don't need to worry about that.
Brett Freedman
Greenhorn

Joined: Feb 26, 2010
Posts: 13
Rob Camick wrote:
Or do I need to create a new method in DrawingPanel to move the ball?


Yes, that would be the better approach.



Could you show me what that method should look like?
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2157
    
    7
This is your current code:



So the method would be:



And your painting code would be:

Brett Freedman
Greenhorn

Joined: Feb 26, 2010
Posts: 13
Rob Camick wrote:This is your current code:



So the method would be:



And your painting code would be:



Sorry, I'm still struggling.

I randomly generate a new x, y and want to use those values in my drawing panel. (The 10 and 100 increments are trivial to the logic. They were there to position the ball image more accurately on the court.)

Here is how I've changed my button click




And this is the method I've added to the DrawingPanel class (empty because I'm not sure what to put in it)




Unfortunately, I am now getting a compile error of "cannot find symbol - method moveBall(int, int)" on the line "drawing_panel.moveBall(xPos,yPos);" - which I don't understand at all. I thought I had created a DrawingPanel object called drawing_panel. Additionally I'm not sure what code goes into my moveBall method in DrawingPanel.

I appreciate your time and patience. Java is gradually seeping in to me but it's slow going.


pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Your current code has some non-graphic problems. You declare a JPanel drawing_panel field but never use it, and then in the init method you have a method-local DrawingPanel drawing_panel variable that is placed inside the applet and visualized, but since there's no field with class scope associated with this object, you have no way to reference it easily. So when a method tries to use the class-scope field drawing_panel, it is both null, and since it is declared as a JPanel variable, none of the methods found in DrawingPanel will be available for it to use (unless you cast).

Solution: create a DrawingPanel field called drawingPanel and construct it as a DrawingPanel object so it is not null. Do not redeclare a DrawingPanel object in your init method but rather use the one that has class scope.

e.g.,



Also, the moveBall method is pretty basic. Just update the xPos and yPos fields (be sure to use this.xPos and likewise for yPos so you aren't setting the parameter = to itself) and then call repaint(). Nothing to it really.
Brett Freedman
Greenhorn

Joined: Feb 26, 2010
Posts: 13
Perfect! Thanks Pete for finding and explaining my mistakes. I'm still at the stage where everytime I see code like "Panel panel = new Panel()", my brain kind of goes blank for a second. I probably would have got stuck on this.xPos = xPos, too.

One more question. What is the proper way to replace the drawingPanel component in my layout with another component?

I was thinking it would be something like "remove(drawingPanel, BorderLayout.CENTER)" followed by "add(newPanel, BorderLayout.CENTER)" but apparently not so.

Thanks!
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2157
    
    7
What is the proper way to replace the drawingPanel component in my layout with another component?




Or you can also look into the CardLayout, which manages this for you.
Brett Freedman
Greenhorn

Joined: Feb 26, 2010
Posts: 13
Thanks Rob! I'm going to experiment with CardLayout. Very helpful forum you guys got here.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Using Graphics in a BorderLayout
 
Similar Threads
How to set this component?
Gray box where my applet should be
How to add in button and paint in applet?
how to use image ?
Applet freezing.. Problem with thread?