简体   繁体   English

尝试在JPanel上绘制图像

[英]Trying to paint an Image onto a JPanel

Sorry guys I know there is a lot of info on this topic, but I'm still stuck. 抱歉,我知道关于此主题有很多信息,但我仍然受阻。 I have two panels mainPanel and sidePanel. 我有两个面板mainPanel和sidePanel。 What I'm trying to do is paint an Image to the sidePanel. 我想做的是将图像绘制到sidePanel上。 My sidePanel will have other components such as buttons and labels. 我的sidePanel将具有其他组件,例如按钮和标签。 I can add the Image to the sidePanel using a JLabel, however, sizing and positioning the image is a problem. 我可以使用JLabel将图像添加到sidePanel中,但是,图像的大小和位置是一个问题。 Therefor, I'm experimenting with Graphics g to paint the Image onto the sidePanel instead. 因此,我正在尝试使用Graphics g将Image绘制到sidePanel上。 If anyone could help much would be appreciated. 如果有人可以提供帮助,将不胜感激。 Thanks all that help.` import java.awt.BorderLayout; 谢谢所有的帮助。`import java.awt.BorderLayout;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;



       public class Gui extends JFrame {
    private JPanel j;
    private ImageIcon i;

    public Gui(){
        this.setSize(800,600);
        this.setUp();
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    void setUp(){
        j = new JPanel();
        JPanel contentPanel = new JPanel();
        contentPanel.setLayout(new BorderLayout());
        contentPanel.setSize(new Dimension(800,600));
        JPanel mainPanel = new JPanel();
        JPanel sidePanel = new JPanel();

        sidePanel.setPreferredSize(new Dimension(200,600));
        mainPanel.setPreferredSize(new Dimension(600,600));

        ImagePanel v = new ImagePanel();
        //v.setBackground(Color.BLUE);
        v.setPreferredSize(new Dimension(100,100));

        sidePanel.add(v);

        mainPanel.setBackground(Color.BLACK);
        sidePanel.setBackground(Color.RED);


        contentPanel.add(sidePanel, BorderLayout.WEST);
        contentPanel.add(mainPanel, BorderLayout.CENTER);

        this.add(contentPanel);


    }
       private class ImagePanel extends JPanel{
        public void createImage(Graphics g){
            super.paintComponent(g);
            ImageIcon i = new       ImageIcon("/GUI Practice/src/images.jpeg");
            Image ii = i.getImage();
            g.drawImage(ii, 10, 10, 90, 90, Color.WHITE, this);

            repaint();
            validate();
            updateUI();



        }

    }
           public static void main(String [] args){

        Gui g = new Gui();

    }

  }
`

Taken from bcash's answer here . 从bcash的回答采取这里

public class ImagePanel extends JPanel {

    private BufferedImage image;

    public ImagePanel() {
       try {                
          image = ImageIO.read(new File("image name and path"));
       } catch (IOException ex) {
            // handle exception...
       }
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawImage(image, 0, 0, null); // see javadoc for more info on the parameters            
    }

}

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

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