Pfad für das laden der Bilder

Möchte ich ein Bild hinzufügen zu einem Bild, aber ich bin nicht in der Lage zu verstehen, wo ich zu halten, sollten die image-Datei und wie muss ich den Pfad des Bildes?

Ich platziert ein Bild in My Document Ordner und bedenkt, dass Weg, aber es gab eine Fehlermeldung.

Ich habe den folgenden code.

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

public class Test extends JPanel {
    BufferedImage image;

    public Test(BufferedImage image) {
        this.image = image;
    }

    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        //Draw image centered.
        int x = (getWidth() - image.getWidth())/2;
        int y = (getHeight() - image.getHeight())/2;
        g.drawImage(image, x, y, this);
    }

    public static void main(String[] args) throws IOException {
        String path = "images.jpeg";
        BufferedImage image = ImageIO.read(new File(path));
        Test contentPane = new Test(image);
        //You'll want to be sure this component is opaque
        //since it is required for contentPanes. Some
        //LAFs may use non-opaque components.
        contentPane.setOpaque(true);
        contentPane.setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(5,5,5,5);
        gbc.weightx = 1.0;
        gbc.weighty = 1.0;
        //Add components.
        for(int j = 0; j < 8; j++) {
            gbc.gridwidth = ((j+1)%2 == 0) ? GridBagConstraints.REMAINDER
                                           : GridBagConstraints.RELATIVE;
            contentPane.add(new JButton("button " + (j+1)), gbc);
        }
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setContentPane(contentPane);
        f.setSize(400,400);
        f.setLocation(200,200);
        f.setVisible(true);
    } }

InformationsquelleAutor Eljay | 2012-01-25

Schreibe einen Kommentar