I tried to put the code in mtPanel before but then the drawing functions messed up:
package objectclasstrain;
import javax.swing.SwingUtilities;
import javax.swing.JFrame;
import...
Type: Posts; User: kinkita
I tried to put the code in mtPanel before but then the drawing functions messed up:
package objectclasstrain;
import javax.swing.SwingUtilities;
import javax.swing.JFrame;
import...
Tank you, no it works perfectly :)
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 ---
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...
I've got another problem no :/ I've created two objects of the Square class, squareRed and squareRed2, and I'm trying to draw them each with the code
squareRed2.draw(g);
...
Ahh, I also forgott to put the squareRed.draw(g); after super.paintComponent(g);. But now i did and it works :) thank you
You're right, I should do something like this instead:
public static void draw(Graphics g){
g.setColor(Color.BLACK);
g.fillRect(Xposition, Yposition, 32, 32);
...
okey, I tried to create the draw method, I put it in the Square class:
public static void draw(int x, int y){
setColor(Color.BLACK);
fillRect(x, y, 32, 32);
...
Would a for loop be of best usage here?
for (int i; i < 10; i ++){
i.draw();
}
so then there would be 10 objects, each numbered from 1 to 10, draw in a loop calling them separately.
I understand when you say that there should be a loop, but what do you mean by list? how should it look like?
Just another quick question:
What should I do to be able to create a new square object with properties on a command? for example when button released.
What I thought I would do (which does not...
okey, I think I got it to work now :)
package objectclasstrain;
import javax.swing.SwingUtilities;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Dimension;
import...
So where should I initialize "int x" for it to function?
And how should the setXposition look like, I did this inside class Square, I want squareRed.setXposition(double x); to give the square its...
Do you mean something like this?:
The whole code:
package objectclasstrain;
import javax.swing.SwingUtilities;
import javax.swing.JFrame;
import javax.swing.JPanel;
import...
I think I got it to work now!
The problem was that g.fillRect(squareRed.Xposition, 0, 32, 32); required four ints but I had instead given it a double, an int, an int and another int. (I saw jps's...
The whole code:
package objectclasstrain;
import javax.swing.SwingUtilities;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Dimension;
import java.awt.Color;
import...
So which way is the most efficient way do you think? (I'm sorry if you find me to lazy to google it) I moved "Square squareRed = new Square();" to inside the "class Square" place but after that?...
squareRed is defined in (Probably not a very good choice now when I think of it.)"createAndShowGUI()":
Did you mean this?
I'm using netbeans. If that is not the right error message then I do not know what I should do.
The error message:
I've only been using java for a short period of time. I program stuff, like creating a window, the way the tutorials says I should do it. So I dont know how to create, for example the code in the...
Like this?
public static void createAndShowGUI(){
Square squareRed = new Square();
JFrame f = new JFrame("JFrame test");
Hello everyone!
I've tried to create objects from classes and it worked until I wanted to draw the new objects I created.
package objectclasstrain;
import javax.swing.SwingUtilities;...