Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Using Graphics in a BorderLayout

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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!



 
Rancher
Posts: 3191
29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Rancher
Posts: 3191
29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Rancher
Posts: 3191
29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is your current code:



So the method would be:



And your painting code would be:

 
Brett Freedman
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.


 
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 3191
29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Rob! I'm going to experiment with CardLayout. Very helpful forum you guys got here.
 
Who among you feels worthy enough to be my best friend? Test 1 is to read this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic