Results 1 to 2 of 2
  1. #1
    qazplm123890 is offline Member
    Join Date
    Mar 2009
    Posts
    2
    Rep Power
    0

    Smile 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!

    Java 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);
    		}
    	}
    }

  2. #2
    qazplm123890 is offline Member
    Join Date
    Mar 2009
    Posts
    2
    Rep Power
    0

Similar Threads

  1. Method access or field access
    By carderne in forum New To Java
    Replies: 2
    Last Post: 12-06-2008, 07:20 PM
  2. Classes with object attributes
    By RRasco in forum New To Java
    Replies: 5
    Last Post: 11-25-2008, 01:07 AM
  3. Replies: 1
    Last Post: 07-22-2008, 05:27 PM
  4. Replies: 1
    Last Post: 08-07-2007, 08:51 AM
  5. how to access to an object from a thread
    By tamayo in forum New To Java
    Replies: 1
    Last Post: 07-24-2007, 05:24 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •