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:
package objectclasstrain;
import javax.swing.SwingUtilities;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Dimension;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class ObjectClassTrain {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
@Override
public void run(){
createAndShowGUI();
}
});
}
public static void createAndShowGUI(){
JFrame f = new JFrame("JFrame test");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setResizable(false);
f.add(new myPanel());
f.pack();
f.setVisible(true);
}
}
class myPanel extends JPanel{
int room_width = 640;
int room_height = 480;
int x = (int) (Math.random() * 640);
int y = (int) (Math.random() * 480);
int xx = (int) (Math.random() * 640);
int yy = (int) (Math.random() * 480);
public myPanel(){
Square squareRed2 = new Square();
squareRed2.setXposition(1); squareRed2.setYposition(1);
System.out.println(squareRed2.getXposition());
System.out.println(squareRed2.getYposition());
Square squareRed = new Square();
squareRed.setXposition(128); squareRed.setYposition(128);
System.out.println(squareRed.getXposition());
System.out.println(squareRed.getYposition());
addMouseListener(new MouseAdapter(){
@Override
public void mousePressed(MouseEvent e){
}
});
addMouseMotionListener(new MouseAdapter(){
@Override
public void mousePressed(MouseEvent e){
}
});
}
public int checkCollision(int x, int hspeed, int compare){
if (x > compare)
{hspeed = -1;}
else
if (x < compare)
{hspeed = 1;}
return hspeed;
}
@Override
public Dimension getPreferredSize(){
return new Dimension(room_width, room_height);
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
squareRed2.draw(g);
squareRed.draw(g);
}
private static class squareRed2 {
public squareRed2() {
}
}
}
class Square extends JPanel{
private int Xposition;
private int Yposition;
private int hspeed;
private int vspeed;
public void setXposition(int x){
Xposition = x;
}
public void setYposition(int y){
Yposition = y;
}
int room_width = 640;
int room_height = 480;
/* public Square(){
Xposition = (int) (Math.random() * room_width);
Yposition = (int) (Math.random() * room_height);
}*/
//int XpositionINT = (int) Xposition;
//int YpositionINT = (int) Yposition;
public int getXposition(){
return Xposition;
}
public int getYposition(){
return Yposition;
}
public int getHspeed(){
return hspeed;
}
public void draw(Graphics g){
g.setColor(Color.BLACK);
g.fillRect(Xposition, Yposition, 32, 32);
g.setColor(Color.RED);
g.drawRect(Xposition, Yposition, 32, 32);
repaint();
}
}
And I dont know how I should gain acces to the objects :/
Originally Posted by
jps
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