Cathy McDuff

Greenhorn
+ Follow
since May 09, 2014
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Cathy McDuff

Hi,

So it might be best to have the entire GUI on another thread and have the main thread wait until going through the next element in the loop?

Thank you!
Hi Steve,

So I've done what you asked and now the vehicle isn't moving at all. The vehicle is represented by an image which I move and turn using the rotate and translate methods from the Affine Transform class. Anyway the image isn't moving at all, it's not updating. I've got the repaint(); method and I have added super.paintComponent(g) to but the image does not do anything at all. The program runs but GUI isn't being updating. Any ideas? It looks like I might have to go back to static methods.

Thank you.
Hi Steve,

Thank you for your reply.

So I've done what you advised, I've changed the method to non static. However when I'm calling these methods in other classes it states "Cannot make a static reference to the non-static method turnRight() from the type Movement" and it asking me to change the methods to static. Now I found that if I create an instance of the Movement class like so "Movement m = new Movement();" and then call the method like so "m.method();" it doesn't come up with an error. Is this the right way to go about it? Will I need to create an instance whenever I want to use a method? Is the a good way to code? What's a better way of doing of this or is this the only way?

Many thanks!
Hi,

The method are static because I've added them to the ActionListener of a button. These 2 classes are part of several others.

The program works like this:
Enter commands for the vehicle to move into a text field. Click on the button which performs the commands one by one from a loop. The issue is the for loop doesn't wait for the timer to finish, the for loop just goes from first command to last within a second. That's why only the last command takes any actual affect on the vehicle. I need the for loop to somehow wait for the Timer to finish before going moving on.

I hope this makes sense.

Thank you.
Hi everyone,

Hope you are all well.

Also I'm so sorry for how long winded this is. And I'm new to Java so please forgive me for my lack of knowledge/terminology/Java conventions.

Basically I've created a program which takes user input and moves the vehicle across the surface area. So the user input can be "50 left 4" so that means go 50 meters forward, turn left and go 4 meters forward. For the vehicle I'm using a paint method on a JPanel. When the vehicle moves forward, it initially jumped from one side of the area to another. I wanted to be able to see it moving meter by meter. So I added a Swing Timer which moves the vehicle 1 meter, pauses for a second, moves the vehicle 2 meter and so on.

Now, the problem is that when I enter the commands "50 left 4", the vehicle simply turns left and then moves 4 meters forward. It ignores the first number command. The same happens when I enter "3 4", it will ignore 3 and just move 4 meters forward. Also when I enter "3 left", it will turn left first and then move 3 meters forward. Now I've got methods which takes the user input, chop it up into an array, feed each element of the array through a loop, check if it's an integer or not. If it is an integer it moves the vehicle forward, if not it turns the vehicle either left or right. That all works fine and I'm happy with it.

So what I thought I'd do is have the class which moves the vehicle implement Runnable so that this method will be executed from a separate thread, making the main thread wait and that way it will won't ignore every command except for the last. But that doesn't work either.

Here is the movement class which moves the vehicle forward 1 meter at a time using a Swing Timer.
It implements Runnable.







And here is the code from another class which runs the for loop and thread:




I really appreciate you reading all that, thank you so much!

And thank you to anyone who replies! If you know where I'm going wrong and what I can do to fix it, you're an absolute lifesaver, I really appreciate it
Hi Campbell,

Thank you for your reply!

I did find that thread really helpful. Also I was using the method setToTranslate which was causing the problem. I've changed this to just translate and it's working correctly. Thanks again
9 years ago
Hello everyone!

Hope you are all well.

I'm experimenting with AffineTransform, I've never used it before so I'm not sure where I've gone wrong. Basically I've created just a simple test GUI to see how the code works and all that. Anyway, there are two buttons which turn an image left and right and a third button with moves the image to the right (East). When you first run the program the image is facing down (South). Now to turn left or right I'm using an Affine Transform and the rotate method. That works fine. The issue is when I move the image to the right, it ignore whatever rotation I previously I made (from turn left and right buttons). Therefore when I click to move the image, it faces down again even if it was facing left or up or right before I moved it. It will always be facing down. How do I get it to keep the rotation and just move? Btw I'm using the setTranslate method to move the image.

Below is my code. Forgive me for the messiness - this was just for testing purposes and I apologise for breaking/violating Java conventions and laws - I'm still in the learning process.


I've included all the classes in case you want to compile and run the code to better understand what it's doing wrong.

Images.java



direction.java





testRotate.java




Thank you very much!!

9 years ago
Hi All,

I'm sorry for being absent for so long!

Piet, I can't thank you enough for your help, your code is really useful to play around with and experiment so thank you.

I also appreciate everyone for reading and commenting.

Hi Ed, below is a link to some of the Java playlists I benefited from, I hope they helps you too:

https://www.youtube.com/playlist?list=PLFE2CE09D83EE3E28 - perfect for beginners, simple and straightforward.
https://www.youtube.com/playlist?list=PL6E90696571998DC2 - this guy knows what he's talking about, I find his tutorials full of information.
https://www.youtube.com/playlist?list=PL2B0D071533E7D993 - another great playlist for beginners.

Hi Campbell, thank you and yes you're absolutely right, writing your own code is much better than using someone else's!

Hi Stevey, using a grid of labels wasn't working for me, so I've changed my approach. I'm using the paintComponent method instead to draw a grid of Rectangles and draw a BufferedImage and it's much better than using labels. Less hassle. I say that but I'm already having issues with AffineTransform lol

Thank you all
9 years ago
Hi,

I just wanted to say thank you to Craig for posting that class, it's one of the few that actually makes sense and is easy to follow. Other classes using Affine Transform have been horrific to understand. So yeah, thank you very much, you've been a big help )
9 years ago
Thanks Piet!! That piece of code worked, I cannot believe I didn't see this earlier

Forgive my ignorance.

I have a question for you, not too sure if this is possible to be honest.

But one of my JLabels from the Grid will contain an image. That image is supposed to show me where the character in the program is. I use a JLabal so I can get the position. So basically when I want to move the character, I remove the image from the current JLabel and add it onto the next JLabel. I understand this is possibly the worst approach ever but I am new, all I know is Java and I've got a deadline and didn't think to learn Game Development in Java.

Anyway, since I'm using a JScrollPane, I would like the program to automatically scroll when the JLabel with the image cannot be seen anymore. Therefore as the character is moving, the JSCrollPane is scrolling, so I don't have to scroll to it. It does it for me. So like a camera watching where it's going.

Is this possible?

Thank you very much!!
9 years ago
Hi Piet,

The scrollpane works, it's exactly what I was looking for thank you!

I was hoping you could help me with something else please.

So you've seen what my 100 by 100 grid of labels looks like. Let me explain how I created that. I've got a class. It's got method. I've got a method which creates and populates an array of Strings. Next method creates an array of labels and then adds the String (created from previous method) to the labels using the setText() method. Method after that takes those JLabels and adds them to a JPanel of Grid Layout. Then I've added the JPanel to a scrollpane, the scollpane gets added to another JPanel with an empty border and this final JPanel gets added to the JFrame. So that's how I've created the grid and I'm happy with that, I don't want to change it.

My only issue is that I am unable to amend the image of the JLabels from the main method. I can do this from the method which creates and populate the JLabels, I can do it from the method which creates the grid, but I cannot change the image of a JLabel from the main method. I've tried to create a new method for this and call it in the main method - nothing. I've also tried to change it from a button's ActionListener - nothing again. I know it's possible and I know I've missed something really obvious.

Please could you help with this? Below is the code, if you run it and click on the button you'll notice nothing happens. However the strangest thing is that I can change the background colour of the JLabels from almost anywhere.




Sorry about the messy unorganised code!

Thank you!
9 years ago
Omg, thank you SO much! That's exactly what I've been trying to achieve. Honestly, you've no idea how much I appreciate this, thank you!

I'm just going to add the code to my existing code and I'll let you know how I get on. Fingers crossed
9 years ago
Hi!

Thank you so much for your suggesting!! I used JLabels to create a grid layout and it works, it's exactly what I wanted.

I went away and I've gotten quite a bit done. I've got several classes, method, layouts and buttons etc.

But I have a new dilemma now! I am using a Grid Layout full of JLabels. Now the Grid Layout automatically resizes the cells to try to get all the cells to show up on screen, it tries to accommodate all cells, so no cells are dropped off. However I need a grid which is 100 cells by 100. I created a test grid of this size and it's HUGE. It takes up the entire screen. That's inconvenient. Therefore I have opted for a scroll pane. So I've got a JPanel with Grid Layout. I added this JPanel to a JScrollPane. The only problem is that the grid still resizes itself and the scroll pane is useless to me. How do I get it not to do that?

Also how can I set up a JViewport so that the JScrollPane only shows 30 rows by 30 columns at a time?

I've attaches screenshots of what a 30 by 30 looks like and what a 100 by 100 grid looks like so you can understand why I need the JScrollPane and a JViewport.

I hope I'm making sense.

Many thanks!!



9 years ago
Hi

I basically realised I am approaching this all wrong!! I'm trying to create a grid of tiles, for now it's 25 by 25. But these tiles don't need images on them. I want a player to move one tile to another, rather than pixel by pixel. I want it to hop from one tile to the next. And then I want to be able to get which tile the player has landed on.

For example, say I've got a 10 by 10 tile grid, the player starts off on the first tile which is in the top left hand corner. Then I move the player left by 6 and down by 4. The player should be on tile 47 now. I want to be able to get this information, that the player is on tile 47.

What's the best way to do this? I was thinking of using an array of rectangles. But I read something about using JLabels instead.

Any ideas are appreciated!

Many thanks!


9 years ago
Hi guys,

So I've done what you suggested and I must say my Panel class makes much more sense to me so thank you very much for that.

The only problem I have now is that I don't know how to draw a tile map on my Panel. I've searched for more tutorials and everyone seems to be using tick and render methods instead of paintComponent. I can't find anything where arrays or rectangles are used. I don't really know how to approach this because ideally I want the player to move from tile to another.

Here's what the code looks like now:

Window.java


Panel.java


I've also attached what the Window looks like.

Many thanks!
9 years ago