AWT只描绘最后加帆布 [英] AWT paints only last-added Canvas
问题描述
我有一个非常简单的AWT绘画。只是在玩风靡做出更大的东西。但不能得到它的工作...
什么情况是,只有elypse2显示 - 无论重绘()的ING与否
我也试过用Swing组件而不是AWT(JFrame的,JComponent的),但是这也没有改变什么。
时使用必要的布局管理器?但我只想绘制的图形组件,如弧,矩形,线,多线,麻生太郎...
下面是最主要的():
公共静态无效的主要(字串[] args){
帧testFrame =新帧(GRAFX试验);
testFrame.setSize(300,200);
testFrame.setAlwaysOnTop(真); 了java.awt.EventQueue.invokeLater(新的Runnable(){
@覆盖
公共无效的run(){
testFrame.setVisible(真);
}
}); Elypse elypse =新Elypse(新点(70,80),30,30,Color.BLUE,假);
testFrame.add(elypse); Elypse elypse2 =新Elypse(新点(70,50),50,30,Color.BLUE,真);
testFrame.add(elypse2);
}
和这里所使用的类:
公共类Elypse扩展画布{ 私人点开始;
私人诠释宽度;
私人诠释高度;
私人色彩℃;
私人布尔填补; 公共Elypse(点开始,诠释的宽度,诠释高度,颜色C,布尔填写){
this.start =启动;
this.width =宽度;
this.height =高度;
this.c = C;
this.filled =填补;
} @覆盖
公共无效漆(图形G){
g.setColor(C);
如果(填写){
g.fillOval(start.x,start.y,宽度,高度);
}
其他{
g.drawOval(start.x,start.y,宽度,高度);
}
}
}
您忽略了包()
封闭窗口
。注意:在你原来的code中的典型症状:调整框架,它生成一个更新,使 elypse2
出现
附录:你可以看到的两个的 Elypse
使用布局,如网格布局
。
testFrame.setLayout(新的GridLayout(0,1));
由于测试:
进口java.awt.Canvas中;
进口java.awt.Color中;
进口java.awt.Dimension中;
进口java.awt.Frame中;
进口java.awt.Graphics;
进口java.awt.Point中;公共类的测试{ 公共静态无效的主要(字串[] args){
帧testFrame =新帧(GRAFX试验);
testFrame.setAlwaysOnTop(真);
Elypse elypse =新Elypse(新点(70,80),30,30,Color.BLUE,假);
testFrame.add(elypse);
Elypse elypse2 =新Elypse(新点(70,50),50,30,Color.BLUE,真);
testFrame.add(elypse2);
testFrame.pack();
testFrame.setVisible(真);
} 私有静态类Elypse扩展画布{ 私人点开始;
私人诠释宽度;
私人诠释高度;
私人色彩℃;
私人布尔填补; 公共Elypse(点开始,诠释的宽度,诠释高度,颜色C,布尔填写){
this.start =启动;
this.width =宽度;
this.height =高度;
this.c = C;
this.filled =填补;
} @覆盖
公共无效漆(图形G){
g.setColor(C);
如果(填写){
g.fillOval(start.x,start.y,宽度,高度);
}其他{
g.drawOval(start.x,start.y,宽度,高度);
}
} @覆盖
公共尺寸的get preferredSize(){
返回新尺寸(320,240);
}
}
}
I've a VERY simple AWT Painting. Just playing aound to make something bigger. But can't get it working ...
What happens is that only elypse2 is shown - regardless of repaint()ing it or not.
I also tried to use Swing components instead of AWT (JFrame, JComponent) but this also changes nothing.
Is using a Layout Manager necessary? But I want to draw only graphical components, like arcs, rectangles, line, poly-lines, aso ...
Here's the main():
public static void main(String[] args) {
Frame testFrame = new Frame("Grafx-Test");
testFrame.setSize(300, 200);
testFrame.setAlwaysOnTop(true);
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
testFrame.setVisible(true);
}
});
Elypse elypse = new Elypse(new Point(70, 80), 30, 30, Color.BLUE, false);
testFrame.add(elypse);
Elypse elypse2 = new Elypse(new Point(70, 50), 50, 30, Color.BLUE, true);
testFrame.add(elypse2);
}
and here the used class:
public class Elypse extends Canvas {
private Point start;
private int width;
private int height;
private Color c;
private boolean filled;
public Elypse(Point start, int width, int height, Color c, boolean filled) {
this.start = start;
this.width = width;
this.height = height;
this.c = c;
this.filled = filled;
}
@Override
public void paint(Graphics g) {
g.setColor(c);
if (filled) {
g.fillOval(start.x, start.y, width, height);
}
else {
g.drawOval(start.x, start.y, width, height);
}
}
}
You neglect to pack()
the enclosing Window
. Note the characteristic symptom in your original code: resizing the frame, which generates an update, causes elypse2
to appear.
Addendum: You can see both Elypse
instances by using a layout such as GridLayout
.
testFrame.setLayout(new GridLayout(0, 1));
As tested:
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Point;
public class Test {
public static void main(String[] args) {
Frame testFrame = new Frame("Grafx-Test");
testFrame.setAlwaysOnTop(true);
Elypse elypse = new Elypse(new Point(70, 80), 30, 30, Color.BLUE, false);
testFrame.add(elypse);
Elypse elypse2 = new Elypse(new Point(70, 50), 50, 30, Color.BLUE, true);
testFrame.add(elypse2);
testFrame.pack();
testFrame.setVisible(true);
}
private static class Elypse extends Canvas {
private Point start;
private int width;
private int height;
private Color c;
private boolean filled;
public Elypse(Point start, int width, int height, Color c, boolean filled) {
this.start = start;
this.width = width;
this.height = height;
this.c = c;
this.filled = filled;
}
@Override
public void paint(Graphics g) {
g.setColor(c);
if (filled) {
g.fillOval(start.x, start.y, width, height);
} else {
g.drawOval(start.x, start.y, width, height);
}
}
@Override
public Dimension getPreferredSize() {
return new Dimension(320, 240);
}
}
}
这篇关于AWT只描绘最后加帆布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!