KOB
KOB

Reputation: 4565

Drawing circle at centre of JFrame doesn't update after `repaint()`

I am writing a program which involves creating a JFrame and drawing a circle inside it using drawOval() from the Graphics class. I have reached a problem where I am trying to create a point at the centre of the JFrame, and then draw my circle with this pont being the x and y coordinates of the circle. Here is my code so far:

import java.awt.Graphics;
import javax.swing.JFrame;
import java.awt.event.*;
import java.awt.geom.Point2D;
import java.awt.Point;

class MouseJFrameMotion  extends JFrame implements MouseMotionListener{

    int circleXcenter;
    int circleYcenter;  
    int circleRadius = 50;
    boolean show = false;

    public MouseJFrameMotion(){       
        addMouseMotionListener(this);
    }

    public void paint(Graphics g){     

        super.paint(g);

        if(show){
            g.drawOval(circleXcenter,circleYcenter, circleRadius*2,circleRadius*2);
        }
    }

    public void mouseDragged(MouseEvent e){

    }

    Point frameCenter = new Point((this.getWidth()/2), (this.getHeight()/2));

    public void mouseMoved(MouseEvent e){
        int xLocation = e.getX();
        int yLocation = e.getY();   
        show = true;
        circleXcenter = (int) frameCenter.getX();
        circleYcenter = (int) frameCenter.getY();
        repaint();
    }
}

public class GrowingCircle {

    public static void main(String[] args) {
        MouseJFrameMotion  myMouseJFrame = new MouseJFrameMotion();
        myMouseJFrame.setSize(500, 500);
        myMouseJFrame.setVisible(true);
    }
}

As you can see in the main() function, I set the size of the JFrame to 500x500. However, when the circle is drawn, it's x and y coordinates are (0,0) when I expect them to be (250, 250) based on Point frameCenter after repaint() is called. Where am I going wrong?

Upvotes: 0

Views: 349

Answers (3)

MadProgrammer
MadProgrammer

Reputation: 347332

So two things...

  1. Don't override paint of JFrame, there a JRootPane, contentPane and other components between the user and the frames surface which can interfere with the painting. Instead, use a JPanel and override its paintComponent method
  2. At the time Point frameCenter = new Point((this.getWidth()/2), (this.getHeight()/2)); is evaluated, the frame's size is 0x0, you need to reevaluate the frameCenter before you paint the circle. When you do this will depend on how dynamic you want the change to be

Upvotes: 2

Alexander Campos
Alexander Campos

Reputation: 111

When you are constructing the class MouseJFrameMotion, the variable frameCenter is defined and set width and height 0 and it will never change. So what you can do is to calculate the frame center when you are drawing.

public void mouseMoved(MouseEvent e) {
    int xLocation = e.getX();
    int yLocation = e.getY();
    show = true;
    Point frameCenter = new Point((this.getWidth() / 2), (this.getHeight() / 2));
    circleXcenter = (int) frameCenter.getX();
    circleYcenter = (int) frameCenter.getY();
    repaint();
}

Upvotes: 0

rilent
rilent

Reputation: 722

I think you need both repaint() and revalidate() method

Upvotes: 0

Related Questions