import java.awt.Graphics;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class TowerDefence extends JPanel {
public void paintComponent(Graphics g) {
super.paintComponent(g);
ImageIcon EasyImage = new ImageIcon("WelcomeImage.png");
Image Image = EasyImage.getImage();
Image newimg = Image.getScaledInstance(1000, 700, java.awt.Image.SCALE_SMOOTH);
EasyImage = new ImageIcon(newimg);
EasyImage.paintIcon(this, g, 0, 0);
JButton button = new JButton("Click Button");
button.setBounds(100, 100, 100, 100);
super.add(button);
}
public static void main(String[] args) throws Exception {
TowerDefence T = new TowerDefence();
JFrame frame = new JFrame("Sam");
frame.setVisible(true);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setBounds(150, 20, 1000, 700);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(T);
}
}
Why is this printing out 2 JButtons? I literally don't know how this works and it would be nice to know. The idea is to literally print out a picture and a button and I can't even get that to work lol. The main issue is I don't know how to use the paint component as I am quite new to java.
First don't use setBounds unless you have a null type layout.
Second don't use a null layout unless you want to handle everything about the layout.
ImageIcon easyImage;
public TowerDefense(){
EasyImage = new ImageIcon("WelcomeImage.png");
Image Image = EasyImage.getImage();
Image newimg = Image.getScaledInstance(1000, 700, java.awt.Image.SCALE_SMOOTH);
JButton button = new JButton("Click Button");
add(button);
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
EasyImage.paintIcon(this, g, 0, 0);
}
I've re-arranged the code to try to separate the painting and the initialization. I strongly recommend checking out the link provided by Abra because you'll save quite a few headaches.
Related
This question already exists:
Java Graphics not showing on Mac even with overriding the paintComponent() method of the JPanel
Closed 2 years ago.
I'm new to Java and am trying to create a program in Swing following an Youtube Tutorial. Everything works fine in Windows but in Mac, the background image doesn't show up but only the menuBar(JLabel). I hope to get background image and JLabels in a same page.
Can someone please help me to continue in the right direction? I will put all the file codes so that understading the issue is better.
Thanks a lot in advance!
[Main.java]
package dynamic_beat_4;
public class Main {
public static final int SCREEN_WIDTH = 1280;
public static final int SCREEN_HEIGHT = 720;
public static void main(String[] args) {
new DynamicBeat();
}
}
[Dynamic Beat.java]
package dynamic_beat_4;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class DynamicBeat extends JFrame {
private Image screenImage;
private Graphics screenGraphic;
private Image introBackground = new ImageIcon(Main.class.getResource("../images/introBackground(Title).jpg"))
.getImage();
private JLabel menuBar = new JLabel(new ImageIcon(Main.class.getResource("../images/menuBar.png")));
public DynamicBeat() {
setUndecorated(true);
setTitle("Dynamic Beat Game");
setSize(Main.SCREEN_WIDTH, Main.SCREEN_HEIGHT);
setResizable(false);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
setBackground(new Color(0, 0, 0, 0));
setLayout(null);
menuBar.setBounds(0, 0, 1280, 30);
add(menuBar);
Music introMusic = new Music("introMusic.mp3", true);
introMusic.start();
}
public void paint(Graphics g) {
screenImage = createImage(Main.SCREEN_WIDTH, Main.SCREEN_HEIGHT);
screenGraphic = screenImage.getGraphics();
screenDraw(screenGraphic);
g.drawImage(screenImage, 0, 0, null);
}
public void screenDraw(Graphics g) {
g.drawImage(introBackground, 0, 0, null);
paintComponents(g);
this.repaint();
}
}
Some major points.
don't call repaint inside of a paint method. This doesn't make sense.
the preferred painting technique of swing is to override paintComponent.
Don't override Window#paint and if you do, call super.paint(g). (As per the linked javadoc.)
Calling paintComponents(g) with the graphics object of a temporary image is unnecessary at best. Swing handles buffering.
I'm amazed this worked on any platform.
I think the easiest fix for this is to create a JPanel.
public DynamicBeat() {
setUndecorated(true);
setTitle("Dynamic Beat Game");
setSize(Main.SCREEN_WIDTH, Main.SCREEN_HEIGHT);
setResizable(false);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
setBackground(new Color(0, 0, 0, 0));
JPanel panel = new JPanel(){
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(introBackground, 0, 0, this);
}
};
panel.setLayout(null);
menuBar.setBounds(0, 0, 1280, 30);
panel.add(menuBar);
setContentPane(panel);
Music introMusic = new Music("introMusic.mp3", true);
introMusic.start();
}
You might want to refer to this painting in awt and swing.
Some further tips, don't extend JFrame, and do your GUI work on the EDT.
I have this code:
package game;
import java.awt.Graphics;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Draw {
Object block;
public Draw(JFrame frame, Object object) {
this.block = object;
JPanel pane = new JPanel() {
private static final long serialVersionUID = 3869097656854760151L;
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
try {
g.drawImage(ImageIO.read(new File(object.getTexture())), object.getX(), object.getY(), object.getWidth(),
object.getHeight(), null);
} catch (IOException e) {
System.err.println("Image '" + object.getTexture() + "' could not be found!");
}
}
};
frame.add(pane);
}
}
and I call the class here:
package game;
import javax.swing.JFrame;
public class Frame {
private final int X_SIZE = new vena.util.Computer().screenWidth();
private final int Y_SIZE = new vena.util.Computer().screenHeight();
public Frame() {
JFrame f = new vena.util.Frame().frame("2D Game", X_SIZE - X_SIZE / 5, Y_SIZE - Y_SIZE / 5, true, false, false,
"res/icon.png");
new Draw(f, new Object(0, 0, 100, 100, "grass"));
new Draw(f, new Object(100, 0, 100, 100, "grass"));
f.setVisible(true);
}
public static void main(String[] args) {
new Frame();
}
}
It renders when I call the image with
new Draw(f, new Object(0, 0, 100, 100, "grass"));
But when I call the image another time, next to it
new Draw(f, new Object(100, 0, 100, 100, "grass"));
it only renders the second image, and removes the first one. I have noticed that this doesn't occur when I call g.drawImage() twice in the paintComponent method. Is there a way so that I can call the Draw class as many times as I want, without clearing the JPanel?
The default layout manager of the content pane of a frame is the BorderLayout. When you add components to a BorderLayout and you don't specify a constraint the component goes to the CENTER. Only the last component added can be displayed in the CENTER.
If you want multiple components on the frame then you can change the layout manager. Try
f.setLayout( new FlowLayout() );
to see the difference.
I have noticed that this doesn't occur when I call g.drawImage() twice in the paintComponent method.
Yes, if you are trying to paint images at specific locations on the frame then you should really be overriding paintComponent() to paint each image.
For a game I'm making, I want the player to begin at a menu screen(Panel05), and then click a button to start the actual game(Panel00). And, when playing the game, if they win or lose, when they click another button they either go back to the menu or go onto another level. Right now, the panels are all separate programs, with their own drivers, and I'm unsure on how to get one panel to work inside another, if that is at all possible. I would appreciate any and all advice, answers, or criticism given. Below is the panel for the menu
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
public class Panel05 extends JPanel
{
private BufferedImage myImage;
private Graphics myBuffer;
public JButton button1;
public Panel05()
{
myImage = new BufferedImage(1000, 1000, BufferedImage.TYPE_INT_RGB);
myBuffer = myImage.getGraphics();
setLayout(null);
button1 = new JButton();
button1.setSize(100, 100);
button1.setLocation(500,500);
button1.setForeground(Color.WHITE);
button1.setFont(new Font("Serif", Font.BOLD, 30));
button1.setText("Start");
button1.addActionListener(new B1Listener());
button1.setBorder(null);
button1.setOpaque(false);
button1.setContentAreaFilled(false);
button1.setBorderPainted(false);
add(button1);
setFocusable(true);
}
public void paintComponent(Graphics g)
{
ImageIcon Nintendo = new ImageIcon("trumpL.png");
g.drawImage(Nintendo.getImage(), 0, 0, 1000, 1000, null);
}
private class B1Listener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
}
}
}
And here is the panel for the actual first level of the game.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import javax.swing.Timer;
public class Panel00 extends JPanel
{
private BufferedImage myImage;
private Graphics myBuffer;
public Timer timer;
public JButton button1;
public JButton button2;
public JLabel label1 = new JLabel("Good Choice!");
public JLabel label2 = new JLabel("You're Fired!!");
public int x = 5; //CountDown from 5
public int delay = 1000; //milliseconds
boolean drawWin = false;
boolean drawLose = false;
public Panel00()
{
myImage = new BufferedImage(1000, 1000, BufferedImage.TYPE_INT_RGB);
myBuffer = myImage.getGraphics();
setLayout(null);
button1 = new JButton();
button1.setSize(300, 200);
button1.setLocation(100,150);
button1.setFont(new Font("Serif", Font.BOLD, 18));
button1.setText("<html><center>"+"Until we are able to determine and understand this problem"+"<br>"+" and the dangerous threat it poses, our country cannot be the victims of horrendous attacks"+"<br>"+"by people that believe only in Jihad, and have no sense of reason or respect for human life"+"</center></html>");
button1.addActionListener(new B1Listener());
button1.setBorder(null);
button1.setOpaque(false);
button1.setContentAreaFilled(false);
button1.setBorderPainted(false);
add(button1);
button2 = new JButton();
button2.setSize(300, 200);
button2.setLocation(600,150);
button2.setFont(new Font("Serif", Font.BOLD, 18));
button2.setText("<html><center>"+"If ISIS wants to fight, fine with us. "+"<br>"+"We have wanted that fight for a long time. There is no room in the world for ISIS any more."+"<br>"+"The Muslims or us, one of us will have to go."+"</center></html>");
button2.addActionListener(new B2Listener());
button2.setBorder(null);
button2.setOpaque(false);
button2.setContentAreaFilled(false);
button2.setBorderPainted(false);
add(button2);
ActionListener counter =
new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
repaint();
x--;
if (x == 0)
{
timer.stop();
}
}
};
timer = new Timer(delay, counter);
timer.start();
setFocusable(true);
}
public void paintComponent(Graphics g)
{
ImageIcon Nintendo = new ImageIcon("trump speech.jpg");
g.drawImage(Nintendo.getImage(), 0, 0, 1000, 1000, null);
ImageIcon N = new ImageIcon("happy.JPG");
g.setColor(Color.WHITE);
g.fillOval(90,100,320,320);
g.setColor(Color.WHITE);
g.fillOval(590,100,320,320);
g.setColor(Color.WHITE);
g.setFont(new Font("Serif",Font.BOLD, 50));
g.drawString(""+x,500,50);
if (drawWin)
{
g.drawImage(N.getImage(), 0, 0, 1000, 1000, null);
}
ImageIcon L = new ImageIcon("loser.JPG");
if (drawLose)
{
g.drawImage(L.getImage(), 0, 0, 1000, 1000, null);
}
}
private class B1Listener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
repaint();
drawWin = true;
label1.setLocation(250,650);
label1.setSize(1000, 400);
label1.setForeground(new Color(212, 175, 55));
label1.setFont(new Font("Serif", Font.BOLD, 100));
add(label1);
button1.setEnabled(false);
button2.setEnabled(false);
button1.setText("");
button2.setText("");
timer.stop();
}
}
private class B2Listener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
repaint();
drawLose = true;
label2.setLocation(500,700);
label2.setSize(400, 400);
label2.setForeground(Color.RED);
label2.setFont(new Font("Serif", Font.BOLD, 40));
add(label2);
button1.setEnabled(false);
button2.setEnabled(false);
button1.setText("");
button2.setText("");
timer.stop();
}
}
}
Suggestions:
First and foremost, swap JPanels using a CardLayout. You'll need another JPanel, one that uses the CardLayout, you'll add your two JPanels above to this first JPanel with an appropriate String constant, and then you can swap JPanels at will. The CardLayout Tutorial will show you how. I have posted some code that can be found in some of these links. Here's a nice one.
Other serious issues in your code -- first and foremost, never read in images or any other files within a paintComponent method. This method is what mostly determines the perceived responsiveness of your GUI, and you never want to slow it down. But also, why do this? Why not read in the image once, store it in a variable, and be done with it?
Also call the super.paintComponent(...) method within your override, else you may not rid your GUI of dirty pixels.
Also Avoid using null layouts like the plague. While null layouts and setBounds() might seem to Swing newbies like the easiest and best way to create complex GUI's, the more Swing GUI'S you create the more serious difficulties you will run into when using them. They won't resize your components when the GUI resizes, they are a royal witch to enhance or maintain, they fail completely when placed in scrollpanes, they look gawd-awful when viewed on all platforms or screen resolutions that are different from the original one.
I have a JPanel with a simple animated snow particle effect inside of a JFrame, and it works from on its own. But when I try and add another panel to it, it makes the effect of the snow stop. Can anyone help me with this? Is it a problem with layering, or something? Or is it an issue with my Layout Managers?
My Code:
package christmasfinal;
import java.awt.*;
import javax.swing.*;
public class ChristmasFinal extends JApplet {
public static void main(String[] args) {
JFrame frame = new JFrame();
TreePanel treePanel = new TreePanel();
frame.setSize(550, 420);
SnowBackgroundPanel snowPanel = new SnowBackgroundPanel(frame.getWidth(), frame.getHeight());
treePanel.setSize(200, 320);
snowPanel.setSize(frame.getWidth(), frame.getHeight());
snowPanel.setLayout(new BorderLayout());
frame.setLayout(new BorderLayout());
frame.add(snowPanel, BorderLayout.CENTER);
snowPanel.add(treePanel, BorderLayout.CENTER);
System.out.println(treePanel.getWidth() + " " + treePanel.getHeight());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setTitle("Christmas Final");
}
}
package christmasfinal;
import javax.swing.*;
import java.awt.*;
public class TreePanel extends JPanel{
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
int[] treeX = {getWidth()/2, getWidth()/2-20, getWidth()/2-60, getWidth()/2-20,
getWidth()/2-40, getWidth()/2-80, getWidth()/2-20, getWidth()/2-60, getWidth()/2-100,
getWidth()/2+100,getWidth()/2+60, getWidth()/2+20, getWidth()/2+80,
getWidth()/2+40, getWidth()/2+20, getWidth()/2+60, getWidth()/2+20, getWidth()/2};
int[] treeY = {getHeight()/2-120, getHeight()/2-80, getHeight()/2-40, getHeight()/2-40,
getHeight()/2-20, getHeight()/2, getHeight()/2, getHeight()/2+40, getHeight()/2+60,
getHeight()/2+60, getHeight()/2+40, getHeight()/2, getHeight()/2, getHeight()/2-20,
getHeight()/2-40, getHeight()/2-40, getHeight()/2-80, getHeight()/2-120};
g.setColor(Color.GREEN);
g.fillPolygon(treeX, treeY, treeX.length);
g.setColor(new Color(102, 51, 0));
g.fillRect(getWidth()/2-20, getHeight()/2+60, 40, 40);
}
}
package christmasfinal;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Random;
import java.math.*;
public class SnowBackgroundPanel extends JPanel{
Random rand = new Random();
//Create ArrayList for SnowParticles
ArrayList<SnowParticle> snow = new ArrayList();
//Create animation timer
Timer timer = new Timer(7, new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
repaint();
}
});
public SnowBackgroundPanel(int width, int height){
setSize(width, height);
System.out.println(getHeight() + " " + getWidth());
for(int i=0; i<50; i++){
snow.add(new SnowParticle());
snow.get(i).x = rand.nextInt(getWidth()+1);
snow.get(i).y = 0-rand.nextInt(getHeight()+1);
}
timer.start();
}
#Override
public void paintComponent(Graphics g){
super.paintComponent(g);
g.setColor(Color.WHITE);
//Set, paint, and move snow particles
for(int i=0; i<50; i++){
g.fillOval(snow.get(i).x, snow.get(i).y, 10, 10);
snow.get(i).y += 1;
if(snow.get(i).y > getHeight()){
snow.get(i).x = rand.nextInt(getWidth()+1);
snow.get(i).y = 0-rand.nextInt(getHeight()+1);
}
}
}
}
You're adding your TreePanel to the BorderLayout.CENTER position of the SnowPanel which will effectively cover up the SnowPanel, so it should be expected to cover SnowPanel's graphics and animation. Likely setting TreePanel to be non-opaque by calling setOpaque(false) on it will allow you to see through it:
TreePanel treePanel = new TreePanel();
treePanel.setOpaque(false);
Other issues with your code:
Why have ChristmasFinal extend JApplet when it has no applet code nor behaviors?
Avoid calling setSize(...) on anything as that is a dangerous thing to do.
Better to let the components size themselves based on their layout managers and preferredSizes.
If you absolutely need to set a size, better to override getPreferredSize()
You should avoid having program logic within a paintComponent(...) method since you never have complete control over when or even if paintComponent(...) gets called.
I suggest an entirely different approach:
SnowBackgroundPanel & TreePanel which extend JPanel should instead implement Drawable
The Drawable interface will have a single method draw(Graphics2D g, Dimension sizeOfParent)
In the draw() method, put what was in paintComponent()
In the single area designated for custom painting, (e.g. RenderingSurface extends JPanel) keep a collection of the drawn elements in a list that respects their order. e.g. if the first element is the BG, it is rendered first.
In the paintComponent() of RenderingSurface, iterate the collection of drawable elements and draw each one.
I am trying to create a translucent window which has no border or background other than the JLabel image's I put in it, using OverlayLayout and an extended JPanel...
My problem is when I try to add more components over the one I initially added which would be the background, I have no idea how to enable changing of the new components position.. x,y etc...
Please if possible show me what I can do and don't just point me to layoutmanagers, I need an example please if anyone is willing to show me.
Or better yet, show me what I need to do to my code in order to get the desired effect.. like changing "text" (A JLabel) position to be 10,10 ... x and y.
package core;
import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.LayoutManager;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JWindow;
import javax.swing.OverlayLayout;
public class App {
// Window & Panel...
public JWindow frame;
public TranslucentPanel panel;
// OverlayLayout
public LayoutManager overlay;
// Components
public JLabel bg;
public JLabel test;
// Constructor
public App() {
try {
// Basics...
frame = new JWindow();
frame.setBackground(new Color(0, 0, 0, 0));
// Overlay
panel = new TranslucentPanel();
overlay = new OverlayLayout(panel);
panel.setLayout(overlay);
frame.setContentPane(panel);
// initComponents
initComponents();
// Finalize Frame
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
catch(Exception e) {
e.printStackTrace();
}
}
// Initialize Additional Components
public void initComponents() throws Exception {
test = new JLabel("test");
test.setForeground(Color.WHITE);
frame.add(test);
bg = new JLabel(new ImageIcon(ImageIO.read(getClass().getResource("/ball.png"))));
frame.add(bg);
// What must I do to be able to do this???
test.setLocation(10, 0);
}
// TranslucentPanel Class...
public class TranslucentPanel extends JPanel {
private static final long serialVersionUID = 1L;
public TranslucentPanel() {
setOpaque(false);
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D)g.create();
g2d.setComposite(AlphaComposite.SrcOver.derive(0.0f));
g2d.setColor(getBackground());
g2d.fillRect(0, 0, getWidth(), getHeight());
}
}
}
One way would be to discard the Overlayout manager, set the TranslucentPanel's layout manager to something like BorderLayout and use the JLabel, bg as a container in of itself...
bg = new JLabel(new ImageIcon(ImageIO.read(getClass().getResource("/ball.png"))));
frame.add(bg);
// Set the layout of the JLabel
bg.setLayout(new GridBagLayout());
test = new JLabel("test");
test.setForeground(Color.WHITE);
// Add the test label to the bg JLabel...
bg.add(test);
Personally, I don't like this, as JLabel doesn't take into consideration the components (or the layout manager) when it makes it's calculations for it's preferred size.
Personally, I would create a custom background component that was responsible for painting the background image. Then, onto this, I would place the other components, using what ever combination of components and layout managers I need to produce the desired results.
Pixel perfect layouts are an illusion within modern UI design. There are too many factors which affect the individual size of components, none of which you can control. Swing was designed to work with layout managers at the core, discarding these will lead to no end of issues and problems that you will spend more and more time trying to rectify
After reading the following pieces from your codes :
// What must I do to be able to do this???
test.setLocation(10, 0);
If I understand correctly , you want to arrange position of your component based on custom coordinates. If so then You can use Insets class http://docs.oracle.com/javase/7/docs/api/java/awt/Insets.html to achieve that.
So you can set position of your component according to position you want
Insets insets = panel.getInsets();
Dimension size =test.getPreferredSize();
// just replace 10 & 0 according to X & Y postion you want.
test.setBounds(10 + insets.left, 0 + insets.top,size.width, size.height);
Here is you modified version:
*Note that I don't have your Icon , so I just put text on your label to help you see the result.
import java.awt.*;
import javax.swing.*;
public final class App{
// Window & Panel...
public JWindow frame;
public TranslucentPanel panel;
// OverlayLayout
public LayoutManager overlay;
// Components
public JLabel bg;
public JLabel test;
// Constructor
public App() {
try {
// Basics...
frame = new JWindow();
// Overlay
// Overlay
panel = new TranslucentPanel();
overlay = new OverlayLayout(panel);
panel.setLayout(overlay);
frame.add(panel);
initComponents();
// Finalize Frame
frame.pack();
frame.setSize(400,400);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
catch(Exception e) { e.printStackTrace();}
}
// Initialize Additional Components
public void initComponents() throws Exception {
test = new JLabel("test");
test.setForeground(Color.RED);
panel.setLayout(null);
panel.add(test);
Insets insets = panel.getInsets();
Dimension size =test.getPreferredSize();
test.setBounds(10 + insets.left, 0 + insets.top,
size.width, size.height);
frame.add(panel);
}
// TranslucentPanel Class...
class TranslucentPanel extends JPanel {
private static final long serialVersionUID = 1L;
public TranslucentPanel() {
setOpaque(false);
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D)g.create();
g2d.setComposite(AlphaComposite.SrcOver.derive(0.0f));
g2d.setColor(getBackground());
g2d.fillRect(0, 0, getWidth(), getHeight());
}
}
public static void main (String args []){
App ap = new App();
}
}
The output :
If you declare your position as test.setBounds(500 + insets.left, 10 + insets.top,size.width, size.height); then the output would be :