0

i am trying to load a jpg image on a canvas where the Line2D line1 is being drawn at. I wanted to overlay the image under the line because i am trying to do a symmetry math question online.

I keep getting the exception error file not found but i have placed my image in the main directory in the java file itself.

Any one can help??

public SliderControlPaintLine() {
JPanel controlPanel = new JPanel();
controlPanel.setLayout(new GridLayout(2,2));
getContentPane().add(controlPanel, BorderLayout.NORTH);

JLabel label1 = new JLabel("Translate(dx,dy): ");
JLabel label2 = new JLabel("Rotate(Theta,ox,oy): ");
JLabel label3 = new JLabel("Scale(sx,sy)x10E-2:");

controlPanel.add(label1);

slider1 = createSlider(controlPanel, JSlider.HORIZONTAL, 0, 300, 150, 100, 50);
slider2 = createSlider(controlPanel, JSlider.HORIZONTAL, 0, 300, 150, 100, 50);

controlPanel.add(label2);

slider3 = createSlider(controlPanel, JSlider.HORIZONTAL, 0, 360, 0, 90, 45);

getContentPane().add(canvas);

try {
    image = ImageIO.read(new FileInputStream("symmetry.jpg"));
} catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

setSize(800,700);
setVisible(true);
}

I am showing where i declared and initialized the ImageIO.

  class DrawingCanvas extends Canvas {
public DrawingCanvas() {
  setSize(300, 300);
}

public void paint(Graphics g) {


  super.paint(g);   
  g.drawImage(image, 0, 0, this);
  Graphics2D g2D = (Graphics2D) g;

  g2D.translate(transX, transY);
  g2D.rotate(rotateTheta, rotateX, rotateY);
  g2D.scale(scaleX, scaleY);
  BasicStroke stroke = new BasicStroke(width);
  g2D.setStroke(stroke);
  Line2D line1 = new Line2D.Float(100f, 200f, 500f, 200f);
  g2D.draw(line1);
}
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
gonggong
  • 67
  • 1
  • 2
  • 17
  • 1) By the time of deployment, those resources will likely become an [tag:embedded-resource]. That being the case, the resource must be accessed by `URL` instead of `File`. See the [info page](http://stackoverflow.com/tags/embedded-resource/info) for the tag, for a way to form an `URL`. 2) Use a consistent and logical indent for code blocks. The indentation of the code is intended to help people understand the program flow. – Andrew Thompson Dec 27 '13 at 08:13
  • Why AWT rather than Swing? See my answer on [Swing extras over AWT](http://stackoverflow.com/a/6255978/418556) for many good reasons to abandon using AWT components. If you need to support older AWT based APIs, see [Mixing Heavyweight and Lightweight Components](http://www.oracle.com/technetwork/articles/java/mixing-components-433992.html). – Andrew Thompson Dec 27 '13 at 08:15

4 Answers4

0

Make sure that your current working directory is the one with the image file. You can get the current directory by calling the following:

System.getProperty("user.dir")

You could be using absolute path too to make sure the correct path. You can get your program's path by calling:

 MyClass.class.getProtectionDomain().getCodeSource().getLocation().getPath()
Attila
  • 3,206
  • 2
  • 31
  • 44
  • 1
    *"You can get your program's path by calling:"* Not for any app. deployed using JWS (common for any desktop app.) or for any applet (whether deployed by JWS or not). Sun decided that it was no business of code to know the installation location, *not even if fully trusted.* – Andrew Thompson Dec 27 '13 at 08:17
0

Which IDE do you use? You need to place your symmetry.jpg file into your working directory. This is mostly configured in the runtime settings.

Codev
  • 1,040
  • 2
  • 14
  • 31
  • I am using Eclipse IDE.. i have put the image file in the src folder in the workspace i have specified.. – gonggong Dec 27 '13 at 08:20
0
try {
        image = ImageIO.read(new File(getClass().getResource("symmetry.jpg").toURI()));
    } catch (URISyntaxException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

Thanks guys for all your comment. I managed to use this to get the image to show. :)

gonggong
  • 67
  • 1
  • 2
  • 17
  • 1
    If `image = ImageIO.read(new File(getClass().getResource("symmetry.jpg").toURI()));` works, then `image = ImageIO.read(getClass().getResource("symmetry.jpg"));` should also work. Not only that, but the 2nd way will work with resources in a Jar, while the first won't. – Andrew Thompson Dec 27 '13 at 08:28
0

Try

ImageIO.write(new BufferedImage(10, 10, BufferedImage.TYPE_3BYTE_BGR), "jpg", new File("symmetry2.jpg"));

and see where the file is written. That is where you have to place your symmetry.jpg

Codev
  • 1,040
  • 2
  • 14
  • 31