Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 importing a picture from a file and printing it out into a window
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
tenniscrazy




PostPosted: Sun May 25, 2008 5:05 pm   Post subject: 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


code:
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<gw/2) npx = x - mx;
       //     else npx = x + (gw-mx);
 
        //    if (my<gh/2) npy = y - my;
        //    else npy = y + (gh-my);
 
            status.setText(npx+", "+npy);
            repaint();
        }
    });
    //buffer
    buf = new BufferedImage(vpw, vph, BufferedImage.TYPE_INT_RGB);
 
    add(vp);
    add(status, BorderLayout.SOUTH);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    pack();
    setVisible(true);
}
public void paint (Graphics notused) {
    Graphics2D g = (Graphics2D)buf.createGraphics();
   
    boolean Graphics.drawImage(Image img, 0, 0,ImageObserver observer);
 
    g.setColor(Color.DARK_GRAY);
    //grid vertical lines
    for (int i=gw;i<vpw;i+=gw) {
        g.drawLine(i, 0, i, vph);
        }
        //grid horizontal lines
        for (int j=gh;j<vph;j+=gh) {
            g.drawLine(0, j, vpw, j);
        }
        //show the snapped point
        g.setColor(Color.WHITE);
        if (npx>=0 && npy>=0 && npx<=vpw && npy<=vph) {
            g.drawRect(npx, npy, gw, gh);
        }
 
        vp.getGraphics().drawImage(buf, 0, 0, Color.BLACK, null);
   }
    public static void main(String args[]) {
        new GridSnap();
    }
}
Sponsor
Sponsor
Sponsor
sponsor
HellblazerX




PostPosted: Mon May 26, 2008 12:38 pm   Post subject: Re: importing a picture from a file and printing it out into a window

Shouldn't this:
code:
boolean Graphics.drawImage(Image img, 0, 0,ImageObserver observer);

be this:
code:
g.drawImage (img, 0, 0, null);
tenniscrazy




PostPosted: Tue May 27, 2008 7:53 am   Post subject: Re: importing a picture from a file and printing it out into a window

thanks, that works.

but this part of the code keeps giving me various errors

code:
BufferedImage img;
    try {
        img = ImageIO.read (new File ("BG1.jpg"));
         catch (IOException e) {
    }
}


it says illigal start of type for the try line

it also gives an error for the command 'File'

I have tried various things but all the stuff i found said it should be exactly like this
HellblazerX




PostPosted: Tue May 27, 2008 8:03 am   Post subject: Re: importing a picture from a file and printing it out into a window

You need to import the ImageIO and File classes:
code:

import javax.imageio;
import java.io.File;
tenniscrazy




PostPosted: Wed May 28, 2008 7:41 am   Post subject: RE:importing a picture from a file and printing it out into a window

AH!!!

i got the first one but i had no idea about the second one. OK it should work now...hopefully
tenniscrazy




PostPosted: Mon Jun 02, 2008 8:37 am   Post subject: RE:importing a picture from a file and printing it out into a window

so i found code that works in the java almanac.

It compiles fine, but when i try to run it it the window comes up and it doesn't print the picture, it just compies the image that is behind it on the monitor. Could this be a problem with the computer i'm using, or is it something thats wrong with the code again?
HellblazerX




PostPosted: Mon Jun 02, 2008 3:59 pm   Post subject: Re: importing a picture from a file and printing it out into a window

You're drawing your picture before you draw your background, so your background is covering up your picture. Just reverse the order and you should be fine.
tenniscrazy




PostPosted: Mon Jun 02, 2008 6:34 pm   Post subject: RE:importing a picture from a file and printing it out into a window

my picture is my background.

what i meant is that its copying the background of my desktop and putting it in the window, so it's like see-through exept when you move the window around it keeps the picture of the desktop it started with.

Like when a computer is being laggy and you try and open a minimized window and it does the same thing

Edit: nevermind i got it to work
Sponsor
Sponsor
Sponsor
sponsor
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 8 Posts ]
Jump to:   


Style:  
Search: