Object access between classes
Hello! I'm new to Java Forums, but I have a question. In the following code, how could I make the scroll() method into it's own class but let is still be able to access/edit the camera object with the least coding jumble possible. Thank you!
Code:
import java.awt.*;
import javax.swing.*;
public class ScrollTest extends JPanel {
JFrame frame;
static JPanel screen;
Camera camera;
public static void main (String[] args) {
ScrollTest gui = new ScrollTest();
gui.go();
}
public void go() {
setUpGUI();
scroll(camera.getX(), camera.getY(), 1400, 350, 390, 130, 50, 20, 'l');
}
public void setUpGUI() {
frame = new JFrame("Scrolling Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
screen = new JPanel();
camera = new Camera();
screen.setLayout(null);
screen.setSize(350,130);
camera.setLayout(null);
camera.setSize(1400,390);
screen.add(camera);
camera.setLocation(0,0);
frame.getContentPane().add(BorderLayout.CENTER, screen);
//(6,38) pixels used on frame.
frame.setSize(356,168);
frame.setResizable(false);
frame.setVisible(true);
}
public void scroll(int currentX, int currentY, int width, int visWidth, int height, int visHeight, int dist, int msDelay, char dir) {
if (dir == 'l') {
for (int i = currentX; i>-(width - visWidth) && i>=(currentX - dist); i--) {
camera.setLocation(i,0);
try {
Thread.sleep(msDelay);
} catch(InterruptedException ex) {
ex.printStackTrace();
}
}
}
else if (dir == 'r') {
for (int i = currentX; i<=0 && i<=(currentX + dist); i++) {
camera.setLocation(i,0);
try {
Thread.sleep(msDelay);
} catch(InterruptedException ex) {
ex.printStackTrace();
}
}
}
else if (dir == 'u') {
for (int i = currentY; i>=-(height - visHeight) && i>=(currentY - dist); i--) {
camera.setLocation(0,i);
try {
Thread.sleep(msDelay);
} catch(InterruptedException ex) {
ex.printStackTrace();
}
}
}
else if (dir == 'd') {
for (int i = currentY; i<=0 && i<=(currentY + dist); i++) {
camera.setLocation(0,i);
try {
Thread.sleep(msDelay);
} catch(InterruptedException ex) {
ex.printStackTrace();
}
}
}
}
class Camera extends JPanel {
public void paintComponent(Graphics g) {
Image background = new ImageIcon("images\\TestBackground.png").getImage();
g.drawImage(background,0,0,this);
}
}
}