One of the things that's been confusing me so far is why we have to override the paintcomponent() function after extending it. For example, this program draws an ellipse:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;
import javax.swing.JComponent;
public class DrawEllipseComponent extends JComponent
{
public void paintComponent(Graphics g)
{
Graphics2D g2 = (Graphics2D)g;
Ellipse2D.Double ellipse = new Ellipse2D.Double(0,0,this.getWidth(),this.getHeight());
g2.draw(ellipse);
g2.setColor(Color.GREEN);
g2.fill(ellipse);
}
}
import javax.swing.JFrame;
public class DrawEllipse
{
public static void main(String[] args)
{
DrawEllipseComponent ellipse = new DrawEllipseComponent();
JFrame frame = new JFrame();
frame.setSize(300, 400);
frame.setTitle("An Ellipse");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(ellipse);
frame.setVisible(true);
}
}
I can see that you create an instance of the object, create a frame, and put the object into the frame. Outside of that, I'm not quite sure I understand how the ellipse is being drawn here.