• 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
  • Paul Clapham
  • Liutauras Vilda
  • Knute Snortum
  • Bear Bibeault
Sheriffs:
  • Devaka Cooray
  • Jeanne Boyarsky
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • salvin francis
Bartenders:
  • Tim Holloway
  • Piet Souris
  • Frits Walraven

Trying to Understand How an ArrayList Stores Graphical Object References

 
Greenhorn
Posts: 11
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using an ArrayList to store references to Graphical Objects drawn with a GeneralPath method. I am trying to cycle through the list
after a MouseListener detects a mouse click.  I am then using getPoint on the mouseclick, and contains point on the graphical object to
detect if the click occurs within the bounds of a given object.  Would it be better to store a GetBounds reference with the object in the ArrayList, or
would it be better to calculate that as I iterate through the ArrayList? If my code would help I can post it. Hoping there is a general approach
that one could recommend.
 
Marshal
Posts: 6829
182
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you using Swing, JavaFX, or some other GUI system?  Probably some code would help, or create an SSCCE (that's a link).
 
Marshal
Posts: 68069
258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Swing. I shall move this thre‍ad to that forum.
 
Rancher
Posts: 3139
27
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I am using an ArrayList to store references to Graphical Objects drawn with a GeneralPath method


Just store the GeneralPath object.

You just invoke the contains(…) method on the GeneralPath object as you iterate through the ArrayList.
 
Eric Madore
Greenhorn
Posts: 11
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seems some code is in order



This is where the difficulty lies, I have tried various methods to getBounds on the Exit, but it keeps returning the origin and size of my JFrame/JPanel



 
Rob Camick
Rancher
Posts: 3139
27
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not really sure what all your code is doing but:

1. your GeneralPath is really just a rectangle with a width and height of 10
2. custom painting code should never update properties of your class. You can't control how often the object will be painted
3. you can pass parameters to your class to control where the object is painted

Using the above suggestions your class might look something like:



1. you pass the x/y location as a parameter
2. You use a Shape to control the object you want to paint. This will allow you to use a GeneralPath, Rectangle, Oval etc.
3. In this case it is easier to just use a Rectangle
4. The getShape() method was added. Now as you iterate through the ArrayList you invoke the getShape() method on each instance and then invoke the contains(…) method.
 
Eric Madore
Greenhorn
Posts: 11
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the responses.

The purpose of the program is to randomly generate a dungeon.  My ultimate goal is for the players to click on a exit and the program randomly generate the next dungeon segment.
I am trying to set it up so that when the player clicks the mouse, the program will iterate through the exit list, identify inside which specific exit the click occurred, and
generate the next dungeon segment appended to that exit.

The reason I have been using General Path instead of a simple draw rectangle, is that I am differentiating a door from a continuing corridor by drawing an arrow to represent the continuing corridor.

Would it be easier to use BitMap graphics to represent these items?
 
Rob Camick
Rancher
Posts: 3139
27
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The purpose of the program is to randomly generate a dungeon.


Which is why you pass parameters to the class so it can be painted in your randomly generated location.

I am differentiating a door from a continuing corridor by drawing an arrow to represent the continuing corridor.


We can only suggest a solution based on the information provided. In the code provided you are simply drawing a Rectangle so I suggested a simpler approach.

If you need a GeneralPath it doesn't change the suggestion. A GeneralPath is a Shape and you can still construct it in the constructor, not in the painting method. So you can still use the getShape() method and the contains() method.
 
Eric Madore
Greenhorn
Posts: 11
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I understand, just was trying to explain what I was trying to accomplish.  But now I (think!) I understand.  Thanks again for all of the responses!
Will post code once it is working.
 
Bartender
Posts: 3769
154
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Eric,

I thought of a method that makes the lookup of the Dungeon in question unnecessary. That way has some positives and some negatives. The negatives are that it makes things complex and that it requires quite some code. The positives are that you practise some OOP and that it is quite fun (well, said I).

It makes use of a JPanel with null layout and as many JLabels as required.

For nstance, I have this class:
And I have this method:
And in the constructor of the main class:
The panel does not need an overridden paintComponent. the JLabels take care of themselves.
I have also a class that uses Bezier curves, and is applied likewise.

As I said, more fun than useful, but maybe you find something in it you can use.
 
Eric Madore
Greenhorn
Posts: 11
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@ Piet Souris  So initially I had considered using JButtons with "custom" graphics. Figured it would be easier to program on click as I had done that before.
Then I decided to stick with the current plan to try to better understand Java overall.  That being said, is there a "best practice"? Or is the idea
to use what you can figure out? (That was only partially in jest!).  I may still go this route if I cannot figure out the Graphics solution.  

@ Rob Camick  Please excuse my density, but do I use a setter and getter for a Shape parameter that is saved as part of the Exit instance in the ArrayList?
                     I am not asking for the code, I just want to make sure that I grasp the process. Because I thought I did at first.  
 
Piet Souris
Bartender
Posts: 3769
154
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Eric,

first of all, I never played D&D myself, so I can't say what I coded is anything useful. I just wanted to give an alternative to defining many Shapes in a List and draw these in the paintComponent of your panel. That solution might be both easiest and suitable for your game at hand.

The advantages of using JLabels (transparent by default. JButtons are also good, but then you must use ActionListeners), is that you always can assume a coordinate system (0,0) - (width, height), and that it is easy to drag a JLabel around, without affecting the coordinates in the Shape (but I do not know if that is any relevant for D&D).

Is this 'best practise'? No, I don't think so, it is just 'my best practice'. I come up with an idea 'A', start coding, and usually ending with idea 'Z'. For instance, in my code I create a Shape, create a JLabel around, and that works. I also added a MouseListener to the Label. But while programming it, I thought of using  a Controller for this, and I wouldn't be surprised if it turns out that I need to retrieve a Shape, given a JLabel. Well, I'll find out while coding, but I know a lot of folks around who would be terrified
 
Eric Madore
Greenhorn
Posts: 11
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a solution. So I stripped down my code by commenting out some of the extraneous features for the purpose of my learning.
So I experimented with a couple of ideas, both of which worked.  I started by drawing a simple square using drawRect().  I created Setter/Getters
for the x and y coordinates.  I passed these as instance variables to the MouseListener.  I essentially reconstructed the rectangle and used the contains() method.
The next idea was to use a Setter/Getter for a rectangle object.  Instead of passing coordinates to the MouseListener, I pass a duplicate of the square. Much cleaner code
that way.  

My excitement over passing the rectangle is that I can calculate a rectangle around an arrow shape (which will indicate a continuing passage) and pass that
as a hit box for the mouse click. Would be a mess reconfiguring the arrow in the mouseListener.

The thing that I was not able to figure out (and I believe was the source of my problem) was that I could not cast the objects in my Arraylist to Shape, despite the
fact that they were drawn with GeneralPath().  I am sure this gets back to inheritance, lack of understanding of which is responsible for 80 - 90% of my newbie problems in Java.

Once I finish out the code, I will post it to hopefully help other fledgling Java-ologists, and to ask for recommendations to improve encapsulation as well as Object-Oriented design principles.
 
Campbell Ritchie
Marshal
Posts: 68069
258
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Eric Madore wrote:. . . I could not cast the objects . . . to Shape . . .

GeneralPath implements Shape, so you should be able to cast it to that Shape type. The most likely reason for your problem is that you have your own Shape type, which is taking precedence over java.awt.Shape when you are casting. So that won't compile.
 
Campbell Ritchie
Marshal
Posts: 68069
258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the slice of pie
 
Piet Souris
Bartender
Posts: 3769
154
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The classes in Erics code use a Shape to draw them onto screen, but they do not implement Shape themselves. Now, letting these classes implement Shape means implementing quite some methods. So, an alternative is to have an interface

and let all your classes implement this. As Rob showed, if you define a Shape in your classes (the Shape that gets drawn) you can implementit like this :

Therefore, instead of having a List<Sape> you have a List<Bounds>, and you can cast your objects to Bounds.
 
PI day is 3.14 (march 14th) and is also einstein's birthday. And this is merely a tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!