JPEG文件无法在BlueJ中正确加载

时间:2019-01-29 04:23:45

标签: java image jpeg

我正在尝试为项目创建名片,但遇到了一个我不知道如何解决的问题。

我想将自己的名为“ ME”的照片上传到我的名片上,但不断出现错误“读取文件时出现问题”。

我尝试将名为“ ME”的图像的图片放入我的BlueJ项目文件夹中,在该文件夹中加载项目但仍然出现错误。

import javax.swing.*;
import java.awt.*;
import java.awt.image.*;
import javax.imageio.*;
import java.io.*;

public class Drawing extends JPanel{
public static void main(String[] a) {
    JFrame f = new JFrame();
    f.setContentPane(new Drawing());
    f.setSize(545, 367);
    f.setVisible(true);


}

public void paintComponent(Graphics g){
    // this statement required
    super.paintComponent(g); 
    setBackground(Color.WHITE);

    int x = 5;
    int y = 0;

    //Orange pattern rectangles
    g.setColor(Color.ORANGE);
    g.fillRect(0, 0, 34, 367); 
    g.fillRect(x + 34 * 2, 0, 34, 367);
    g.fillRect(x + 34 * 4, 0, 34, 367);
    g.fillRect(x + 34 * 6, 0, 34, 367);
    g.fillRect(x + 34 * 8, 0, 34, 367);
    g.fillRect(x + 34 * 10, 0, 34, 367);
    g.fillRect(x + 34 * 12, 0, 34, 367);
    g.fillRect(x + 34 * 14, 0, 34, 367);

    //White pattern rectangles
    g.setColor(Color.WHITE);
    g.fillRect(x + 34 * 1, 0, 34, 367);
    g.fillRect(x + 34 * 3, 0, 34, 367);
    g.fillRect(x + 34 * 5, 0, 34, 367);
    g.fillRect(x + 34 * 7, 0, 34, 367);
    g.fillRect(x + 34 * 9, 0, 34, 367);
    g.fillRect(x + 34 * 11, 0, 34, 367);
    g.fillRect(x + 34 * 13, 0, 34, 367);
    g.fillRect(x + 34 * 15, 0, 34, 367);

    //This is the border line
    g.setColor(Color.black);
    g.drawRect(15,13,500,300);

    //This is the outside white rectangle on top of the border line
    g.setColor(Color.WHITE);
    g.fillRect(0,0,550,13);

    //This draws the left white rectangle to the left of the border line
    g.setColor(Color.WHITE);
    g.fillRect(0,0,15,320);

    //This draws the bottom white rectangle below the border line
    g.setColor(Color.WHITE);
    g.fillRect(0,314,550,15);

    //Draws the Hexagon behind the honeycomb
    g.setColor(Color.BLACK);
    int [ ] x2 = {330, 413, 457, 413, 330, 292};
    int [ ] y2 = {83, 83, 155, 227, 227, 155};
    g.fillPolygon(x2, y2, 6);

    // Draws the honeycomb design
    int x3 = 325;
    int y3 = 90;  
    g.setColor(Color.YELLOW);
    g.fillOval(x3, y3, 50, 50);
    g.fillOval(x3 + 45, y3, 50, 50);
    g.fillOval(x3 + 25, y3 + 40, 50, 50);
    g.fillOval(x3 - 20, y3 + 40, 50, 50);
    g.fillOval(x3 + 70, y3 + 40, 50, 50);
    g.fillOval(x3, y3 + 40 * 2, 50, 50);
    g.fillOval(x3 + 45, y3 + 40 * 2, 50, 50);

    // draw lines
    //g.setColor(Color.BLUE);
    //g.drawLine(190, 50, 190, 150);  
    //g.drawLine(210, 50, 210, 150);  
    //g.drawLine(230, 50, 230, 150); 

    Font myFont = new Font("serif", Font.BOLD, 40);
    g.setFont(myFont);
    g.setColor(Color.black);
    g.drawString("H.I.V.E. Enterprise", 30, 45);

    BufferedImage photo = null;
    try {
        File file = new File("ME.jpg");
        photo = ImageIO.read(file);
    } catch (IOException e){
        g.drawString("Problem reading the file", 100, 100);
    }
    g.drawImage(photo, 10, 10, 150, 225, null);

  }
}

这应该有效,但我不知道为什么?我为此做错了什么?

输入后,我收到以下堆栈跟踪      e.printStackTrace(); 进入catch块:

javax.imageio.IIOException: Can't read input file!
    at javax.imageio.ImageIO.read(ImageIO.java:1301)
    at Drawing.paintComponent(Drawing.java:102)

0 个答案:

没有答案