简体   繁体   English

找不到ImageIO错误文件

[英]ImageIO error file not found

i am trying to load a jpg image on a canvas where the Line2D line1 is being drawn at. 我正在尝试在绘制Line2D line1的画布上加载jpg图像。 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. 我一直在找不到异常错误文件,但是我将映像放在Java文件本身的主目录中。

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. 我正在显示声明和初始化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);
}
}

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()

Which IDE do you use? 您使用哪个IDE? You need to place your symmetry.jpg file into your working directory. 您需要将symmetry.jpg文件放入工作目录中。 This is mostly configured in the runtime settings. 这主要是在运行时设置中配置的。

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. 我设法用它来显示图像。 :) :)

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 那是您必须放置symmetry.jpg的地方

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM