Results 1 to 5 of 5

Thread: drawing images

  1. #1
    diggitydoggz is offline Member
    Join Date
    Dec 2008
    Posts
    55
    Rep Power
    0

    Default drawing images

    Hey all. I'm trying to finish the "image" button feature in my paint program. What I want it to do is prompt for an image, and then have that image be created by clicking twice in different spots and drawing it in the rectangle with the two points pressed representing opposite corners. Basically what I need to know how to do is draw an image onto a JPanel at a specified point and into a rectangle of specified dimensions, and have it stretch out to fit those dimensions.

    Here's the code for my program: finished paint!

    Thanks in advance...

  2. #2
    Join Date
    Dec 2008
    Location
    Italy
    Posts
    79
    Rep Power
    0

    Default

    i think this can help you
    java.sun.com/docs/books/tutorial/2d/images/index.html

  3. #3
    diggitydoggz is offline Member
    Join Date
    Dec 2008
    Posts
    55
    Rep Power
    0

    Default

    Thanks for the link. Working through it now. I'm a little confused by this example though:

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.image.*;
    import java.io.*;
    import javax.imageio.*;
    import javax.swing.*;
    
    /**
     * This class demonstrates how to load an Image from an external file
     */
    public class LoadImageApp extends Component {
              
        BufferedImage img;
    
        public void paint(Graphics g) {
            g.drawImage(img, 0, 0, null);
        }
    
        public LoadImageApp() {
           try {
               img = ImageIO.read(new File("strawberry.jpg"));
           } catch (IOException e) {
           }
    
        }
    
        public Dimension getPreferredSize() {
            if (img == null) {
                 return new Dimension(100,100);
            } else {
               return new Dimension(img.getWidth(null), img.getHeight(null));
           }
        }
    
        public static void main(String[] args) {
    
            JFrame f = new JFrame("Load Image Sample");
                
            f.addWindowListener(new WindowAdapter(){
                    public void windowClosing(WindowEvent e) {
                        System.exit(0);
                    }
                });
    
            f.add(new LoadImageApp());
            f.pack();
            f.setVisible(true);
        }
    }
    That was in the tutorial. The program, when I compiled and ran it, cause the image of the strawberry to appear. It is only drawn in the paint method though and I don't see any calls to that method. Why is it that it still was painted?

  4. #4
    neilcoffey is offline Senior Member
    Join Date
    Nov 2008
    Posts
    286
    Rep Power
    7

    Default

    Essentially because "the GUI system" calls a component's paint() method whenever it needs to paint it.

  5. #5
    diggitydoggz is offline Member
    Join Date
    Dec 2008
    Posts
    55
    Rep Power
    0

Similar Threads

  1. Drawing Applet
    By Urgle in forum New To Java
    Replies: 6
    Last Post: 11-19-2008, 09:38 AM
  2. Drawing a map
    By Karp in forum AWT / Swing
    Replies: 4
    Last Post: 11-07-2008, 01:26 PM
  3. Help with 2-D Drawing
    By Deathmonger in forum New To Java
    Replies: 4
    Last Post: 06-18-2008, 03:23 AM
  4. New: Want to understand Drawing...
    By diRisig in forum New To Java
    Replies: 1
    Last Post: 02-05-2008, 09:13 AM
  5. Help with drawing strings!
    By JavaInLove in forum AWT / Swing
    Replies: 1
    Last Post: 02-05-2008, 04:39 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
  •