简体   繁体   English

paintComponent没有被调用

[英]paintComponent isnt called

In JFrame i have some buttons and BufferedImgae which will be used to draw on it some shapes and pictures using my own methods ( drawLine, drawing raster picture pixel by pisel and so on). 在JFrame中,我有一些按钮和BufferedImgae,它们将用于使用我自己的方法在其上绘制一些形状和图片(drawLine,按像素绘制栅格图片像素等)。 Here is how i add things to JFrame 这是我如何向JFrame添加内容

public class Main extends javax.swing.JPanel {

    JPanel panel;
    JFrame fr;

    Graphics2D g2;
    ImageIcon icon;
    BufferedImage img;

    public void init()
    {
        fr = new JFrame("Lab 2");
        fr.setMinimumSize(new Dimension(1350, 650));
        fr.setMaximumSize(fr.getMinimumSize());
        fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel glowny = new JPanel(new BorderLayout());
        glowny.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
        glowny.add(getBorderCenter(), BorderLayout.CENTER); 
        fr.add(glowny);
        fr.pack();
        fr.setVisible(true);
    }

    private JScrollPane getBorderCenter()
    {
        img = new BufferedImage( fr.getWidth()-40, fr.getHeight()-40-50, BufferedImage.TYPE_INT_RGB); //20+20 odstępy w glowny, 50 - szerokość paska z guzikami
        icon = new ImageIcon(img); 
        return new JScrollPane(new JLabel(icon));
    }

    public static void main(String [] args)
    {
        Main m = new Main();
        m.init();
    }
}

Then i try to draw on BufferedImage using double buffering. 然后,我尝试使用双缓冲在BufferedImage上进行绘制。 The following example change BufferedImage color from black (current) to white. 下面的示例将BufferedImage的颜色从黑色(当前)更改为白色。

@Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        System.out.println("paintComponent");

        WritableRaster raster = img.getRaster();
        DataBuffer db = raster.getDataBuffer();

        int[] pixels = ((DataBufferInt)db).getData();        
        int adres = 0;
        for (int y = 0; y < img.getHeight(); y++) 
        {
            adres = y * img.getWidth();
            for (int x = 0; x < img.getWidth(); x++) 
            {
                pixels[adres] = 16777215;
                adres += 1;
            }
        }

        Graphics2D g2dComponent = (Graphics2D) g;
        g2dComponent.drawImage(img, null, 0, 0); // draw buffer on screen
    }

As i understand repaint() force calling paintComponent(). 据我了解repaint()强制调用paintComponent()。 The problem is that it doesnt matter how i call repaint() 问题是我调用repaint()都没关系

fr.repaint();
glowny.repaint();
repaint();

its never called. 它从未被调用过。

You never add Main to the GUI. 您永远不会将Main添加到GUI。 You need to add the JPanel that overrides the paintComponent, the this , to the GUI somewhere and you don't. 您需要将覆盖paintComponent的JPanel this到GUI的某个地方,而您不需要这样做。 For example, you would need somewhere something like: 例如,您将需要类似以下内容的地方:

someComponentShownInGui.add(this);

I would get rid of the glowny variable and instead use this . 我会摆脱glowny变量,而使用this

ie,

public void init()
{
    fr = new JFrame("Lab 2");
    fr.setMinimumSize(new Dimension(1350, 650));
    fr.setMaximumSize(fr.getMinimumSize());
    fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    // JPanel glowny = new JPanel(new BorderLayout());
    // glowny.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
    // glowny.add(getBorderCenter(), BorderLayout.CENTER); 
    // fr.add(glowny);

    setLayout(new BorderLayout();
    setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
    add(getBorderCenter(), BorderLayout.CENTER); 
    fr.add(this);

    fr.pack();
    fr.setVisible(true);
}

Note that anything components added to the drawing JPanel will cover up its image, and so you may need to make some non-opaque. 请注意,添加到图形JPanel的所有组件都将掩盖其图像,因此您可能需要使它们变得不透明。

You should add all you components inside a JPanel then override paintComponent method of the panel. 您应该将所有组件添加到JPanel中,然后重写面板的paintComponent方法。 (JFrame doesn't have paintComponet but paint method and it's not recommanded to override it) (JFrame没有paintComponet,但是paint方法,并且不建议重写它)

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

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