0

I try to make a JFrame in which I have a JPanel (which contains a circle) and it is bordered by four buttons (north, south, east, west). The circle will move in the direction indicated by the pressed button.
My problem is that I can't manage to put my JPanel in the center:

https://i.stack.imgur.com/Cv12Q.png

The JFrame's class looks like this:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;

@SuppressWarnings("serial")
public class Frame extends JFrame implements ActionListener {

JButton north, south, east, west;
int x = 10, y = 10;
MyPanel panel;

public Frame() {
    setLayout(new BorderLayout());
    panel = new MyPanel();
    panel.setBackground(Color.MAGENTA);

    north = new JButton("NORTH");
    south = new JButton("SOUTH");
    east = new JButton("EAST");
    west = new JButton("WEST");

    add(panel, BorderLayout.CENTER);
    add(north, BorderLayout.NORTH);
    add(south, BorderLayout.SOUTH);
    add(east, BorderLayout.EAST);
    add(west, BorderLayout.WEST);

    north.addActionListener(this);
    south.addActionListener(this);
    east.addActionListener(this);
    west.addActionListener(this);

    setBounds(100, 100, 300, 300);
    setVisible(true);
}

@Override
public void actionPerformed(ActionEvent e) {
    if (e.getSource() == north) {
        y -= 3;
        panel.setY(y);
        panel.repaint();
    }

    if (e.getSource() == south) {
        y += 3;
        panel.setY(y);
        panel.repaint();
    }

    if (e.getSource() == east) {
        x += 3;
        panel.setX(x);
        panel.repaint();
    }

    if (e.getSource() == west) {
        x -= 3;
        panel.setX(x);
        panel.repaint();
    }
}
}

And the MyPanel class looks like this:

import java.awt.Color;
import java.awt.Graphics;

import javax.swing.JPanel;

@SuppressWarnings("serial")
public class MyPanel extends JPanel {
private Color color = Color.CYAN;
private int x = 10, y = 10;

public void paint(Graphics g) {
    super.paintComponent(g);
    g.setColor(color);
    g.fillOval(x, y, 20, 20);
}

public int getX() {
    return x;
}

public void setX(int x) {
    this.x = x;
}

public int getY() {
    return y;
}

public void setY(int y) {
    this.y = y;
}
}
Barbi
  • 37
  • 7
  • [many duplicates](https://www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=site:stackoverflow.com+java+swing+center+jpanel+in+jframe) – Hovercraft Full Of Eels Nov 12 '16 at 21:30
  • @HovercraftFullOfEels, please look at the picture and the code (before you keep closing questions as duplicates). Why would the magenta panel be painted over the other components when a simple BorderLayout is being used? The magenta panel should be in the middle. The problem is with code we can't see.. – camickr Nov 12 '16 at 21:35
  • This question was originally closed as a duplicate of: http://stackoverflow.com/questions/7223530/how-can-i-properly-center-a-jpanel-fixed-size-inside-a-jframe. I reopened it because I don't believe it is a duplicate. If you replace the "MyPanel" with a "JPanel" the panel is dislayed in the center. So the problem is in the custom "MyPanel" class and we don't have access to that code so we can't help. – camickr Nov 12 '16 at 21:43
  • I suspect your code is changing the x/y location of the panel instead of the circle. Without the `MyPanel` code we don't know what your custom painting is doing. – camickr Nov 12 '16 at 21:49
  • @camickr thank you. you are right. It looks like the problem is with the x,y integers, like you said. – Barbi Nov 12 '16 at 21:50

1 Answers1

1

Don't override getX() and getY()methods of your custom panel. Those methods are used by Swing to determine the location of the component.

Instead you should have methods like setCirleX(...), setCircleY(...), getCircleX() and getCircleY().

camickr
  • 321,443
  • 19
  • 166
  • 288