Results 1 to 3 of 3
- 07-15-2008, 11:51 AM #1
Member
- Join Date
- Jan 2008
- Posts
- 83
- Rep Power
- 0
[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…
Java 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); } }
- 07-15-2008, 03:39 PM #2
Can you describe what is happening and what's wrong with it and what you'd like it to do?screen is not repainting properly
Some questions/comments:
Why are you using the JApplet class?
You don't add components directly to a JFrame, use its contentPane.
Where do you call repaint(), it seems to be commented out.
repaint is called when you want the system to call your paintComponent method to actually draw something. Your program logic should decide when the display needs to be re-drawn, do some computations and then calls repaint() which queues the request and sometime later the system calls the paintComponent() method.
Put some comments in you code to describe what each routine expects to happen and how the control flow should be. For example:Java Code:// When user presses the button, the new boundary is computed and then repaint is called to // cause the box to be drawn at the new location.
Last edited by Norm; 07-15-2008 at 03:42 PM.
- 07-16-2008, 07:28 AM #3
Member
- Join Date
- Jan 2008
- Posts
- 83
- Rep Power
- 0
Similar Threads
-
Problem in mouse click n repaint
By Preethi in forum New To JavaReplies: 4Last Post: 07-04-2008, 11:16 AM -
repaint problem
By amith in forum Java 2DReplies: 2Last Post: 07-01-2008, 12:10 AM -
Problem in repaint
By Preethi in forum AWT / SwingReplies: 16Last Post: 03-18-2008, 08:10 PM -
Repaint problem
By swimberl in forum Java 2DReplies: 1Last Post: 02-16-2008, 09:12 PM -
Repaint problem
By swimberl in forum Java 2DReplies: 0Last Post: 01-06-2008, 03:28 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks