Win a copy of Spring Boot in Practice this week in the Spring 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

How to generate multiple graphics objects Java Swing

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying to generate/draw multiple graphics objects for my game(Snake) in which the graphical objects are circles which when eaten will stop the game causing the game to be over. These circle objects will need to be drawn at random coordinates and the quantity of how many will be drawn will depend on the value the user selects from the JSlider and then to confirm their choice of input they would click a Confirm button.

I have tried to draw these circles through the method poisonApples() which generates a (x,y) coordinate for where the circle needs to be placed. In another method called draw() I have attempted to use a for loop to call the poisonApples() method to generate the circles (poison apples) however many times depending on the input of the JSlider. I am facing two main problems; When the program is run firstly as the Confirm button is clicked, instead of drawing a specific number of circles it just shows the circles switching from different (x, y) positions but I just want to have many circles displayed at different (x, y) coordinates. Secondly, after the user selects the number of apples (circles) through JSlider on first attempt, after the game is over, once they have made their choice again on the number of circles (apples - through JSlider) then the KeyListener stops working and the snake cannot move.

I will paste the code below (Misc code has been taken out due to limit ) as there are two files Main.java and Panel.java but the game logic is all in Panel.java:


As I mentioned, the aim currently is to generate the number of apples (circles) depending on the value gained from the JSlider and put them at different (x, y) coordinates (which I have a function for called poisonApples() ).

Note: Since I have had to remove some misc code you may gain access to all the code and test out my project through this link to my repository on github:  https://github.com/zkhan122/snakeboost

 
Saloon Keeper
Posts: 9437
79
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You only have one each of appleX and appleY, you'd need an array or List of each. Then in draw() you'd need to loop through them and draw your apples.

Also note that in this code the appleX and appleY are local variables so executing this method essentially does nothing.

 
Carey Brown
Saloon Keeper
Posts: 9437
79
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Changed subject heading to be "Java Swing" because that's what you're using, not "Java AWT". Hope this helps.
 
Master Rancher
Posts: 4839
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'd suggest a class to hold the x and y locations of the apples.  
The determining of the apples' locations should be done outside of the draw method, unless the apples are supposed to be in different locations every time the draw method is called.
 
Rancher
Posts: 3272
30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

but I just want to have many circles displayed at different (x, y) coordinates



And you were given a complete working example in your last question showing how to use an ArrayList to hold all the objects you want to paint

then the KeyListener stops working and the snake cannot move.



You should NOT be using a KeyListener. A KeyListener only works when a component has focus. When you click on the button your drawing panel loses focus. Instead you should be using Key Bindings. Key Bindings allow you to use the keyboard even when a component doesn't have focus. Read the Swing tutorial on How ot Use Key Bindings for more information.
 
Zayaan Khan
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Norm Radder wrote:I'd suggest a class to hold the x and y locations of the apples.  
The determining of the apples' locations should be done outside of the draw method, unless the apples are supposed to be in different locations every time the draw method is called.



Well yes the location will be different of the apples every time the method is called since I want it to be randomised
 
Norm Radder
Master Rancher
Posts: 4839
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

 location will be different of the apples every time the method is called


How frequently is the draw method called?  What is the value of DELAY?
Are the applets to be drawn at a new location every time draw is called?  I doubt that is a good solution.
I think a better way would be to set the apples' locations every time the slider is changed.
 
Zayaan Khan
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:You only have one each of appleX and appleY, you'd need an array or List of each. Then in draw() you'd need to loop through them and draw your apples.

Also note that in this code the appleX and appleY are local variables so executing this method essentially does nothing.



Yes I have fixed that by declaring as global variables. In terms of your suggestion of using a data structure to hold the x and y coordinate of the apples, I have attempted to do so:



I have attempted to hold the random x and y coordinates of the poison apples in 2 different integer ArrayLists called pxPos and pYPos and then looped through both to generate random x and y coordinates and add them back into the array list.
As a side note: I am doing this for poisonApple not apple. However, running my program causes a NullPointerException which I am finding hard to debug. Any advice?
 
Norm Radder
Master Rancher
Posts: 4839
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

NullPointerException which I am finding hard to debug. Any advice?


Look at the statement where the NPE happens and find what variable has the null value.  Then back track in the code to see why that variable does not have a valid value.

If you need more help, copy the full text of the error message and paste it here.
Also post all the code where the NPE happens.

How many apples are you trying to draw?  If the slider has a value of 4, the nested loop shown in the code will draw 5*5 = 25 apples.

Having two parallel arrays is a terrible solution.  You need a class that holds both the x and y values for the apple's location.
For example the Point class.
 
Zayaan Khan
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Camick wrote:

but I just want to have many circles displayed at different (x, y) coordinates



And you were given a complete working example in your last question showing how to use an ArrayList to hold all the objects you want to paint

then the KeyListener stops working and the snake cannot move.



You should NOT be using a KeyListener. A KeyListener only works when a component has focus. When you click on the button your drawing panel loses focus. Instead you should be using Key Bindings. Key Bindings allow you to use the keyboard even when a component doesn't have focus. Read the Swing tutorial on How ot Use Key Bindings for more information.



Is there not a way to reintialize focus for the KeyListener? Also how would I transition from KeyListener to KeyBindings?
 
Marshal
Posts: 27289
87
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Zayaan Khan wrote:Is there not a way to reintialize focus for the KeyListener? Also how would I transition from KeyListener to KeyBindings?



First you need to decide how you want your application to work. Right now it seems like you have a component which is there so you can type on the keyboard and the component will react to that typing. Only now you have a problem because it doesn't always react to the typing.

So, what do you want? Should that component always react to typing? If so then you're better off using KeyBindings. Otherwise you're condemned to writing extra code to ensure that that component always has the focus. This is possible to do, but if you decide to add more components to your application then those components can't ever have the focus.

As for transitioning from KeyListener to KeyBinding, you would write KeyBinding code and insert the important bits, which are already present in your KeyListener code, in the appropriate places. The two are not that different. You should be able to track down KeyBinding tutorials on the web, and they should help you to figure out what you don't yet understand.
 
Norm Radder
Master Rancher
Posts: 4839
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is an entry on this page for key bindings:
https://docs.oracle.com/javase/tutorial/reallybigindex.html
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic