Pergunta

How to get the x and y offset for the coordinates of a JPanel?

I am trying to draw a circle in the middle of a square (a panel) using the paint method of the frame the panel is in ( I want the circle to move to other squares so i can't draw on the panel itself)

now as easy as that sounds... when I try to draw the circle relative to the x and y coordinates of the panel there seems to be an offset such that my circles coordinate is always a bit shifted from the middle (also tried that with a rectangle).

yet I found the x offset to be constant at 8 but the y offset is changing so i cant fix that hard coded...

My constructor and paint method look like this:

    public Client() {
    tokens = new int[8][8];
    field = new JPanel[8][8];
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    // setResizable(false);
    setBounds(100, 100, WIDTH, HEIGHT);
    setLocationRelativeTo(null);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(new GridLayout(8, 8, 0, 0));

    // Draw field
    int row = -1;
    for (int i = 0; i < 64; i++) {
        if (i % 8 == 0)
            row++;
        JPanel panel = new JPanel();
        if ((i + row) % 2 == 0)
            panel.setBackground(Color.BLACK);
        else
            panel.setBackground(Color.WHITE);
        field[row][i % 8] = panel;
        contentPane.add(panel);
    }
}

    private void resizeToken(){
    tokenWidth = field[0][0].getWidth();
    tokenHeight = field[0][0].getHeight();
    xOffset = 8;
    //this one is wrong and just placed as example
    yOffset = 37;

}

public void paint(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;
    super.paint(g);
    resizeToken();
    // draw all tokens
    tokens[1][1] = PLAYER_ONE;
    for (int row = 0; row < 8; row++) {
        for (int i = 0; i < 8; i++) {
            if (tokens[row][i] != EMPTY_FIELD) {
                JPanel p = field[row][i];
                if (tokens[row][i] == PLAYER_ONE)
                    g2.setColor(Color.RED);
                else
                    g2.setColor(Color.BLUE);
                g2.fillOval(p.getBounds().x+xOffset, p.getBounds().y-yOffset, tokenWidth, tokenHeight);
            }
        }
    }
    if (movingToken != null)
        g2.fillOval(movingToken.x, movingToken.y, tokenWidth, tokenHeight);
}
Foi útil?

Solução

First thing:

DONT USE PAINT METHOD OF JFRAME. Instead, use paintComponent of the jpanel where you are trying to draw the circle.

Second thing:

A circle is basically an ellipse inside a rectangle which touches the borders of the rectangle.

So you just need to draw a rectangle theoretically.To draw a circle which covers entire panel

Ellipse2D circle = new Ellipse.Double(0,0,panelWidth,panelHeight);

public void paintComponent(Graphics g){
     super.paintComponent(g);//this is very important
      Graphics2D g2 = (Graphics2D) g;
      ....
      Ellipse2D circle = new Ellipse.Double(0,0,panelWidth,panelHeight);
      g2.draw(circle);  
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top