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

    Default Multiple images from different classes


    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);
    Erik erik = new Erik();
    Inside Erik we have something like:
    Java Code:
        setImage("image.png", 100, 100)
    private void setImage( String fileName, int x, int y )
        xPos = x;//Global Variables (within the class)
        yPos = y;
            File input = new File(fileName);
            myimage =;//Also global
        } catch (IOException ie) {
            System.err.println("Error:"+ie.getMessage()+" : "+fileName);
    public void paint(Graphics 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
    Bulacan, Philippines
    Rep Power


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

Similar Threads

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