Custom component doesn't show up

269 views Asked by At

When I set size of the component something more than the x and y of the location of the component both of them show up, otherwise, they don't.

Here is my code:

public class AdamakPogram{

  public static void main(String[] args) {
     PlayGround playGround = new PlayGround();
     Adamak ad1 = new Adamak();
     Adamak ad2 = new Adamak();
     ad1.setLocation(100,100);
     ad2.setLocation(150, 100);
     ad1.addMouseListener(new FocusListener(ad1));
     ad1.addKeyListener(new ArrowListener(ad1));
     playGround.addPerson(ad1);
     ad2.addMouseListener(new FocusListener(ad2));
     ad2.addKeyListener(new ArrowListener(ad2));
     playGround.addPerson(ad2);
 }  
}

My PlayGround class:

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

public class PlayGround extends JFrame{

   private static final long serialVersionUID = 1L;
   private JPanel zone;

public PlayGround() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 800, 600);
    setResizable(false);
    zone = new JPanel();
    zone.setBorder(new EmptyBorder(5, 5, 5, 5));
    zone.setBounds(100, 100, 800, 600);
    setContentPane(zone);
    zone.setLayout(null);
    setVisible(true);
}

public void addPerson(Adamak adamak){
    zone.add(adamak);
    revalidate();
    repaint();
}

public JPanel getZone() {
    return zone;
}

public void setZone(JPanel zone) {
    this.zone = zone;
}

}

And my adamak(custom component) class:

import java.awt.Graphics;
import java.awt.Point;

import javax.swing.JComponent;

public class Adamak extends JComponent {

   private static final long serialVersionUID = 1L;

   @Override
   protected void paintComponent(Graphics arg0) {
     super.paintComponent(arg0);
     Point point = getLocation();
     arg0.drawRect(point.x-10, point.y-10, 20, 50);//drawing stomach
     arg0.drawLine(point.x+10,point.y, point.x+20, point.y+50);//drawing right hand
     arg0.drawLine(point.x-10, point.y, point.x-20, point.y+50);//drawing left hand
     arg0.drawLine(point.x, point.y-10, point.x, point.y-20);//drawing neck
     arg0.drawLine(point.x-5, point.y+40, point.x-5, point.y+100);//drawing left leg
     arg0.drawLine(point.x+5, point.y+40, point.x+5, point.y+100);//drawing right leg
     arg0.drawOval(point.x-10, point.y-40, 23, 20);//drawing head
     arg0.drawRect(point.x-20, point.y-40, 40, 140);
}

public void moveRight(){
    Point point = getLocation();
    if((point.x+1-20) < 710){
        setLocation(new Point(point.x+1, point.y));
        setBounds(point.x-20,point.y-40,710-40,461-140);
        repaint();
    }
}

public void moveLeft(){
    Point point = getLocation();
    if((point.x-1-20) > 0){
        setLocation(new Point(point.x-1,point.y));
        setBounds(point.x-20,point.y-40,710-40,461-140);
        repaint();
    }
}

public void moveUp(){
    Point point = getLocation();
    if((point.y-1-40) > 0){
        setLocation(new Point(point.x,point.y-1));
        setBounds(point.x-20,point.y-40,710-40,461-140);
        repaint();
    }
}

public void moveDown(){
    Point point = getLocation();
    if((point.y+1-100) < 461){
        setLocation(new Point(point.x,point.y+1));
        setBounds(point.x-20,point.y-40,710-40,461-140);
        repaint();
    }
}
}

Any help would be appreciated.

1

There are 1 answers

2
Madhawa Priyashantha On

when you add child components to null layout(absolute positioning) you should specify the position and dimension of the component exactly.

using setbounds(x,y,width,height) method you can do it.

since you are set location for adamax component setSize() is enough.

example

Adamak ad1 = new Adamak();
ad1.setSize(300, 300);
ad1.setLocation(100, 100);

or using setBounds()

Adamak ad1 = new Adamak();
ad1.setBounds(100,100,300,300);

edit

this is for your comment

yes let me explain using the rectangle you are drawing .

the main problem is this code

Point point = getLocation();

this return the location of Adamakrelative to it's parent container in this case

ad1.setLocation(100,100); 

and from the paintComponent method you call

 Point point = getLocation();

now point.x=100 and point.y=100 and you draw the outer box using

arg0.drawRect(point.x-20, point.y-40, 40, 140);

same as arg0.drawRect(80, 60, 40, 140);

but your component width you expect to be 40 and how can this be possible?? it start drawing from x of the component which is 80 but your panel have width of 40 .so you not gonna see anything .at least (80 + 40) width component need to display left right of the rectangle but border lines has some thikness so you need 120+ width component to show it.

what you want is change this point for instance

Point point = new Point(20, yyy);
arg0.drawRect(point.x-20, point.y-40, 40, something);

and now 40+ width component is fine .because this is same as arg0.drawRect(0, point.y-40, 40, something); it start drawing from very right corner which is 0 and width is 40 also component have width 40 + .same for y and height

this is output

enter image description here

but

if you want to move your spirit instead moving jcomponents which is not efficient use paintComponent method to redraw them .this is good example https://www3.ntu.edu.sg/home/ehchua/programming/java/J8a_GameIntro-BouncingBalls.html