• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Detecting when an object crosses a line

 
Ranch Hand
Posts: 146
Mac OS X IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This code here creates a blank pane with a moving circle.

My question is, how would I detect when one of the circles crosses a imaginary line. This would then spawn another circle. When that crosses the line, it spawns another; you get the point.
For clarification, I am trying to create basic fireworks.

Bod
 
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is the line horizontal, vertical, or diagonal?
"Crossing": is that the first edge of the circle touches the line, or the mid point of the circle touches the line, or the last point on the edge touches the line?
 
Bod McLeon
Ranch Hand
Posts: 146
Mac OS X IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:Is the line horizontal, vertical, or diagonal?
"Crossing": is that the first edge of the circle touches the line, or the mid point of the circle touches the line, or the last point on the edge touches the line?


Sorry, forgot that in the text.
The line is Horizontal (going across the pane)
Also, when the mid point of the circle touches it.

Bod
 
Bod McLeon
Ranch Hand
Posts: 146
Mac OS X IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I could be being totally blind, but I don't see an edit button/icon so I created a separate reply.
I went ahead and cleaned up the code and stuff.
 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bod McLeon wrote:I could be being totally blind, but I don't see an edit button/icon so I created a separate reply.  


You're not blind -- the ability to edit is restricted by several factors.  What you did was the correct thing to do.
 
Carey Brown
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Bod McLeon
Ranch Hand
Posts: 146
Mac OS X IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Knute Snortum wrote:

Bod McLeon wrote:I could be being totally blind, but I don't see an edit button/icon so I created a separate reply.  


You're not blind -- the ability to edit is restricted by several factors.  What you did was the correct thing to do.


Thanks! Now I know that I wasn't being stupid.

Carey Brown wrote:


Thanks for the code, will try it out!
Bod
 
Carey Brown
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:


This presumes you have a circle class and a line class. If not, then it would probably be more like
 
Bod McLeon
Ranch Hand
Posts: 146
Mac OS X IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried the code, but I could not seem to implement it into my code. Could someone please help me with this?

Bod
 
Carey Brown
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bod McLeon wrote:
The line is Horizontal (going across the pane)
Also, when the mid point of the circle touches it.



I would think you'd just check if the Y coordinate of the circle's center point is equal to the Y of the horizontal line.

To make the code easier to change, I would try to have a separate method called something like intersects()
 
Bod McLeon
Ranch Hand
Posts: 146
Mac OS X IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for all the help!
Bod
 
Bod McLeon
Ranch Hand
Posts: 146
Mac OS X IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Damn, ran into a new problem; is there a "def" (define) function in java?
As an example, in Python, you can go:

Or something similar.
Is there an alternative like this in Java?
This post was about seeing if you could detect when an object crosses a line:

When that activates, I want another sphere to be drawn on the panel. When that one reaches that point, another will be drawn. It will go on forever.
Bod
 
Carey Brown
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Now you're getting into a little bit of a deeper problem. If you want to draw multiple circles you'll need a Circle class and maintain a list of circles (List<Circle>). Each circle would have a center point (x,y) and a diameter, and perhaps a color, and perhaps x&y increments for moving. The Circle class would implement a draw(Graphics g) method, a move() method, and an intersects() method. In your paintComponent() method you'd iterate through the list of Circles and call each one's draw() method.
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Re "def" functions in Java, your code already has that; they are called methods.

The equivalent to your Python example in Java is
 
Bod McLeon
Ranch Hand
Posts: 146
Mac OS X IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I really don't want to be a pain -  but could someone post the code that includes drawing multiple objects. I have looked up online and found out how to do it; I just can't seem to implement it in my own code.
Bod
 
Bod McLeon
Ranch Hand
Posts: 146
Mac OS X IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Unfortunately there isn't an edit button.
A change for my last post. I said can someone post the code for multiple objects. That is already there. Just for clarification I mean can you post the code when it has been implemented into my main version.
Wording is always a problem!

Bod
 
Bod McLeon
Ranch Hand
Posts: 146
Mac OS X IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ugghhh, so many problems.
I am trying to make a "velocity" for the fireworks.
Th code i have added in is commented out:


When I was making this, I thought that if you just minused X from the timer (the speed of the object) would decrease until a stop. For some reason it either slows very slowly or it moves at the same speed. What is the best way to have a velocity/gravity effect on the object?
Bod
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One common (perhaps the usual) design for animation is to set up a "game loop"

Objects that participate in the game respond to some kind of event that is triggered inside the game loop, like onMove() or onRefresh(), which is called an event handler. The event handler is where you would write code to update your object's status. So, if I had a Car and I could add any number of instances to a Track, the Track would have a game loop that would call each Car's move() method every time the game loop is executed.

My Car might look like this:

And my Track might look like this:

The move() method is called at regular intervals from the Track's game loop. Each Car manages its own speed attribute which can be adjusted by calling its goFaster() , slowdown(), and stop() methods. You can control the overall "refresh rate" by putting a timer in the Track object and making it pause for a certain interval (this can also be made configurable).

NOTE: For simplicity, I didn't include any considerations related to concurrency. A game loop would normally be running in a separate thread so that doesn't block user input.
 
Bod McLeon
Ranch Hand
Posts: 146
Mac OS X IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the help
Bod.
 
Bod McLeon
Ranch Hand
Posts: 146
Mac OS X IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So sorry here, I really don't want to seem annoying; once again I have completely redone my code.
This is the Fireworks class:

This is the Circle class:

What should happen is will permanently being creating circles and moving them. The only problem is that once run, it will create and move one batch of circles, and them it will sit the forever not moving anymore circles.
(To get a better understanding of what I mean, it would be best to run it)
Hope there is a way around it.
Thanks,
Bod
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have a look at this example: https://www.mkyong.com/swing/java-swing-draw-shapes-dynamically-example/

You'll notice that his shape classes (Circle and Star) do not extend JPanel, unlike your shape (Circle) which does.

One thing I would change in that example is this:

Defining a Drawable interface let's you define different classes that know how to draw themselves and respond to the draw(Graphics g) method call. No need to use instanceof because the way it's used in the article is very bad form.
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The names you use here are misleading.

I don't like to use suffixes like "List" and "Array" in names like these because they just redundantly restate the kind of data structure involved (an implementation detail), which is unnecessary and can become misleading if someone decides later on to change the implementation to something else, like from an array to a List or vice versa. The biggest problem here is that you use the name aCircleArray to represent something that's not even an array, it actually references ONE Circle, not an array of Circles.

You could use better names to make the code clearer and avoid "leaking" implementation details. Ideally, you shouldn't have to care if the underlying structure is a List or Array. All you care about is iterating over a collection and accessing each of its elements. So use a name that focuses the reader on the intent/purpose rather implementation.

or alternatively, this:

The problem with this version is that there is only one letter difference between circle and circles. I don't find that to be too big of a deal but other people can sometimes overlook that fact and get confused. <shrug>
 
Bod McLeon
Ranch Hand
Posts: 146
Mac OS X IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks! Will mess with those!
Bod.
 
Can you really tell me that we aren't dealing with suspicious baked goods? And then there is this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic