Ruu*_*kis 12 java applet swing awt
我正在尝试创建一个Applet加载器,我需要在显示的Applet上绘制,但我似乎无法找到一种方法来做到这一点.
我最初的理解是Applet,通过扩展Component就像任何常规的java.awt.Component一样,可以在Container中添加,只有覆盖paint方法,但它似乎不起作用.
在我的初始化代码中,我创建了一个java.awt.Frame,我在其上添加了我的java.awt.Container的自定义实现,它覆盖了所有的paint方法,以便它们在x:5,y:5处填充rect,大小为w:10 ,h:10调用父方法后
但是,当添加小程序时,无论在所有内容之上绘制什么,它总是如此
public class AppletTest {
public static void main(String[] args) {
Frame frame = new Frame("Applet Test!");
Container container = new Container() {
@Override
public void paint(Graphics g) {
super.paint(g);
g.fillRect(5, 5, 10, 10);
}
@Override
public void paintAll(Graphics g) {
super.paintAll(g);
g.fillRect(5, 5, 10, 10);
}
@Override
public void paintComponents(Graphics g) {
super.paintComponents(g);
g.fillRect(5, 5, 10, 10);
}
@Override
public void print(Graphics g) {
super.print(g);
g.fillRect(5, 5, 10, 10);
}
@Override
public void printComponents(Graphics g) {
super.printComponents(g);
g.fillRect(5, 5, 10, 10);
}
@Override
public void update(Graphics g) {
super.update(g);
g.fillRect(5, 5, 10, 10);
}
};
Dimension dimension = new Dimension(50, 50);
container.setPreferredSize(dimension);
Applet applet = new Applet() {
@Override
public void paint(Graphics g) {
super.paint(g);
g.fillRect(0, 0, 10, 10);
}
};
container.add(applet);
applet.setBounds(0, 0, 50, 50);
frame.add(container);
frame.pack();
frame.setVisible(true);
applet.init();
applet.start();
}
}
Run Code Online (Sandbox Code Playgroud)
能够在Applet其父级之上绘制的步骤需要采取哪些步骤Container?
这也是运行上述代码的结果的屏幕截图
如果我改不过的类型applet,以Component这样
Component applet = new Component() {
@Override
public void paint(Graphics g) {
super.paint(g);
g.fillRect(0, 0, 10, 10);
}
};
Run Code Online (Sandbox Code Playgroud)
结果是对的
所需解决方案的局限性在于我无法修改Applet本身,因为它是一个仅以二进制形式提供的遗留组件.我知道有一个通过字节码修改的解决方案,但由于Applets的种类繁多,这是不可能的.
这是因为applet重叠容器的drawaple区域.如果设置applet的背景颜色并更改大小,您可以看到这一点:
applet.setBackground(Color.RED);
applet.setBounds(0, 0, 12, 12);
Run Code Online (Sandbox Code Playgroud)
结果我们可以在applet上绘制的黑色方块下看到红色边框(小程序的背景):
使用applet大小和applet的红色背景完全重叠容器可绘制区域:
如果你换Applet到Component你可以看到集装箱上的黑色方块因为Component没有背景.即改变applet变量的类型:
Component applet = new Component() {
//...
};
applet.setBackground(Color.RED);
Run Code Online (Sandbox Code Playgroud)
您可以在实验中看到图片:
为了能够在其父容器上绘制Applet,需要采取哪些步骤?
除了直接在applet上绘图外,在applet上绘制是不可能的.
使用GlassPane无法解决applet的这个问题.我试过文档中的例子
并替换代码:
contentPane.add(new JButton("Button 1"));
contentPane.add(new JButton("Button 2"));
Run Code Online (Sandbox Code Playgroud)
至:
Applet applet = new Applet() {
@Override
public void paint(Graphics g) {
super.paint(g);
g.fillRect(0, 0, 10, 10);
}
};
applet.setPreferredSize(new Dimension(100, 25));
applet.setBackground(Color.GREEN);
contentPane.add(applet);
Run Code Online (Sandbox Code Playgroud)
结果我们可以看到applet,owerlap darawed circle:
如果我们将applet变量的类型更改为,则完全绘制圆JLanel.