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?
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?
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.
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.
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.
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.