Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8774095
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T18:28:25+00:00 2026-06-13T18:28:25+00:00

I have developed a swing application with an oval and a button whose output

  • 0

I have developed a swing application with an oval and a button whose output is shown below and the code follows :-

enter image description here

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:

  1. If I have specified the x,y coordinates for oval,then why it moves from its position when window is resized? (Though I know that due to FlowLayout it is aligned at centre but it then violates the property that it must be fixed as i have specified coordinates x,y).
  2. Secondly if the output is obvious (which I was not expecting) then x,y coordinates that I specified was w.r.t which corner?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T18:28:26+00:00Added an answer on June 13, 2026 at 6:28 pm

    The coordinates you specify are inside your own “component” and not inside the “parent” container.

    Probably easier to understand if you change add the following line to the paintComponent method:

    g2.drawRect( 0,0, 199, 199 );
    

    The rectangle matches the preferredSize you return. You see that this rectangle is always drawn, and moves when resizing the window. The oval stays at the same relative position inside the rectangle.

    Note that the size you return in getPreferredSize is smaller then the actual size of what you try to paint. That explains why you only see part of the oval

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have developed a java swing client-server application. The server has many services like
I have developed a Java Swing application, which uses the SwingWorker class to perform
I have developed a Java Swing application, and it will be installed on multiple
I have a Java Swing application, developed on Mac OS X 10.5 using Java
I have a stand-alone Java windows application developed based on Swing. It connects to
I have developed a Java swing application, then I have deployed it through a
I have already developed a JAVA SWING Application . I want to install this
We have a desktop client application developed in Swing. This application interacts with backend
I have developed a basic Chat application in Java. It consists of a server
I have developed a little Swing application in which i have added a Square

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.