我开发了一个带有椭圆形和一个按钮的摇摆应用程序,其输出如下所示,代码如下:-
代码:-
import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
public class AlphaCompositeDemo extends JFrame{
AlphaCompositeDemo()
{
super("AlphaComposite Demo");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400,400);
setLayout(new FlowLayout());
setBackground(new Color(0.2f,0.7f,0.1f,0.4f));
comp c=new comp();
add(c);
add(new JButton("Click"));
setVisible(true);
}
public static void main(String args[])
{
JFrame.setDefaultLookAndFeelDecorated(true);
SwingUtilities.invokeLater(new Runnable(){public void run(){new AlphaCompositeDemo();}});
}
}
class comp extends JComponent
{
public void paintComponent(Graphics g)
{
Graphics2D g2=(Graphics2D)g.create();
g2.setComposite(AlphaComposite.SrcOver);
g2.setColor(Color.RED);
g2.fillOval(50, 50, 220, 120);
}
public Dimension getPreferredSize()
{
return new Dimension(200,200);
}
}
现在我有以下问题:
- 如果我为椭圆指定了 x,y 坐标,那么为什么在调整窗口大小时它会从其位置移动?(虽然我知道由于FlowLayout它在中心对齐,但它违反了它必须固定的属性,因为我已经指定了坐标x,y)。
- 其次,如果输出很明显(我没想到),那么我指定的 x,y 坐标是哪个角?