The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

Re: A beginner needs help in oop

what should I do to paint every new object?

Put the Square objects in a list and have the paint method go though the list (in a loop) and call each Square object's draw method. You should move the drawing code from the paintComponent() method to the Square class into a new draw() method in the Square class and have the paint method call it with the Graphics object so each Square can draw itself at its own location and with its own color.

Re: A beginner needs help in oop

Originally Posted by Norm

Put the Square objects in a list and have the paint method go though the list (in a loop) and call each Square object's draw method. You should move the drawing code from the paintComponent() method to the Square class into a new draw() method in the Square class and have the paint method call it with the Graphics object so each Square can draw itself at its own location and with its own color.

I understand when you say that there should be a loop, but what do you mean by list? how should it look like?

Re: A beginner needs help in oop

By list I meant something like an ArrayList that would hold instances of the Square class that you want to draw in the paintComponent() method.
Use it something like this:
begin loop through items in list
get next Square object from list
call Square object's draw method
end loop

Re: A beginner needs help in oop

Look at the code in the paintComponent() method where those methods were called.
The methods are in the Graphics class. The paintComponent() method needs to pass the Graphics object it receives to the Square class's draw() method so it can use it.

Should the draw() method use the x and y position values that are in the Square class?

Re: A beginner needs help in oop

Originally Posted by Norm

Look at the code in the paintComponent() method where those methods were called.
The methods are in the Graphics class. The paintComponent() method needs to pass the Graphics object it receives to the Square class's draw() method so it can use it.

Should the draw() method use the x and y position values that are in the Square class?

But to "start" the whole thing, if we just focus on one object, how do I make the the draw method start.
In the paintComponent, none of these worked:
squareRed.draw(g);
squareRed.draw(Graphics g);
squareRed.draw(Graphics);

Re: A beginner needs help in oop

Use some printlns to verify the values of the variables and see if you can find any clues.

A couple things I noticed in the code post #37

Never create objects or make big calculations in the paint or paintComponent methods. When paintComponent is called, every object should already exist, know how to draw itself on a java.awt.Graphics object, and be ready to do so quickly when called. I would suggest leave it as is until you get it working the way you meant to, but know this will be fixed later on.

I see some methods with names including the word set, like setVspeed and setHspeed, but the methods do not really do as the name suggests. Since you += the incoming value to the old value, you really don't set the value. If the current value is 20 and I call a method named setValue(-4); I expect the value to be -4 rather than 16. I would use a different name for the methods that do not "set" the value.

Re: A beginner needs help in oop

Originally Posted by jps

I see some methods with names including the word set, like setVspeed and setHspeed, but the methods do not really do as the name suggests. Since you += the incoming value to the old value, you really don't set the value. If the current value is 20 and I call a method named setValue(-4); I expect the value to be -4 rather than 16. I would use a different name for the methods that do not "set" the value.

I see what you mean. It's just that I'm so used to the fact that you can set a hspeed to an object. Before I decided to try to learn java I used a program called game maker, and in that program you can set a variable hspeed to 1 and then the object with the variable would move in the horisontal direction of 1

Originally Posted by jps

Use some printlns to verify the values of the variables and see if you can find any clues.

Thats weird, the system outputs returned the right x- and y-position, so both suqares should be drawn.

Re: A beginner needs help in oop

Your Methods and Fields are static. Static XPosition, YPosition mean that there is only 1 for the whole class. You are drawing both squares, but the second square you draw is overlaying the first square, because when you set the second square position, you are also setting the XPosition and YPosition for the first square as well. Remove all the static modifiers to get separate instances.

--- Update ---

Likely you will receive a warning when accessing a static method and field from and instantiated object. Static fields and methods are usually/supposed to be set by call class name rather then instance name. ie Square.setXPosition(); rather then redSquare.setXPosition(); As it changes the position for all instances of that class. It will compile both ways, but most IDE will give you a warning, underlining it in yellow for example.

Re: A beginner needs help in oop

Originally Posted by theoriginalanomaly

Your Methods and Fields are static. Static XPosition, YPosition mean that there is only 1 for the whole class. You are drawing both squares, but the second square you draw is overlaying the first square, because when you set the second square position, you are also setting the XPosition and YPosition for the first square as well. Remove all the static modifiers to get separate instances.

Tank you, no it works perfectly

Originally Posted by theoriginalanomaly

Likely you will receive a warning when accessing a static method and field from and instantiated object. Static fields and methods are usually/supposed to be set by call class name rather then instance name. ie Square.setXPosition(); rather then redSquare.setXPosition(); As it changes the position for all instances of that class. It will compile both ways, but most IDE will give you a warning, underlining it in yellow for example.

My IDE did not give me an error when I ran the program, but the IDE is netbeans and free, so that might be why.

--- Update ---

Originally Posted by jps

Never create objects or make big calculations in the paint or paintComponent methods. When paintComponent is called, every object should already exist, know how to draw itself on a java.awt.Graphics object, and be ready to do so quickly when called. I would suggest leave it as is until you get it working the way you meant to, but know this will be fixed later on.

Where do you suggest me to put the code in?
I put it in the the class myPanel but that gives me an error, telling me it needs an identifyer and that the package squareRed2 does not exist.

And when I put the code code in the public myPanel the squareRed/squareRed2.draw(); wont work. It says that it can't find the symbol squareRed/squareRed2.
Do the public myPanel need some sort of returner?

Re: A beginner needs help in oop

I don't know of an IDE that warns about accessing a static member of a class from an instance. In fact static members are able to be accessed by all instances, that is what they are for. Don't worry about not seeing a warning.

Where to put the code depends on the structure of the program.

Try to keep the construction of objects within the constructor or an init method. In this specific case I would consider the JPanel to be part of the ObjectClassTrain.
You never actually create a Train object, instead you create a JFrame with this line: JFrame f = new JFrame("JFrame test"); (This works, sometimes people also extend the JFrame class, thats another story)
Then with this line of code: f.add(new myPanel()); you are calling the myPanel constructor and after that constructor returns you are adding the newly created myPanel to your jframe, f
This all happens within the code block that would be the constructor of your train object (had you used one). So far so good...
Then in the myPanel constructor you have two mouse listeners. (Both of which capture mousePressed events??) This is a good place to call the constructor for the red squares. Much like you called the constructor for the panel in the "would-be-constructor" for the train class. The train needed a jpanel, and the jpanel needs the two squares it wants to draw.
Then in your mousePressed method (and if I remember correctly the other method was mouseDragged), this is where you would call a method on the squares to set the x and y position of the square to the x and y position reported from the mouse in the MouseEvent. Something like: squareRed.setXposition(e.getX()); With a line of code like this inside the mousePressed method, when the mouse is clicked anywhere on the JPanel, the x position of the square will be updated to the x position of the mouse click.
Finally in the paint method the only thing you would need is to call the individual paint methods for super and each square. The call to repaint within the paintComponent method will cause a repaint every time a repaint occurs. Never call repaint from within a paint or paintComponent method. repaint should be called by your program when a change has been made that will cause a change in the display. Like when you click the mouse or drag the mouse, and the square changes location, after the square updates its x and y positions, then it needs to be redrawn.

I just want to mention, the tutorial you are following is intended for people who have learned a little about the main method, constructors, getters and setters and are ready to get into the graphics packages provided. To use this tutorial as a tool to understand the structure of a good program, is not a very good idea, as it is not a very well structured program in the tutorial. If you are getting hung up on these concepts read through some of the earlier tutorials (even if they are a bit less fun)

Re: A beginner needs help in oop

Originally Posted by jps

Try to keep the construction of objects within the constructor or an init method. In this specific case I would consider the JPanel to be part of the ObjectClassTrain.
You never actually create a Train object, instead you create a JFrame with this line: JFrame f = new JFrame("JFrame test"); (This works, sometimes people also extend the JFrame class, thats another story)
Then with this line of code: f.add(new myPanel()); you are calling the myPanel constructor and after that constructor returns you are adding the newly created myPanel to your jframe, f
This all happens within the code block that would be the constructor of your train object (had you used one). So far so good...
Then in the myPanel constructor you have two mouse listeners. (Both of which capture mousePressed events??) This is a good place to call the constructor for the red squares. Much like you called the constructor for the panel in the "would-be-constructor" for the train class. The train needed a jpanel, and the jpanel needs the two squares it wants to draw.
Then in your mousePressed method (and if I remember correctly the other method was mouseDragged), this is where you would call a method on the squares to set the x and y position of the square to the x and y position reported from the mouse in the MouseEvent. Something like: squareRed.setXposition(e.getX()); With a line of code like this inside the mousePressed method, when the mouse is clicked anywhere on the JPanel, the x position of the square will be updated to the x position of the mouse click.
Finally in the paint method the only thing you would need is to call the individual paint methods for super and each square. The call to repaint within the paintComponent method will cause a repaint every time a repaint occurs. Never call repaint from within a paint or paintComponent method. repaint should be called by your program when a change has been made that will cause a change in the display. Like when you click the mouse or drag the mouse, and the square changes location, after the square updates its x and y positions, then it needs to be redrawn.

I tried to put the code in mtPanel before but then the drawing functions messed up:

I just want to mention, the tutorial you are following is intended for people who have learned a little about the main method, constructors, getters and setters and are ready to get into the graphics packages provided. To use this tutorial as a tool to understand the structure of a good program, is not a very good idea, as it is not a very well structured program in the tutorial. If you are getting hung up on these concepts read through some of the earlier tutorials (even if they are a bit less fun)

You're probably right. It's just that it's much more interesting to be able to see the resaults in a window other than terminal