简体   繁体   English

如何在JPanel绘画方法的顶部添加图像?

[英]How do you add an image on top of a JPanel (in a JPanel) paint method?

I am having trouble inserting an image on top of a paint method that I've written. 我在写的绘画方法之上插入图像时遇到问题。 I want to have images overlap the paint method at certain coordinates. 我想让图像在某些坐标处与绘画方法重叠。

My code: 我的代码:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Polygon;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class testguipaint {

    public static void main(String[] args) {
        testguipaint img = new testguipaint();
    }   
    public testguipaint() {
        JFrame frame = new JFrame();
        frame.add(crafting, BorderLayout.CENTER);
        frame.setSize(442, 284);
        frame.setLocationRelativeTo(null);
        frame.setResizable(false);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(3);
    }

    static JPanel crafting = new JPanel() {
        public void paint(Graphics g) {
            Color darkGrey = new Color(153, 153, 153);
            g.setColor(darkGrey);
            g.fillRect(0, 0, 436, 252);
            Color lightGrey = new Color(198, 198, 198);
            g.setColor(lightGrey);
            g.fill3DRect(3, 3, 430, 246, true);
            g.setColor(darkGrey);
            g.fill3DRect(16, 16, 222, 222, true);
            g.fill3DRect(320, 78, 100, 100, true);
            g.fillRect(248, 121, 39, 12);
            Polygon triangle = new Polygon();
            triangle.addPoint(287, 103);
            triangle.addPoint(287, 151);
            triangle.addPoint(311, 127);
            g.fillPolygon(triangle);
            g.setColor(Color.white);
            g.fill3DRect(88, 16, 3, 222, true);
            g.fill3DRect(163, 16, 3, 222, true);
            g.fill3DRect(16, 88, 222, 3, true);
            g.fill3DRect(16, 163, 222, 3, true);
            //BufferedImage image = new ImageIO.read(new File("/minecraft jpeg's/Products/Bread.png"));
            //g.drawImage(image, 44, 191, null);
            //44, 191
        }
    };      
}

Just a suggestion, paint the images last in the paint component. 只是建议,将图像最后绘画在绘画组件中。 This would allow the images to by drawn on top of the other objects. 这样可以将图像绘制在其他对象之上。 Also, I know that you were probably just testing something, but the first letter of every word in your class name should be capitalized. 另外,我知道您可能只是在测试某些内容,但是您班级名称中每个单词的第一个字母都应大写。

not an answer how to overlay or overlap 没有答案如何重叠或重叠

1) testguipaint should be TestGuiPaint more about Java Naming Conventions here or here 1) testguipaintTestGuiPaint更多关于Java命名约定在这里在这里

2) Swing GUI rellated code should be wrapped into invokeLater() , more about in the Initial Threads 2)与Swing GUI相关的代码应包装在invokeLater() ,更多内容请参见“ 初始线程”

3) for painting in Swing JComponents there is method paintComponent() instead of paint() method, more about in the 2D Graphics 3)对于在Swing JComponents中绘画,有方法paintComponent()而不是paint()方法,有关2D图形中的更多内容

Condensing the imports only for short code here, implementing only 2 of the suggestions MKorbel made (1 and 3), extending JFrame for brevity too, removed instance from main, which was never used. 这里只压缩短代码的导入,仅实现MKorbel提出的2条建议(1和3),也为了简洁起见扩展了JFrame,从main中删除了实例,但从未使用过。 Nothing you asked for. 你什么都没要求。

But you surely don't want to have the image read over and over again from HDD, each time the Panel needs repainting. 但是您肯定不想每次面板需要重新粉刷时都从HDD读取图像。 So we create an instance which holds an attribute, which is the image. 因此,我们创建了一个实例,其中包含一个属性,即图像。

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

public class TestGuiPaint extends JFrame {

    public static void main (String [] args) {
        new TestGuiPaint ();
    }   
    public TestGuiPaint () {
    super ("TestGuiPaint"); 
        add (new CraftingPanel (), BorderLayout.CENTER);
        setSize (442, 284);
        setLocationRelativeTo (null);
        setResizable (false);
        setVisible (true);
        setDefaultCloseOperation (3);
    }

    class CraftingPanel extends JPanel {
    BufferedImage image = null;
        public CraftingPanel () {
        try {
            image = ImageIO.read (new File ("./maeander3.png"));
        } catch (java.io.IOException ioe) 
        {
            System.err.println (ioe.getMessage ());
        }       
        }

        public void paintComponent (Graphics g) {
            Color darkGrey = new Color (153, 153, 153);
            g.setColor (darkGrey);
            g.fillRect (0, 0, 436, 252);
            Color lightGrey = new Color (198, 198, 198);
            g.setColor (lightGrey);
            g.fill3DRect (3, 3, 430, 246, true);
            g.setColor (darkGrey);
            g.fill3DRect (16, 16, 222, 222, true);
            g.fill3DRect (320, 78, 100, 100, true);
            g.fillRect (248, 121, 39, 12);
            Polygon triangle = new Polygon ();
            triangle.addPoint (287, 103);
            triangle.addPoint (287, 151);
            triangle.addPoint (311, 127);
            g.fillPolygon (triangle);
            g.setColor (Color.white);
            g.fill3DRect (88, 16, 3, 222, true);
            g.fill3DRect (163, 16, 3, 222, true);
            g.fill3DRect (16, 88, 222, 3, true);
            g.fill3DRect (16, 163, 222, 3, true);
        g.drawImage (image, 44, 191, null);
        }
    };      
}

I hope you understand that I've chosen a different filename for the image. 希望您了解我为图像选择了其他文件名。 :) :)

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

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