簡體   English   中英

我是否需要為paintComponent擴展JPanel?

[英]Do I need to extend JPanel for paintComponent?

誰能給我一個有關如何使用paintComponent的文檔的鏈接(我幾乎在所有地方都看過),我只是在顯示一個.png,我可以更改x,y坐標(通過鍵盤輸入)。 任何幫助表示贊賞。

編輯:源

這不起作用,我的paintComponent沒有正常運行。

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

class graphicsprogram extends JPanel {
    public void paintComponent(final Graphics g) {
        super.paintComponent(g);
        Image img = new Image("img.png");
        final Dimension d = getSize();
        g.drawImage(img);
    }

    public static void main(String[] args) {
        final Point   point = new Point();
        final JFrame  frame = new JFrame("Grid");
        JLabel        label = new JLabel("Drag Me!!!", JLabel.CENTER);
        JButton       close = new JButton(new ImageIcon("close.png"));

        // Button to close the window because the window is undecorated.
        close.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });

        // Listen to the mouse activity for dragging the window
        frame.addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent e) {
                point.x = e.getX();
                point.y = e.getY();
            }
        });

        // Listener for moving the window
        frame.addMouseMotionListener(new MouseMotionAdapter (){
            public void mouseDragged(MouseEvent e) {
                Point p = frame.getLocation();
                frame.setLocation(p.x + e.getX() - point.x, p.y + e.getY() - point.y);
            }
        });

        close.setPreferredSize(new Dimension(50, 50));
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setUndecorated(true);
        frame.setMinimumSize(new Dimension(800, 600));
        frame.setLocation(200, 200);
        frame.setLayout(new BorderLayout());
        frame.getContentPane().add(close, BorderLayout.NORTH);
        frame.getContentPane().add(label, BorderLayout.CENTER);
        frame.getContentPane().setBackground(Color.PINK);
        frame.setVisible(true);
    }
}

錯誤:

javac graphicsprogram.java
graphicsprogram.java:8: error: cannot find symbol
        Image img = Image("img.png");
                    ^
  symbol:   method Image(String)
  location: class graphicsprogram
graphicsprogram.java:10: error: no suitable method found for drawImage(Image)
        g.drawImage(img);
         ^
    method Graphics.drawImage(Image,int,int,int,int,int,int,int,int,Color,ImageObserver) is not applicable
      (actual and formal argument lists differ in length)
    method Graphics.drawImage(Image,int,int,int,int,int,int,int,int,ImageObserver) is not applicable
      (actual and formal argument lists differ in length)
    method Graphics.drawImage(Image,int,int,int,int,Color,ImageObserver) is not applicable
      (actual and formal argument lists differ in length)
    method Graphics.drawImage(Image,int,int,Color,ImageObserver) is not applicable
      (actual and formal argument lists differ in length)
    method Graphics.drawImage(Image,int,int,int,int,ImageObserver) is not applicable
      (actual and formal argument lists differ in length)
    method Graphics.drawImage(Image,int,int,ImageObserver) is not applicable
      (actual and formal argument lists differ in length)
2 errors

第二次編輯

好,所以我將paintComponent類更新為此

    super.paintComponent(g);
    final Dimension d = getSize();
    g.drawImage(ImageIO.read("img.png"));

現在我得到了這個錯誤。

javac graphicsprogram.java
graphicsprogram.java:9: error: cannot find symbol
        g.drawImage(ImageIO.read("img.png"));
                    ^
  symbol:   variable ImageIO
  location: class graphicsprogram
1 error
javac graphicsprogram.java
graphicsprogram.java:8: error: cannot find symbol
        Image img = Image("img.png");
                    ^

圖像是一個接口,因此根本沒有構造函數。 API的圖片部分會告訴您這一點,當您遇到類似錯誤時,它應該是您的第一個地方。 解決方案是使用ImageIO.read(....)將Image作為文件或資源讀取。

  symbol:   method Image(String)
  location: class graphicsprogram
graphicsprogram.java:10: error: no suitable method found for drawImage(Image)
        g.drawImage(img);
         ^

Graphics類沒有將單個Image對象作為其參數的方法。 組合方法絕不是一個好主意,並希望它們會起作用,並且API會再次告訴您可以在Graphics上使用哪些方法來繪制圖像。

另外,您將希望一次讀取圖像,並且可能要在類的構造函數中讀取圖像,而不是嘗試在paintComponent方法中讀取圖像,因為您不想降低此方法的速度。 這將使您的程序響應速度變慢。

暫無
暫無

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

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