[SOLVED] Problem in Drawing(Repaint)
I want to show some parent-child relationship by drawing rectangles.first,i'll draw all the parent rectangles,on-click of parent rectangle,child should dispaly.(for all clicked rectangle).I implemented,first by drawing the all the Parent rectanlges.if any Parent rectangle was clicked,I 'll check whether
it has child,if it has,I have to call the (drawRect(Graphics g) and repaint everything)…But here,the problem is the screen is not repainting properly..can anyone help me…
Code:
import javax.swing.JApplet;
import javax.swing.JApplet;
import java.awt.event.MouseEvent;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseListener;
import java.util.*;
public class Tree extends JApplet
{
ArrayList <String> dlp;
String str;
int count;
public void init()
{
Container con = getContentPane();
con.setLayout(new BorderLayout());
setPreferredSize(new Dimension(300, 300));
JScrollPane scroll = new JScrollPane();
getContentPane().add(scroll, BorderLayout.CENTER);
scroll.setViewportView(new ImagePanel());
}
private class ImagePanel extends JPanel implements MouseListener
{
ImagePanel image;
Rectangle rext = new Rectangle(40, 60, 30, 15);
int[] x = {75, 95, 115, 130};
int[] width = {15, 15, 15, 15};
boolean selected1 = false;
int count1;
Rectangle [] rects;
Rectangle [] rectangles;
ArrayList<Rectangle> hlp;
Rectangle [] childRects ;
ArrayList <Rectangle> Rects = new ArrayList <Rectangle>();
boolean canDraw = false;
int child_count ;
boolean [] visible ;
ArrayList child = new ArrayList();
boolean selected2 = false;
ArrayList<String> dlp;
String HLP;
boolean canRedraw = false;
int y = 85;
Graphics g;
public ImagePanel()
{
prepareData();
this.addMouseListener(this);
}
@Override
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
int loc = rext.x+rext.width;
g2.draw(rext);
if(canRedraw == false){
drawRect(g2);
//repaint();
}
}
public void prepareData()
{
int x = 75;
int y = 85;
int w = 20;
int h = 15;
/*Adding datas to the list...*/
hlp = new ArrayList<Rectangle>();
for (int i = 0; i <4; i++){
hlp.add(new Rectangle(x, y, w, h));
y += 25;
}
rectangles = hlp.toArray(new Rectangle[hlp.size()]);
visible = new boolean[hlp.size()];
for(int j=0;j<hlp.size();j++)
{
visible[j] = false;
}
}
public void drawRect(Graphics g)
{
int x = 75;
int w = 20;
int h = 15;
Graphics2D g2 = (Graphics2D) g;
/*Draws initially....(Parent Rectangle)*/
if(canRedraw == false){
for (Rectangle r : rectangles)
{
g2.draw(r);
//repaint();
}
}
/*Draws when cliked on the rectangle(Parent)....*/
else if(canRedraw == true){
for(int i=0;i<hlp.size();i++)
{
System.out.println("no child"+y+" "+i);
g2.drawRect(x, y, w, h);
y +=25;
/*Checks whether its clicked...*/
if(visible[i] == true)
{
/*gets the number of child it has.*/
int count = child();
for(int j=0;j<count;j++)
{
g2.drawRect(x+15, y, w, h);
System.out.println("child"+y+" "+i);
y +=25;
}
}
}
//canRedraw = false;
}
}
public int child()
{
/*Returns no. of child Rectangle for the intially drawn rectanlge..*/
if(count1 == 0 )
{
child_count = 5;
return child_count;
}
else if(count1 == 1)
{
child_count = 1;
return child_count;
}
else if(count1 == 2)
{
child_count = 1;
return child_count;
}
return child_count;
}
public void mouseClicked(MouseEvent e)
{
g = getGraphics();
/*If the (Parent)Rectangle was clicked,it should redraw the completely.
*/
if(selected1)
{
drawRect(g);
}
}
public void mousePressed(MouseEvent e)
{
Point pres_point = e.getPoint();
for (int i=0;i<rectangles.length;i++)
{
if(rectangles[i].contains(pres_point))
{
selected1 = true;
visible[i] = true;
canRedraw = true;
count1 = i;
break;
}
}
}
public void mouseReleased(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
}
public static void main(String[] args)
{
JApplet applet = new Tree();
JFrame f = new JFrame("Tree");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(applet);
f.setSize(400, 400);
f.setLocationRelativeTo(null);
applet.init();
f.setVisible(true);
}
}