
-----------------------------------
tenniscrazy
Sun May 25, 2008 5:05 pm

importing a picture from a file and printing it out into a window
-----------------------------------
I need to learn how to do this..i know it's pretty basic and ply easy, but i have been trying for a couple days now and have gotten nowhere.  As far as i can tell there are a number of ways to import pictures into a program.  I found one that looks relatively simple, and i understand it well enuf.  Could someone please tell me what i did wrong to implement it plz


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.image.*;
 
public class GridSnap extends JFrame {
    int vpw = 800;//viewport width
    int vph = 500;//viewport height
    int gw = 25;//grid width
    int gh = 25;//grid height
 
    BufferedImage buf;//double buffer
    JPanel vp;//viewport for display
    JLabel status;//status bar
    int npx, npy;//current snap coords
    
    BufferedImage img = null;{
        try {
            img = ImageIO.read(new File("BG1.jpg"));
        } catch (IOException e) {
    }
}
    
    public GridSnap() {
        super("Grid Snapping");
 
    status = new JLabel("Status bar");
 
    vp = new JPanel();
    vp.setPreferredSize(new Dimension(vpw, vph));
    vp.addMouseMotionListener(new MouseMotionAdapter(){
        public void mouseMoved(MouseEvent e) {//get the nearest snap point
            int x = e.getX(), y = e.getY();
            int mx = x % gw, my = y % gh;

            npx = x - mx;
            
            npy = y - my;
            
        //    if (mx