JPanel paint method is not being called, why?
Posted
by swift
on Stack Overflow
See other posts from Stack Overflow
or by swift
Published on 2010-04-08T10:44:55Z
Indexed on
2010/04/08
12:53 UTC
Read the original article
Hit count: 392
When i run this code the paintComponent method is not being called It may be very simple error but i dont know why this, plz.
package test;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.image.BufferedImage;
import javax.swing.BorderFactory;
import javax.swing.JPanel;
class Userboard extends JPanel
{
static BufferedImage image;
String shape;
Point start;
Point end;
Point mp;
String selected;
int ex,ey;//eraser
int w,h;
public Userboard()
{
setOpaque(false);
System.out.println("paper");
setBackground(Color.white);
setBorder(BorderFactory.createLineBorder(Color.black));
}
public void paintComponent(Graphics g)
{
System.out.println("userboard-paint");
try
{
//g.drawImage(image, 0, 0, this);
Graphics2D g2 = (Graphics2D)g;
g2.setPaint(Color.black);
if(start!=null && end!=null)
{
if(selected==("elipse"))
{
System.out.println("userboard-elipse");
g2.drawOval(start.x, start.y,(end.x-start.x),(end.y-start.y));
System.out.println("userboard-elipse drawn");
}
else if(selected==("rect"))
g2.drawRect(start.x, start.y, (end.x-start.x),(end.y-start.y));
else if(selected==("line"))
g2.drawLine(start.x,start.y,end.x,end.y);
}
}
catch(Exception e)
{}
}
//Function to draw the shape on image
public void draw()
{
System.out.println("Userboard-draw");
System.out.println(selected);
System.out.println(start);
System.out.println(end);
Graphics2D g2 = image.createGraphics();
g2.setPaint(Color.black);
if(start!=null && end!=null)
{
if(selected=="line")
g2.drawLine(start.x, start.y, end.x, end.y);
else if(selected=="elipse")
{
System.out.println("userboard-elipse");
g2.drawOval(start.x, start.y, (end.x-start.x),(end.y-start.y));
System.out.println("userboard-elipse drawn");
}
else if(selected=="rect")
g2.drawRect(start.x, start.y, (end.x-start.x),(end.y-start.y));
}
start=null;
repaint();
g2.dispose();
}
//To add the point to the board which is broadcasted by the server
public void addPoint(Point ps,String varname,String shape,String event)
{
try
{
if(end==null)
end = new Point();
if(start==null)
start = new Point();
if(shape.equals("elipse"))
this.selected="elipse";
else if(shape.equals("line"))
this.selected="line";
else if(shape.equals("rect"))
this.selected="rect";
else if(shape.equals("erase"))
erase();
if(end!=null && start!=null)
{
if(varname.equals("end"))
end=ps;
else if(varname.equals("mp"))
mp=ps;
else if(varname.equals("start"))
start=ps;
if(event.equals("drag"))
repaint();
else if(event.equals("release"))
draw();
}
repaint();
}
catch(Exception e)
{
e.printStackTrace();
}
}
//Function which provides the erase functionality
public void erase()
{
Graphics2D pic=(Graphics2D) image.getGraphics();
pic.setPaint(Color.white);
if(start!=null)
pic.fillRect(start.x, start.y, 10, 10);
}
//To set the size of the image
public void setWidth(int x,int y)
{
System.out.println("("+x+","+y+")");
w=x;
h=y;
image = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
}
//Function to add buttons into the panel, calling this function returns a panel
}
© Stack Overflow or respective owner