Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-15-2008, 01:51 PM
Member
 
Join Date: Jan 2008
Posts: 83
Preethi is on a distinguished road
[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); } }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-15-2008, 05:39 PM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,223
Norm is on a distinguished road
Quote:
screen is not repainting properly
Can you describe what is happening and what's wrong with it and what you'd like it to do?

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:
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 05:42 PM.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 07-16-2008, 09:28 AM
Member
 
Join Date: Jan 2008
Posts: 83
Preethi is on a distinguished road
The Problem was solved....Thanks for your advise and your input,Mr.Norm.

Last edited by Preethi : 07-16-2008 at 01:02 PM. Reason: Issue SOLVED
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem in mouse click n repaint Preethi New To Java 4 07-04-2008 01:16 PM
repaint problem amith Java 2D 2 07-01-2008 02:10 AM
Problem in repaint Preethi AWT / Swing 16 03-18-2008 10:10 PM
Repaint problem swimberl Java 2D 1 02-16-2008 11:12 PM
Repaint problem swimberl Java 2D 0 01-06-2008 05:28 AM


All times are GMT +3. The time now is 01:41 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org