如何在屏幕上绘制图像?

时间:2013-07-10 21:05:47

标签: java image

我想使用paint方法将图像添加到屏幕上?我不想使用JLabel,因为我想自己设置位置。

Board.class:

public class Board extends JPanel{
    BufferedImage image;

public Board() {
    try {
        image = ImageIO.read(new File("C:\\Users\\alexa_000\\Pictures\\RocketShip.png"));
    }catch(IOException e) {
        e.printStackTrace();
    }
}
protected void paintComponent(Graphics g) {
    super.paintComponents(g);
    g.drawImage(image, 0, 0, this);
}
public Dimension getPreferredSize() {
    return new Dimension(image.getWidth(),image.getHeight());
}

}

RType.class:

public class RType extends JFrame{
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
                Panel panel = new Panel();
                frame.setContentPane(panel);
                frame.setSize(800,600);
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
        }
    });
}
}

1 个答案:

答案 0 :(得分:2)

你有很多错误,我甚至不知道从哪里开始...而不是在JLabel上显示图像,你可以在某个容器上显示你的图像。像JPanel一样。 这是一个小型演示,演示如何在JPanel上显示图像。代码中的解释。

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

//No need to extend your class with JFrame
public class Board {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                //Instantiate Panel class.
                Panel panel = new Panel();
                //Set instance of Panel class as a content pane for your JFrame
                frame.setContentPane(panel);
                //Avoid calling setSize. Call pack instead
                frame.pack();
                //If you want to position your frame on center of screen, no need
                //for fancy calculations. This is how you can do it.
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });

    }

    /*Image will be displayed on JPanel*/
    private static class Panel extends JPanel {
        BufferedImage image;

        public Panel() {
            try {
                //String you are passing in "new File()" is a path to your image
                image = ImageIO.read(new File("path-to-your-image"));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        /*Override this method to display graphics on JPanel.
        * Do not override paint method!*/
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.drawImage(image,0,0,this);

        }

        /*Override getPreferredSize method so it returns dimensions of your image.
        * Size of your container (Panel) will be equal to size of that image*/
        @Override
        public Dimension getPreferredSize() {
            return new Dimension(image.getWidth(),image.getHeight());
        }
    }
}