如何使用JFileChooser将图片加载到现有的白色背景

时间:2017-05-05 18:03:02

标签: java

我创建了一个像程序一样的小画面,我想知道我是否可以将图片(.jpg .png)加载到我的'白色工作表'并绘制它。这是我的源代码:

public class rajzlap extends JPanel implements MouseMotionListener {

private static final long serialVersionUID = 1L;

private BufferedImage image;
private Graphics2D g2;
private int x = -1, y = -1;

private JFileChooser fileChooser;

private int toolbaritem = -5;
private int vastagsag = 1;


public rajzlap() {
    setBorder(BorderFactory.createLineBorder(Color.BLACK));
    addMouseMotionListener(this);
    image = new BufferedImage(3000, 2000, BufferedImage.TYPE_INT_RGB);
    g2 = (Graphics2D) image.getGraphics();
    g2.setColor(Color.WHITE);
    g2.fillRect(0, 0, 3000, 2000);

    fileChooser = new JFileChooser();
    fileChooser.setFileFilter(new FileNameExtensionFilter("kepek (*.jpg)", "jpg"));
    fileChooser.setAcceptAllFileFilterUsed(false);
}

@Override
protected void paintComponent(Graphics g) {
    g.drawImage(image, 0, 0, null);

}

// load from file
public void loadText() {
    if (fileChooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
        try (FileReader fr = new FileReader(fileChooser.getSelectedFile())) {

        } catch (IOException e1) {
            JOptionPane.showMessageDialog(this, "Nem sikerult beolvasni a szoveget!", "Hiba",
                    JOptionPane.ERROR_MESSAGE);
        }
    }
}

@Override
public void mouseDragged(MouseEvent e) {
    if (SwingUtilities.isLeftMouseButton(e)) {
        if (toolbaritem == 0) {
            g2.setColor(Color.BLACK);
            g2.setStroke(new BasicStroke(vastagsag));
            if (x != -1 && y != -1) {
                g2.drawLine(x, y, e.getX(), e.getY());
            }
            x = e.getX();
            y = e.getY();
            repaint();
        } else if (toolbaritem == 1) {
            g2.setColor(Color.RED);
            g2.setStroke(new BasicStroke(vastagsag));
            if (x != -1 && y != -1) {
                g2.drawLine(x, y, e.getX(), e.getY());
            }
            x = e.getX();
            y = e.getY();
            repaint();
        } else if (toolbaritem == 2) {
            g2.setColor(Color.GREEN);
            g2.setStroke(new BasicStroke(vastagsag));
            if (x != -1 && y != -1) {
                g2.drawLine(x, y, e.getX(), e.getY());
            }
            x = e.getX();
            y = e.getY();
            repaint();
        } else if (toolbaritem == 3) {
            g2.setColor(Color.BLUE);
            g2.setStroke(new BasicStroke(vastagsag));
            if (x != -1 && y != -1) {
                g2.drawLine(x, y, e.getX(), e.getY());
            }
            x = e.getX();
            y = e.getY();
            repaint();
        } else if (toolbaritem == 4) {
            g2.setColor(Color.WHITE);
            g2.setStroke(new BasicStroke(vastagsag*2));
            if (x != -1 && y != -1) {
                g2.drawLine(x, y, e.getX(), e.getY());
            }
            x = e.getX();
            y = e.getY();
            repaint();
        }
    }
}

@Override
public void mouseMoved(MouseEvent e) {
    x = -1;
    y = -1;

}

public void pencil() {

    toolbaritem = 0;
    System.out.println(toolbaritem);

}

public void piros() {

    toolbaritem = 1;
    System.out.println(toolbaritem);

}

public void zold() {
    toolbaritem = 2;
    System.out.println(toolbaritem);
}

public void kek() {
    toolbaritem = 3;
    System.out.println(toolbaritem);
}

public void radir() {

    toolbaritem = 4;
    System.out.println(toolbaritem);

}

public void vekony() {

    vastagsag = 1;
    System.out.println(vastagsag);

}

public void kozepes() {

    vastagsag = 5;
    System.out.println(vastagsag);

}

public void vastag() {

    vastagsag = 10;
    System.out.println(vastagsag);

}

以下是我的计划图片,以便更好地理解:

https://i.stack.imgur.com/CWjU9.jpg

1 个答案:

答案 0 :(得分:0)

用白色背景填充图像后,可以在背景上绘制图像。

首先通过硬编码要加载的图像的文件名来使代码正常工作。一旦这个工作,那么你可以从JFileChooser获取文件名。

基本代码是:

g2.fillRect(0, 0, 3000, 2000);
File filename = new File("someFle.jpg");
BufferedImage fileImage = ImageIO.read( filename );
g2.drawImage(fileImage, 0, 0, null);

您可能需要更改x / y偏移以使图像在白色背景上居中。