Win a copy of The Business Blockchain this week in the Cloud forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Need help with applets

 
Ryan Yue
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Everyone,
This is my code for right now and i had a simple question and it was how can i call the method i want which is draw line into the paintComponent or if that is even possible. Im trying to make it so that everytime the mouse is pressed a random line will shoot out at random colors.
Please help thank you
 
Maneesh Godbole
Saloon Keeper
Posts: 11308
16
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The recommended way of custom painting in Swing is via the paintComponent();

In pseudo code
1) Listen to mouse clicks
2) Call repaint() on mouse click. This will trigger the paintComponent(); internally
3) Move all your logic from inside drawLine() to inside of paintComponent (You will need to store the click location x & y as instance variables
 
Tim Moores
Bartender
Posts: 3127
50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I *think* (not quite sure, my Java desktop GUI skills are rusty) that Maneesh got it backwards: extending Applet implies using AWT, not Swing, so all drawing should happen in paint, and you should not override paintComponent. Maybe you should extend JApplet instead, and thus use Swing rather than AWT.
 
Ryan Yue
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i know using JApplet would make it a little easier, but i'm trying to learn how to do in applets and just need some help getting past where i'm at
 
Maneesh Godbole
Saloon Keeper
Posts: 11308
16
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Moores wrote:I *think* (not quite sure, my Java desktop GUI skills are rusty) that Maneesh got it backwards: extending Applet implies using AWT, not Swing, so all drawing should happen in paint, and you should not override paintComponent. Maybe you should extend JApplet instead, and thus use Swing rather than AWT.

My bad. I did NOT see the code in detail. I naturally presumed it was JApplet and thus I wrote
The recommended way of custom painting in Swing
 
Ryan Yue
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
okay so this is what i updated now. im having trouble trying to get the oval to follow the mouse movement without the lines going all crazy. i found that when i put repaint into mousepressed i can get the lines and oval to draw at the where the mouse is pressed, but when i repaint in either the mousemoved or in the paint component, the oval repaints but then the lines go crazy and repaint with it. I saw by watching videos about updating the graphics (g) , but that just made everything look like a strobe light haha which is cool that i figured that out but its not what i need. Can someone please help me with this thank you
 
Ryan Yue
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi so i now revised this again and i think im very close but im just messing up somewhere. im not to sure where ,but i can now get the object to follow and i can have it shoot lines ,but when mouse is pressed it shoots lines but once i move the mouse the spot where the mouse is pressed the lines go crazy... please help thank you
 
Tony Docherty
Bartender
Posts: 3054
59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry but I really don't understand what you are trying to do. Your code currently remembers the point the mouse was last clicked and then each time paint is called it draws a line from this point to a random location - is this what you mean by "the lines go crazy".

If you just want it to draw a line (shoot) in a random direction on a mouse click and then not draw lines again, once you have drawn your first line set "point1" to null.

BTW why are you learning applets when they are an obsolete technology?
 
Ryan Yue
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well we learned both about japplets and applets in class how come applets are obsolete?
 
Tony Docherty
Bartender
Posts: 3054
59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's unfortunate if your school is still teaching applets, I guess if it's part of your course you are you are stuck with having to learn about them.

As to why they are obsolete I suggest you read these threads:

http://www.coderanch.com/forums/t/664211/Applets/java-applets-obsolete
http://www.coderanch.com/t/644781/Applets/java/Java-applet-JavaScript

and this article on why they shouldn't be taught to students anymore: http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/
 
Ryan Yue
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i already have point 1 set to null one top but it still shoots everywhere. Not sure if i have it in the right place.
 
Ryan Yue
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
okay so i did a good about of changes now haha i'm having one problem now and i think it might be just a simple error but i'm not to sure where it is or what to do i have been kinda stuck for a while on this now..
i am trying to after i draw the line how do i get to stop drawing lines after that point because right now it just draw lines everywhere after i click.
THANK YOU
 
Tony Docherty
Bartender
Posts: 3054
59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Being a programmer is not just about writing code, it requires thought and understanding as well. If you find a bug, fix it before going onto the next requirement. Now to fix a bug you have to understand what is happening and understanding what is happening is far easier with less code to consider, so test early and fix bugs immediately.

I already told you how to start to fix this bug, if you didn't understand what to do you should have asked for clarification rather than changing loads of your code as now you are expecting me or someone else to look at your code again and work out where the problem is and how to fix it. I'm not going to do that but I will explain why you have the problem and give a general idea on fixing it.

The problem occurs because the paint method is called many times in response to lots of windowing events as well as your calls to repaint(). You cannot put code in your paint method that you only want to run a specific number of times or at a specific point in time unless you provide some sort of flag (for example a boolean variable) to control when the code can be run and some code should just not be in the paint method at all.

Generating a random direction for your shot is definitely one of those things that should not be done in a paint method. You only have to generate the shot direction when the shot is fired and not when drawing the shot so do it on the mouse click event. If you do this then you will find it stops the crazy lines problem but you will now find that your line is permanently drawn ie never disappears so you have to now decide how long to keep the line drawn for, this could be for example time based or until the ship is moved. Either way you need to add an if statement to the code that draws the line so it stops drawing the line when the firing event is finished.

 
Ryan Yue
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi tony i thank you again for all your help the reason why i changed much of the code was because i noticed that actaully didn't need alot of the code that i had in there and it was probably making me alot more confused because that what i tried to go off of when i was watching videos on youtube to try and learn. i understand now what my problem was and that was the if statement and i needed to use a flag boolean to set the conditions. thank you for all your help i always have the problem with trying to just write a bunch of code and just hope it works without really understanding what im doing... i see now that that is the wrong way to do things
 
Tony Docherty
Bartender
Posts: 3054
59
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Glad I could help
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic