简体   繁体   English

为什么getGraphics在ActionPerformed方法中起作用?

[英]Why getGraphics does work inside an ActionPerformed method?

Why it is possible to draw with getGraphics() when called inside method ActionPerformed of Interface ActionListener but not within a method called from constructor or other method. 为什么在接口ActionListener的ActionPerformed方法内部调用而不在从构造函数或其他方法调用的方法内部调用getGraphics()时可能进行绘制。 Here is the code i made. 这是我编写的代码。 Why the calls of the method "dibujar" is ignored inside "empezar" and constructor but not inside the ActionListener method? 为什么在“ empezar”和构造函数中会忽略方法“ dibujar”的调用,而在ActionListener方法中却不会呢?

import javax.swing.*;
import java.awt.Dimension;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class VentanaGrafica extends JFrame{


public static void main(String[] args){
SwingUtilities.invokeLater(new Runnable(){
@Override
    public void run(){
        new VentanaGrafica().setVisible(true);
    }
});
}


public VentanaGrafica(){    
    empezar();  
    dibujar();
}



private void empezar(){
setTitle("Graficar con Jpanel");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setMinimumSize(new Dimension(600,600));
setResizable(false);
panel = new JPanel();
panel.setBackground(Color.blue);
panel.setSize(new Dimension(400,400));
boton = new JButton("Este boton");
boton.setFocusable(false);
panel.add(boton);
add(panel);
boton.addActionListener(new ActionListener(){
    @Override
    public void actionPerformed(ActionEvent e){
        dibujar();
    }
});
pack(); 
dibujar();

}

private void dibujar(){
gc = panel.getGraphics();
gc.setColor(Color.red);
gc.fillRect(200,0,120,80);
}
JPanel panel;
Graphics gc;
JButton boton;
}

I have read that the repaint() method is called every sometime to repaint and it means that dibujar() is not ignored but repaint() have erased its work but why it doesnt happen when the call is made inside the ActionEvent? 我已经读过repaint()方法每隔某个时间就会被调用以进行重画,这意味着dibujar()不会被忽略,但是repaint()删除了其工作,但是为什么在ActionEvent中进行调用时却没有发生呢? it's somekind implicit to do some kind of loop or inhibit the repaint() call? 是某种隐式执行某种循环或禁止repaint()调用吗?

I think that this is caused because in the first case new VentanaGrafica().setVisible(true); 我认为这是因为在第一种情况下, new VentanaGrafica().setVisible(true); is called after dibujar() which means that once you make the frame visible the panel will be repainted and the effects of the dibujar() will be erased and in the second case, the panel isn't repainted after you call dibujar() . dibujar()之后调用,这意味着一旦使帧可见, panel将被重新粉刷,并且dibujar()的效果将被擦除;在第二种情况下,调用dibujar()之后panel不会被重新粉刷。

If you want to do some custom painting, you will have to override the paintComponent method and add some logic to make the component's state consistent. 如果要进行一些自定义绘制,则必须重写paintComponent方法并添加一些逻辑以使组件的状态保持一致。

Here is an example: 这是一个例子:

public class CustomPanel extends JPanel{
    Color color = Color.WHITE;
    protected void paintComponent(Graphics g){
        super.paintComponent(g);
        g.setColor(color);
        g.fillRect(200,0,120,80);
    }
    public void changeColor(Color color){
        this.color = color;
    }
}

And here is how you can use this panel: 这是使用此面板的方法:

CustomPanel panel = new CustomPanel();
panel.changeColor(Color.RED);
panel.repaint();

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

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