Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
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-01-2008, 04:35 PM
Member
 
Join Date: Jan 2008
Posts: 81
Preethi is on a distinguished road
Problem in mouse click n repaint
I drawn some rectangles.If one rectangle was clicked,the rectangles below it should move down..but for me,its not repainting.the old location as well as the new location was there when i click the rectangle...
Code:
import javax.swing.JApplet; /* * To change this template, choose Tools | Templates * and open the template in the editor. */ import java.awt.event.MouseEvent; import javax.swing.*; import java.awt.*; import java.awt.Graphics; import java.awt.Rectangle; import java.awt.Rectangle; import java.awt.event.MouseListener; import java.util.ArrayList; /** * * @author 8563 */ public class TestTree extends JApplet { @Override 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, 40, 30, 15); int[] x = {75, 95, 115, 130}; int[] width = {15, 15, 15, 15}; boolean selected1 = false; int count; Rectangle [] rects; Rectangle [] rectangles; ArrayList<Rectangle> hlp; public ImagePanel() { prepareData(); this.addMouseListener(this); } @Override protected void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D) g; g2.draw(rext); drawRect(g2); } public void prepareData() { int x = 75; int y = 85; int w = 20; int h = 15; 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()]); } public void drawRect(Graphics g) { Graphics2D g2 = (Graphics2D) g; for (Rectangle r : rectangles) { g2.draw(r); //repaint(); } } public void mouseClicked(MouseEvent e) { Point mse_clk = e.getPoint(); int count_incr = count+1; int y = 150; for(int i=count_incr;i<rectangles.length;i++) { if(selected1) { rectangles[i].setLocation(rectangles[i].x,rectangles[i].y+20); // repaint(); } repaint(); } } 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; count = 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 TestTree(); JFrame f = new JFrame(); 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-01-2008, 05:09 PM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: SW MO, USA
Posts: 1,475
Norm is on a distinguished road
A suggestion: add some debug statements (println()) that show how the values of your variables change.
Also some comments in the code describing what you are trying to do would be helpful.
For example why do you draw(rect) in paintComponent and then draw(r) in drawRect()?
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 07-02-2008, 05:06 AM
Senior Member
 
Join Date: Jun 2008
Posts: 425
Fubarable is on a distinguished road
cross-posted in the sun forums
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 07-04-2008, 07:27 AM
Member
 
Join Date: Jan 2008
Posts: 81
Preethi is on a distinguished road
Thank you all for your suggetion and time spent....i solved the issue.I added , super.paintComponent(g); in paintcomponent().
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 07-04-2008, 12:16 PM
Niveditha's Avatar
Senior Member
 
Join Date: May 2008
Posts: 299
Niveditha is on a distinguished road
Send a message via Skype™ to Niveditha
Preethi, kindly mark the thread as solved..
__________________
To finish sooner, take your own time....
Nivedithaaaa
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
repaint problem amith Java 2D 2 07-01-2008 01:10 AM
Problem in repaint Preethi AWT / Swing 16 03-18-2008 09:10 PM
Repaint problem swimberl Java 2D 1 02-16-2008 10:12 PM
Repaint problem swimberl Java 2D 0 01-06-2008 04:28 AM
Mouse Right click option not working in solaris and linux OS dinesh kaushik AWT / Swing 2 11-21-2007 05:02 PM


All times are GMT +3. The time now is 05:23 AM.


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