Results 1 to 2 of 2
Thread: Object access between classes
- 03-04-2009, 06:10 AM #1
Member
- Join Date
- Mar 2009
- Posts
- 2
- Rep Power
- 0
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); } } }
- 03-05-2009, 05:00 AM #2
Member
- Join Date
- Mar 2009
- Posts
- 2
- Rep Power
- 0
Similar Threads
-
Method access or field access
By carderne in forum New To JavaReplies: 2Last Post: 12-06-2008, 06:20 PM -
Classes with object attributes
By RRasco in forum New To JavaReplies: 5Last Post: 11-25-2008, 12:07 AM -
Parsing a superclass object to subclass object dynamicly
By Andrefs in forum Advanced JavaReplies: 1Last Post: 07-22-2008, 04:27 PM -
Help with access a database using Microsoft Access
By cachi in forum JDBCReplies: 1Last Post: 08-07-2007, 07:51 AM -
how to access to an object from a thread
By tamayo in forum New To JavaReplies: 1Last Post: 07-24-2007, 04:24 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks