This week's book giveaway is in the Open Source Projects forum.
We're giving away four copies of Spark in Action and have Jean-Georges Perrin on-line!
See this thread for details.
Win a copy of Spark in Action this week in the Open Source Projects forum!

Kevin Corina

Greenhorn
+ Follow
since Nov 29, 2012
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
5
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Kevin Corina

So I'm working on a really basic "game" just to really test out sprite animation and make a decent, reusable infrastructure.
So basically in GraphicsShell I set up a loopGame that double buffers and renders, and renders then updates.
I set it up to to loop every 30 seconds, in which it calls the draw methods of my background class and player class.
in player, I use to a subclass to load multiple frames into an arrays.

Some animations I would like to be "forced" as the frames cant change until the array finished the animation, (for example, and attack animation would be executed one, and not repeated) while other animations could be changed mid array loop such (such as changing from a left facing run animation to a down facing animation)

GraphicsShell



Player




heres an image.


I feel that my current animation method are inefficient, in your opinion, whats the best way to handle these things?
7 years ago

Tony Docherty wrote:And, whilst this doesn't affect the operation of your code, you've called your variable 'C'. Two things: variables in Java, by convention, start with a lower case letter and you should always use meaningful names for variables. You may know what 'C' is as you've just written the code but in 6 months time you probably won't and more importantly it makes it hard for other people to read and understand your code.



Good idea, I'll try it. Thanks!
7 years ago

Matthew Brown wrote:You've declared your C variable as a local variable within the constructor. You need to declare it as an instance variable if you want to be able to access it in other methods.



OOHHHHH.

Annddd... that did it! Thanks guys! I might come back to this thread later if I encounter something else as I finish my program, but this has been a huge help!

7 years ago

Paul Clapham wrote:You want less cryptic? Check your spelling.



Yeah... I got that XD



So I realized that I was spelling "Performed" as "Preformed" which solved one of my problems.

now what I'm trying to override the Constructor to take a SimpleCounter. Now my real problem is storing it for use in other methods. I can take the SimpleCounter , I can seemingly store it in a object reference, but I cant seem execute increaseValue or returnValue in ActionPerformed.

Here is my ButtonListener.


Thanks for your help so far by the way!
7 years ago

Paul Clapham wrote:

Kevin Corina wrote:I think its bugging out over the fact I overridden the actionPreformed method from GraphicsBox.



What method was that again? No, you didn't override the method you mentioned there. That's because there is no such method to override. There is however another method with a very similar name which you should have overridden, but didn't. Look more carefully at the error message and the line of code you think is right but you know is wrong.



well, that was slightly cryptic, but Ill check it out.
7 years ago
Ok, here we go. Sorry about the delay, been kind of busy lately.

So I made Simple version of the program, to encounter a much smaller, more reasonable set of compilation errors.
I have refrained from posting the code for the Counter Class because isn't being used.

Firestarter



GraphicsBox



When I try to compile it, I get these errors:
SimpleGraphicsBox.java:16: error: constructor ButtonListener in class SimpleGraphicsBox.ButtonListener cannot be applied to given types;
Up.addActionListener(new ButtonListener(A));
^
required: no arguments
found: SimpleCounter
reason: actual and formal argument lists differ in length
SimpleGraphicsBox.java:23: error: SimpleGraphicsBox.ButtonListener is not abstract and does not override abstract method actionPerformed(ActionEvent) in ActionListener
class ButtonListener implements ActionListener {
^
2 errors
I think its bugging out over the fact I overridden the actionPreformed method from GraphicsBox.
Yeah. Any ideas?
7 years ago

Steve Luke wrote:Also: regarding this: Up.CreateActionListener(new UpListener()); in the Firestarter code. A JButton does not have a method named CreateActionListener(). It has a method named createActionListener() (difference is the captial C versus lower case c, Java is case-sensitive). But that method doesn't take an ActionListener as a parameter (is has an empty parameter list) and is probably not accessible by your code. So, not sure what you are trying to do there but whatever it is, it won't work.



Ok, I thought I had returned my code to what it originally was, but I seem to have broken it further. I really appreciate your patience. Ill get this fixed soon.
7 years ago

Steve Luke wrote:

Kevin Corina wrote:What I need to understand is how I can have a non-static counter of interact with static methods inside of inner classes "UpListener" and "DownListener" Is there a method similar to "AddActionListener", but non-static? That way I could make two new actionListeners and THEN apply them to a JButton?


I am not sure what your question is, but JButton#addActionListener() is not static. From what I can see, the one method inside UpListener and DownListener is the non-static method actionPerformed(). I think that SSCCE is needed, and you need to explain in more detail the issue you are having, because what you said doesn't match the code or how JButton or ActionListener (or an interface for that matter) works.



wait, Im sorry this I posted the wrong code. let me repost.

7 years ago
Ok, Ill see what I can do. Have any suggestions though on what I can do to fix the problem?
7 years ago
Ok, Ive been learning java for awhile now, I primarily use "Head First Java" , and I'm self taught. I've written a program called "counter". All counter does is make a JPanel with JButtons and a line of text. Each button when clicked either adds or subtracts from the value in the text field. It is terribly written. Like, seriously terrible. So I decided to properly encapsulate it. Here is the code for the original Counter. RealFirestarter is executed. and Counter runs:



RealFirstarter

All RealFirestarter does is create a new Counter named "A"


Counter



Here is the code for the second program.

Firestarter



GraphicBox



and finally Counter



What I need to understand is how I can have a non-static counter of interact with static methods inside of inner classes "UpListener" and "DownListener" Is there a method similar to "AddActionListener", but non-static? That way I could make two new actionListeners and THEN apply them to a JButton? Obviously second the program isn't finished, it still need to be fixed to update the text field when the buttons are clicked, I want to get through this bug first.

So, yeah. Thats really it. Thanks guys! And please remember, I relatively new to this.

EDIT* took out some unnecessary spaces, and code. Sorry, couldn't really take much out of the code, its all (as far as I can tell) relevant to my issue.
EDIT** updated the "addActionListener" methods in the second Firestarter.
7 years ago
Thanks guys! Sorry I took so long to reply, I completely forgot about this thread. I actually completely scrapped the integer array and made it an array of objects that held, and returned, the numbers. I also reworked some of the for loop. Thanks for your help anyways, this community is pretty great.
7 years ago
Hey guys, another question. So in my oh so spectacular book, (Head First Java) I'm around the part called "simple dot com game", and before I moved on to the next section, I wanted to write a small program experimenting the following concepts:

Dealing with arrays
Passing and calling
generating random numbers
enhanced for loops
encapsulation (Not yet implemented)

So, yeah. Why am I getting this result from this code? All I want is it to print out 10 random integers from one to 7.






While it compiles, it gives me an output like this:

0
0
0
0
0
0
0
0
0
0

Yep, that's all.
I think its a rounding issue. Can someone help me? (Keep in mind, I am EXTREMELY NEW to coding.)

Thanks!
7 years ago

Campbell Ritchie wrote:Welcome again.
You have to imagine that the loop creates a temporary variable which is a copy of the next element in the List. You can use that temporary varaible, and change it, but that does not affect the original List/array/whatever. The Oracle link which Jesper quoted does say it doesn’t cover all possibilities, but they preferred to go for simple syntax.



OOOOHHHH... ok... that makes a lot more sense, thanks for the help!
7 years ago
Hey guys, just wondering if anyone could explain how to use an Enhanced For Loop. The book I've been using was kind of vague on the subject, (Head First Java, 2nd Edition) and I kind of want to understand it before I move on. I already did some research online, but was confused more so.


So, keeping in mind I am EXTREMELY new to java, how do you use an Enhanced For Loop, and why would I need to use one?
7 years ago