当然可以,但不能直截了当。
无论您要在 JButton 上绘制什么,都将其转储到 BufferedImage 中。您的 JButton 的 paint() 本质上应该返回一个缓冲图像。BufferedImages 在重绘期间非常快,它们只是一个像素矩阵,重绘之间几乎不需要时间。
package test;
import java.awt.Graphics;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class ButtonTest {
public static void main(String[] args){
new ButtonTest().test();
}
public void test(){
JFrame frame = new JFrame("TestFrame");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel pnl = new JPanel();
pnl.add(new MyButton());
frame.add(pnl);
frame.setSize(600, 600);
frame.setVisible(true);
}
class MyButton extends JButton{
public void paint(Graphics g){
//return a buffered image everytime
}
}
}
尽管上述技术看起来有点愚蠢,但您基本上可以在内存中拥有一个缓冲图像,并且应该从 paint() 方法返回。您从服务器 (RMI) 接收到的任何内容都可以使用在后台运行的单独线程转储到 BufferedImage 对象中。