• 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

Update the position of an image

 
Greenhorn
Posts: 8
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey there! so I have a task where I have to display an img on a gui, and when i click on it i have to change the pictures position to a new location.
So far i was able to display, the img , and set it to a random location if i launch the code but thats all, i know that i must use the MouseListener as well, but I don't know how to update the postion of the image


 
Marshal
Posts: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch!

When you display the image, you create a Kép_be object and display it at the location which is named by the variables x and y.

Then you want to display another image, correct? So you create another Kép_be object. If you want to display it at a different location, then you need to change the values of x and y, right?
 
Nagy Gerorge
Greenhorn
Posts: 8
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:Welcome to the Ranch!

When you display the image, you create a Kép_be object and display it at the location which is named by the variables x and y.

Then you want to display another image, correct? So you create another Kép_be object. If you want to display it at a different location, then you need to change the values of x and y, right?



It needs to be the same Image, I just need to change the x,y location of the image  on a mouse event.  I'm not sure how could i change the vaules of x and y
 
Nagy Gerorge
Greenhorn
Posts: 8
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Never mind haha i solved it !
 
Marshal
Posts: 79239
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well done Please show us the solution. We might then move you to our GUIs forum.
And welcome to the Ranch (again).
 
Nagy Gerorge
Greenhorn
Posts: 8
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Well done Please show us the solution. We might then move you to our GUIs forum.
And welcome to the Ranch (again).



Thank you!

The solution i hope its right haha

 
Ranch Hand
Posts: 271
6
Eclipse IDE MySQL Database Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry for the off topic question Nagy but why are you Swing, would not Java FX be easier to learn and better, I am not an experienced programmer still in the early stages of learning, so I am simply wondering.
 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Nagy Gerorge wrote:. . . The solution i hope its right haha . . .

Wasn't it simple! I would consider using only one Random object, and also not using the number literal:-That will give slightly different results.

Yosuf Ibrahim wrote:. . . Java FX . . .

Interesting question. Eight years ago, FX was all thee rage, but it seems to have gone into the background a bit. I don't think there is much difference in the difficulty of the two frameworks, but most people no longer use desktop GUIs. I shall let other people state their views because everybody seems to have a different opinion.
 
Yosuf Ibrahim
Ranch Hand
Posts: 271
6
Eclipse IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:... but most people no longer use desktop GUIs.



True, but on the other hand businesses usually still do use desktop GUIs especially big companies if I am correct
 
Saloon Keeper
Posts: 10732
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Keep image from being drawn outside of panel bounds.
 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would use an anonymous class being a subclass of MouseAdapter.
 
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That code is pretty old. This topic contains some code that not only moves an image, but also scales it, if necessary: old topic
 
Paul Clapham
Marshal
Posts: 28226
95
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

Piet Souris wrote:That code is pretty old.



Yes. People used to use MediaTracker when you were connecting with a 14.4 Kbps modem and it would take a significant amount of time for the image to travel from the server to the client.

(I was startled to find it in some code which I wrote rather a long time ago, which I'm still using it.)
 
Piet Souris
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I had never heard of the class MediaTracker. But there is an Swingworker example of loading images : swingworker
 
Nagy Gerorge
Greenhorn
Posts: 8
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for all the reply!

Well I have to use this code such MediaTracker because our professor told us so. We cannot use anything else.
 
Nagy Gerorge
Greenhorn
Posts: 8
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Yosuf Ibrahim wrote:Sorry for the off topic question Nagy but why are you Swing, would not Java FX be easier to learn and better, I am not an experienced programmer still in the early stages of learning, so I am simply wondering.




I have heard its easier but my professor wants us to use this, if we use any else on our exams even though the code its better he will fail us :/
 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Nagy Gerorge wrote:. . . I have heard its easier . . .

Who told you that? I think Swing® and FX are at the same level of difficulty.

my professor wants us to use this . . .

In that case you will have to use Swing®. Please ask for more explanation about the MediaTracker use.
 
Yosuf Ibrahim
Ranch Hand
Posts: 271
6
Eclipse IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Who told you that? I think Swing® and FX are at the same level of difficulty



When I first started learning java I started with Swing and I spent quite time and effort to understand it and work with it. However, with FX the learning process was much easier, I  then spent the pat two years quite occupied and was unable to do any progress in programming. The past few weeks when I got back to it again I found FX was way simpler and better looking than swing.

Nagy Gerorge wrote:
my professor wants us to use this . .



I always hated that I have not been able to afford a proper college education, but comments like these that I hear a lot gives me the idea probably I was lucky not getting the chance
 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Yosuf Ibrahim wrote:. . . I started with Swing and I spent quite time and effort to understand it and work with it. . . . with FX the learning process was much easier,. . .

Maybe FX was easier to learn because you were already familiar with GUIs from Swing®.

. . . a proper college education, but comments like these that I hear a lot gives me the idea probably I was lucky not getting the chance

No, I think you weren't lucky. You learn a lot more than the subject you are reading at College, or at least you should.
Sometimes that sort of requirement is part of the course; if it says, “GUIs with Swing®,” then it is necessary to use Swing®.
Sometimes it is necessary to impose such a requirement to acquaint students with the concept of protocols and requirements, or resources provided which don't need any changes.
Sometimes old code is copied with old features unchanged, for example getContentPane() or myBufferedReader().close().
Sometimes it is for special purposes; you draw triangles of *s to gain practice writing loops.
And sometimes it is a mistake.

It is worth asking for more explanation about such stipulations.
 
Bartender
Posts: 262
12
IntelliJ IDE Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Yosuf Ibrahim wrote:

Campbell Ritchie wrote:... but most people no longer use desktop GUIs.



True, but on the other hand businesses usually still do use desktop GUIs especially big companies if I am correct



JavaFX is still used, of course. That includes Swing too, although I do wish people would start with JFX more, if only because it's so much easier to make a great looking UI that users will actually like. I won't deny though, it's definitely an uphill battle getting some people to break out of the "only web" mindset even in cases where a desktop UI makes more sense.

Web has certainly become the default. But desktop applications remain useful for non-mainstream use cases. JavaFX is still used for visualizations in "sciencey" or other niche cases (Ex. NASA, DOD), and I've seen some blockchain, machine learning and other visualization stuff too before. There's a company named Gluon which supports JFX running on mobile devices (well, any device really), and I can't imagine they'd still exist if they had no customers.

And I'm sure there's a lot of other business use-cases that don't get visibility either due to security reasons or because businesses and governments simply don't advertise their internal applications. Sometimes a web application just isn't appropriate. And with Firefox recently discontinuing "Progressive Web Apps" which could let web stuff function while offline, I would still strongly favor a desktop application for something that needs to work without a connection.

Is it hugely popular? No, that much is obvious. It'll probably never be hugely popular, and if you're trying to target thousands or millions of "casual" users, given the state of technology today, you basically have to go web. But just because something is niche doesn't make it useless or "dead" (a word developers seem to love to throw at technologies that they want to tear down for one reason or another).

Maybe the day will come, somehow, but it's hard for me to see the end of desktop applications or desktop PCs in favor of web and mobile for everything. Even the latest trendy popular IDE (VS Code) is a desktop application. Personally if I had to do all my work on a website (or even worse, on something other than a desktop PC), I think I'd have to find a way to go into early retirement (haha).
 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The result of disagreeing with me like that is a cow
 
Yosuf Ibrahim
Ranch Hand
Posts: 271
6
Eclipse IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:The result of disagreeing with me like that is a cow



I absolutely agree that response deserves a cow
 
Lou Hamers
Bartender
Posts: 262
12
IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Haha, thanks!
 
It's a tiny ad only because the water is so cold.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic