CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Sep 2011
    Posts
    197

    insets and bounds

    In my head it works like this.

    bounds.width:
    Code:
     ___________________________
     |                                                    |
     |             *bounds.width*             |
     |  <-------------------------------->     |                                           
     |                                                    |
     |__________________________|
    bounds.height:

    Code:
    ___________________________
    |                    ^                              |
    |                     |                              |
    |                     | bonds.height        |
    |__________\/_______________|
    so bounds is the integer value of the distance.

    So the insets must be the coordinates?

    _______________________________________

    Code:
                class sTart 
                   extends JPanel {
    
    
    private Image image;
    private int imageHeight;
    private int imageWidth;
    
    
       
    public sTart(ImageIcon img) {
     
      if(img != null) {
       image = img.getImage();
       imageWidth = image.getWidth(this);
       imageHeight = image.getHeight(this);
       if(imageWidth > 0 && imageHeight > 0) {
        setPreferredSize(new Dimension(imageWidth, imageHeight));
       }
      }
     }
    
    public sTart() {
        setBorder(BorderFactory.createLineBorder(Color.black));
        }
    public Dimension getPreferredSize() {
        return new Dimension(500, 350);
        }
    
    public void paintComponent(Graphics g) {
        super.paintComponent(g);       
    
       Insets insets = getInsets();
       int width = insets.left;
     
        int dX1 = 0;
        int dY1 = 0;
        int dX2 = 0;
        int dY2 = 0;
        int sX1 = 0;
        int sY1 = 0;
        int sX2 = 0;
        int sY2 = 0;
    
        g.drawImage(image,
                     dX1,
                     dY1,
                     dX2,
                     dY2,
                     sX1,
                     sY1,
                     sX2,
                     sY2,
                    this);
        g.drawImage(image, 0, 0, 0, 0, 0, 0, 200, 200, this);
        g.drawString(String.valueOf(width) + String.valueOf(insets.right) + String.valueOf(insets.bottom), 10, 20);
       }  
      }
    ___________[______________________________________________

    Code:
    import javax.imageio.ImageIO;
    import java.io.File;
    import java.awt.Frame;
    import javax.swing.JFrame;
    import java.io.IOException;
    import java.awt.*;
    import javax.swing.*;
    import java.awt.image.ImageObserver;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import javax.imageio.ImageIO;
    import java.io.IOException;
    
    public class machine
                  extends JPanel {
    
    
    public static void main(String[] args) {
    
        SwingUtilities.invokeLater(new Runnable() {
         public void run() {
          start();
         }
        }
       );
      }
    private static void start() {
      JFrame surge = new JFrame("SuRgE!");
      surge.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      ImageIcon menu = new ImageIcon("database//StartMenu.jpg");
      surge.add(new sTart(menu));
      surge.pack();
      surge.setVisible(true);
     }
    }
    why doesn't this display the StartMenu.jpg? If it is because I am using the drawImage incorrectly disreguard this question.
    Last edited by kolt007; January 21st, 2012 at 07:06 PM.

  2. #2
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: insets and bounds

    In my head it works like this.
    Rather than guessing why not read the API docs.

    why doesn't this display the StartMenu.jpg? If it is because I am using the drawImage incorrectly disreguard this question.
    I've recently posted the code to do this very thing. Either search for the post on this site on look on my web site
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured