Repardeimaj Repardeimaj - 5 months ago 17
Java Question

java Paint component declaring shape and size outside of paintcomponent

import java.awt.Graphics;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JPanel;
import javax.swing.Timer;



public class Panel extends JPanel implements ActionListener {


Timer t = new Timer(5, this);

public Panel(){

t.start();
setFocusable(true);
setFocusTraversalKeysEnabled(false);
}

public void paintComponent(Graphics g){

}

public void actionPerformed(ActionEvent e) {
repaint();

}
}


I want to set the shape and size of my graphic outside of the paint component, but I don.t know how to. I have tried Oval o = new Oval but it doesn't work please help me.

Answer

Have you tried this ?

    Ellipse2D.Double oval = new Ellipse2D.Double(x, y, width, height);
     public void paintComponent(Graphics g)
    {
         g.fill(oval);
    }

Then you could just fill it in with g2D