Results 1 to 4 of 4
  1. #1
    sim18 is offline Member
    Join Date
    Oct 2011
    Posts
    30
    Rep Power
    0

    Default Background image not loading?

    Hi guys so I'm having some trouble with the paint method, could anybody help?

    Java Code:
    class MyDrawingPanel extends JPanel {
        public MyDrawingPanel(Template parentRef)
        {
            this.parentRef=parentRef;
            setPreferredSize(new Dimension(800,600));
            setBackground(Color.WHITE);
        }
        
        public void paint(Graphics g)
        {
            super.paint(g);
        }
    }
    So that is my drawing panel, and here is where it is giving me the error :

    Java Code:
    class MyUIPanel extends JPanel {
        Template parentRef;
        JButton b1=new JButton("Click Me");
        BufferedImage backgroundImage;
        public MyUIPanel(AssignmentTemplate parentRef)
        {
            this.parentRef=parentRef;
            setPreferredSize(new Dimension(300,600));
            setBackground(Color.LIGHT_GRAY);
            setLayout(null);
            add(b1);
            b1.setBounds(20,20,100,30);
        }
        
              public void drawBackground(Graphics g)
    	{
    	    try {
    	    backgroundImage = ImageIO.read(new File("C:\\Temp\\PEGBackground.png"));
    	    } catch (IOException ex) {
    			System.out.println("Cannot find image!");
    		}
    
    	    g.[COLOR="#FF0000"]drawImage[/COLOR](backgroundImage);
        }
    }
    No suitable method found for drawImage - that is the error.

    Can anybody help me? Am I doing the paint method correct?
    I have looked online and read about it but can't seem to do anymore, thank you

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

    Default Re: Background image not loading?

    Take a look at Graphics (Java Platform SE 6).
    The draweImage method requires at least a x and y value and an ImageObserver (could be null)!
    The sense of your overrided paint method is not clear to me - is something missing here? And in swing applications usually we override the paintComponent method instead of the paint method!

  3. #3
    sim18 is offline Member
    Join Date
    Oct 2011
    Posts
    30
    Rep Power
    0

    Default Re: Background image not loading?

    Thank you I will have a look at that and get back to you :)

  4. #4
    sim18 is offline Member
    Join Date
    Oct 2011
    Posts
    30
    Rep Power
    0

    Default Re: Background image not loading?

    I have read up on it, I am getting no errors but am confused as to why the image does not appear on the screen :

    Java Code:
    class MyDrawingPanel extends JPanel {
        Template parentRef;
        private BufferedImage image;
        public MyDrawingPanel(Template parentRef)
        {
            this.parentRef=parentRef;
            setPreferredSize(new Dimension(800,600));
            setBackground(Color.WHITE);
        }
        
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.drawImage(image, 0, 0, null);
        }
    }
    Java Code:
    class MyUIPanel extends JPanel {
        AssignmentTemplate parentRef;
        JButton b1=new JButton("Click Me");
        private BufferedImage image;
        public MyUIPanel(AssignmentTemplate parentRef)
        {
            this.parentRef=parentRef;
            setPreferredSize(new Dimension(300,600));
            setBackground(Color.LIGHT_GRAY);
            setLayout(null);
            add(b1);
            b1.setBounds(20,20,100,30);
        }
        
        public MyUIPanel(){
            try{
                image = ImageIO.read(new File("C:\\Temp\\PEGBackground.PNG"));
            } catch (IOException ex) {
                System.out.println("No such file");
            }
        }

    What I am trying to do, is from the class MyUIPanel, it will get my background image. How do I then get MyDrawingPanel class to draw that image onto the screen?
    I have put g.drawImage image, which is what I called it in MyUIPanel. Do I need to call the paint component method in MyUIPanel ??

    thank you

Similar Threads

  1. Loading a image with Image and ImageIcon
    By cel0x in forum AWT / Swing
    Replies: 3
    Last Post: 02-15-2012, 11:38 AM
  2. Loading an image and then using it as a background
    By ResidentBiscuit in forum New To Java
    Replies: 3
    Last Post: 11-30-2011, 05:04 PM
  3. Replies: 12
    Last Post: 04-14-2011, 02:58 PM
  4. Loading an image into JDesktopPane as background
    By Rose88 in forum Advanced Java
    Replies: 13
    Last Post: 08-25-2009, 02:10 AM
  5. Image as background
    By Java.child in forum AWT / Swing
    Replies: 2
    Last Post: 10-03-2008, 12:37 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
  •