簡體   English   中英

super.paintComponent(g)做了什么?

[英]What did super.paintComponent(g) do?

因此,在過去的幾天中,我嘗試實現一種更簡單的圖形繪圖儀版本。 我遇到的一個大問題是重新粉刷時發生的錯誤。 基本上,我已經在一個類中設計了程序,該類負責在另一個類中單擊JButton之后繪制整個坐標系和給定的函數。 另一個類包含被按下的JButton。 按下JButton后,它將在坐標系類中調用一個函數以重畫圖片。 這兩個類都在擴展JPanel。

錯誤是,當我在按下按鈕時進行重新繪制時,按鈕是在坐標系上繪制的,而不是在其原始位置繪制的,因此換句話說,在另一個JPanel上,即使我沒有更改放置位置和東西。 這兩個類都添加到使用GridLayout的JFrame中。

誰能告訴我為什么super.paintComponent(g); 解決了這個錯誤?

編輯:添加代碼

窗類

public class main {

public static void main(String[] args) throws SemanticFailureException {

    int x = 800;
    int y = 600;


    JFrame frame = new JFrame();
    frame.setLayout(new GridLayout());
    frame.setTitle("Function plotter");
    frame.setSize(2*x, 2*y);

    Surface test = new Surface(x, y, 10);
    CommandDraw test1 = new CommandDraw(x/2,y,test);


    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLocationRelativeTo(null);
    frame.add(test);
    frame.add(test1);


    frame.setVisible(true);

}
}

坐標系類:(為簡單起見,將坐標系的繪制更改為矩形,僅繪制矩形仍會發生錯誤)

public class Surface extends JPanel {

/**
 * 
 */
private static final long serialVersionUID = 1L;
boolean drawFunct;

public Surface(int x1, int y1, int coordLength) {
    setSize(x1,y1);
    drawFunct = false;

}

public void paintComponent(Graphics g) {
    super.paintComponent(g); // without this the jbutton occures on the left

    // create Graphics object to get more functions
    Graphics2D g2 = (Graphics2D) g;
    // draw Plotter
    drawFunction(g2);

    if (drawFunct)
        g2.drawLine(0, 0, 80, 80);
}

public void drawFunction(Graphics2D g) {
    g.drawRect(40, 40, 30, 30);
}

public void redraw() {
    drawFunct = true;
    repaint();
}
}

具有JButton的類:

public class CommandDraw extends JPanel {
/**
 * 
 */
private static final long serialVersionUID = 1L;
JButton makeDraw;
JTextField inputPoly;
Surface surf;

public CommandDraw(int x, int y, Surface surf) {
    this.surf = surf;
    setSize(x,y);
    setLayout(new FlowLayout());


    makeDraw = new JButton("draw Function");
    makeDraw.setBackground(Color.LIGHT_GRAY);
    makeDraw.setFocusable(false);

    makeDraw.addActionListener( new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            surf.redraw();

        }

    });

    add(makeDraw);


    inputPoly = new JTextField("Input polynomial");
    inputPoly.setHorizontalAlignment(JTextField.CENTER);
    add(inputPoly);


}
}

Can anyone tell me why super.paintComponent(g); solved that bug?

因為對超類(JPanel)的paintComponent(g)的調用將確保在執行繪制操作之前將按預期方式渲染面板。 您需要確保您的JPanel在“圖形”透視圖中的行為與其他任何JPanel一樣。

用於定制的JPanel繪制的模板應該類似於:

import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JPanel;

public class MyDrawPanel extends JPanel {

    @Override
    protected void paintComponent( Graphics g ) {

        super.paintComponent( g );

        // create a new graphics context based on the original one
        Graphics2D g2d = (Graphics2D) g.create();

        // draw whatever you want...


        g2d.dispose();

    }

}

編輯

您需要調用super.paintComponent(g)來告知JPanel paintComponent(Graphics)版本應在開始查找自定義內容之前執行。 這就像要求JPanel准備要使用的繪畫功能一樣。 這些疑問的一個很好的起點是文檔: https : //docs.oracle.com/javase/10/docs/api/javax/swing/JComponent.html#paintComponent(java.awt.Graphics)

在原始Graphics的副本中調用dispose()方法。 如果您處理傳遞給該方法的Graphics ,則可能還會遇到一些問題。 您可以使用原始Graphics執行繪畫操作,但是不應該這樣做,因此,更好的做法是制作原始Graphics的副本並在使用后進行處置。

同樣在這里看看: paintComponent如何工作?

暫無
暫無

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

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