I developed a rotation application with an oval and a button, the output of which is shown below, and the code is as follows: -

Code: -
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); } }
Now I have the following questions:
- If I specified the x, y coordinates for the oval, then why does it move from its position when the window is resized? (Although I know that due to FlowLayout, it is centered, but then it violates the property, which needs to be fixed, since I specified the x, y coordinates).
- Secondly, if the conclusion is obvious (which I did not expect), then the x, y coordinates that I indicated were wrt, in which corner?
source share