簡體   English   中英

如何在JPanel繪畫方法的頂部添加圖像?

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

我在寫的繪畫方法之上插入圖像時遇到問題。 我想讓圖像在某些坐標處與繪畫方法重疊。

我的代碼:

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
        }
    };      
}

只是建議,將圖像最后繪畫在繪畫組件中。 這樣可以將圖像繪制在其他對象之上。 另外,我知道您可能只是在測試某些內容,但是您班級名稱中每個單詞的第一個字母都應大寫。

沒有答案如何重疊或重疊

1) testguipaintTestGuiPaint更多關於Java命名約定在這里在這里

2)與Swing GUI相關的代碼應包裝在invokeLater() ,更多內容請參見“ 初始線程”

3)對於在Swing JComponents中繪畫,有方法paintComponent()而不是paint()方法,有關2D圖形中的更多內容

這里只壓縮短代碼的導入,僅實現MKorbel提出的2條建議(1和3),也為了簡潔起見擴展了JFrame,從main中刪除了實例,但從未使用過。 你什么都沒要求。

但是您肯定不想每次面板需要重新粉刷時都從HDD讀取圖像。 因此,我們創建了一個實例,其中包含一個屬性,即圖像。

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);
        }
    };      
}

希望您了解我為圖像選擇了其他文件名。 :)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM