Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: drawImage help! :(

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default drawImage help! :(

    I'm trying to draw an image inside a jScrollPane, or in any other scrollable type of panel... can someone please give me a VERY simple method as I'm new to java.. I have read all about jScrollPane on Oracle and about drawImage but can't find a way to do it ... :/ can someone please explain whats wrong?

    BufferedImage img;
    img = ImageIO.read(new File("C:\\apple.png"));
    Graphics g = jScrollPane1.getGraphics();
    g.drawImage(img, 0, 0, 100, 100, rootPane);

    //I also tried to use paintComponents... but .. not successful.


  2. #2
    Forum VIP
    Join Date
    Jun 2011
    Posts
    317
    My Mood
    Bored
    Thanks
    47
    Thanked 89 Times in 74 Posts
    Blog Entries
    4

    Default Re: drawImage help! :(

    It looks correct to me. What is the error message?

  3. #3
    Junior Member
    Join Date
    Nov 2011
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: drawImage help! :(

    I don't get an image showing... the directory is good.. no error message.. it runs, but empty.. :s I've been trying for hours .. just frustrated heh.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: drawImage help! :(

    I also tried to use paintComponents.
    Remove the s from the method name.
    Can you post the code where you override the paintComponent method of a JPanel and call drawImage there?

  5. #5
    Forum VIP
    Join Date
    Jun 2011
    Posts
    317
    My Mood
    Bored
    Thanks
    47
    Thanked 89 Times in 74 Posts
    Blog Entries
    4

    Default Re: drawImage help! :(

    I wrote an article on Image processing a few months ago. It may be worth while checking out.

    This is a stripped down version of the JPanel

     
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.image.BufferedImage;
    import javax.swing.JPanel;
    import java.io.File;
    import java.io.IOException;
    import javax.imageio.*;
     
    public class ImageJPanel extends JPanel {
     
    public BufferedImage image;
     
    public void loadImage(File imageFile) {
    	try {
    		image = ImageIO.read(imageFile);
    	} catch (IOException ex) {
    		String directory = new File(".").getAbsolutePath();
    		System.err.println("Could not open " + imageFile.getAbsolutePath() + " at " + directory);
    		System.exit(-1);
    	}	
    }
     
    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        g.drawImage(image, 0, 0, null);  //draws the image
    }
     
     
    public ImageJPanel() {
            String filename = "myFile.jpg";
            File imageFile = new File(filename);
    	loadImage(imageFile);
    }

  6. #6
    Junior Member
    Join Date
    Nov 2011
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: drawImage help! :(

    ChristopherLowe but then how do you put that into the JPanel you want? or how do you trigger it I tried adding for example:

    ImageJPanel jPanelNew = new ImageJPanel();
    jPanelNew.loadImage("C:\\apple.png");

    also tried: jPanel1.add(jPanelNew); (in netbeans to get the things from that.. but nothing aswell)

    and turned some things around so it does that but still nothing comes up :s.. am I calling it wrong? :s Iam using netbeans, and have a jPanel1 on the gui but.. have no idea how to declare it.. :s

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: drawImage help! :(

    IF you are getting error messages, please copy the full text and paste them here

  8. #8
    Forum VIP
    Join Date
    Jun 2011
    Posts
    317
    My Mood
    Bored
    Thanks
    47
    Thanked 89 Times in 74 Posts
    Blog Entries
    4

    Default Re: drawImage help! :(

    You add the JPanel to a JFrame.

Similar Threads

  1. drawImage() causes endless calls to paintComponent()
    By repaint_forever in forum What's Wrong With My Code?
    Replies: 3
    Last Post: August 10th, 2011, 12:15 PM