java graphics question

0 pratt 15 · August 2, 2014
Hey i was just playing with my java program and i found that there is no need to add 
super.paintComponent(g); in method paintComponent. Is it right to write code without it coz books always uses super.paintComponent(g) before drawing graphics....:angel:

MY code->

package pratt;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.Graphics;
import java.awt.Polygon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JColorChooser;

import org.w3c.dom.css.RGBColor;

public class Project2 extends JPanel {

public void paintComponent(Graphics g){

int x1[]={50,100,100,50};
int y1[]={50,50,200,200};

int x2[]={100,150,200,350};
int y2[]={50,150,200,300};
Polygon p1=new Polygon(x2,y2,4);

Polygon p2=new Polygon();
p2.addPoint(120, 140);
p2.addPoint(500, 500);



Post a Reply


Oldest  Newest  Rating
0 Colin James · August 3, 2014
Every single Java swing control, be it JButtons or JScrollPanes, they're all painted (fancy term for drawn) to the screen.

Since your class extends the JPanel class it adopts all of the methods contained within the JPanel class. You won't have to call paintComponent because it is already called when the control becomes visible. 

Simply: Your current code is just overriding the default paintComponent method which is called by default so you don't have to call it yourself. You can call repaint() on it though and that will call paintComponent.
  • 1

Java / Android Development


Very popular language used to create desktop applications, website applets, and Android apps.

Bucky Roberts Administrator