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

    Default Multiple images from different classes

    Hello,

    In the project I'm working on at the moment, I have a number of different images all of which have their methods wrapped up in different classes.

    Everything has been working fine while I've been designing them, but I dont seem to be able to put more than one in the window at the same time. Here's the kind of thing I've been doing:

    The main class:
    Java Code:
    JFrame w = new JFrame();
    w.setSize(800, 600);
    w.addKeyListener(kl);
    w.setDefaultCloseOperation(w.EXIT_ON_CLOSE);
    w.setFocusable(true);
    
    Erik erik = new Erik();
    erik.addKeyListener(kl);
    w.add(erik);
    w.pack();
    w.setVisible(true);
    Inside Erik we have something like:
    Java Code:
    Erik()
    {
        setImage("image.png", 100, 100)
    }
    
    private void setImage( String fileName, int x, int y )
    {
        xPos = x;//Global Variables (within the class)
        yPos = y;
    
        try
        {
            File input = new File(fileName);
            myimage = ImageIO.read(input);//Also global
        } catch (IOException ie) {
            System.err.println("Error:"+ie.getMessage()+" : "+fileName);
        }
    }
    
    public void paint(Graphics g) {
            super.paint(g);
            g.drawImage( myimage, xPos, yPos, null);
        }
    When I added a second class (ie second image) to the main class, it seems to overwrite the first;

    The only thing its because I'm only drawing one images in paint. But then all I can think of doing is having a list of images in the first class (with co-ordinates) and passing the first class as an object to all the other ones. But this does seem rather messy.

    What is the best way to do this?

    Thank you

    Rob Briggs

  2. #2
    nap_patague is offline Member
    Join Date
    Mar 2011
    Location
    Bulacan, Philippines
    Posts
    23
    Rep Power
    0

    Default

    create a list of object let say
    Java Code:
    public class ImageList{
    	private ArrayList list=new ArrayList();
    	public void add(Erik e){
    		do something here
    	}
    	public void paint(Graphics2D g){
    		for(int x=0;x<list.size();x++){
    			Erik e=(Erik)list.get(x);
    			e.paint(g);
    		}
    	}
    }

Similar Threads

  1. help with GUI and multiple classes
    By sssss in forum Advanced Java
    Replies: 14
    Last Post: 01-16-2011, 10:08 PM
  2. Help with multiple frames/classes
    By Celletti in forum AWT / Swing
    Replies: 1
    Last Post: 04-28-2010, 03:18 AM
  3. Multiple Images on a Canvas with SWT
    By jdc in forum SWT / JFace
    Replies: 0
    Last Post: 08-20-2009, 04:05 PM
  4. Adding multiple images to frame
    By 435.mahesh in forum AWT / Swing
    Replies: 1
    Last Post: 04-24-2009, 08:31 PM
  5. Privileged processing of multiple images?
    By peci1 in forum Java Applets
    Replies: 0
    Last Post: 11-20-2007, 11:57 PM

Posting Permissions

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