Results 1 to 5 of 5
  1. #1
    Preethi is offline Member
    Join Date
    Jan 2008
    Posts
    83
    Rep Power
    0

    Default 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...
    Java 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);
        }
    }

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,607
    Rep Power
    23

    Default

    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()?

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    cross-posted in the sun forums

  4. #4
    Preethi is offline Member
    Join Date
    Jan 2008
    Posts
    83
    Rep Power
    0

    Default

    Thank you all for your suggetion and time spent....i solved the issue.I added , super.paintComponent(g); in paintcomponent().

  5. #5
    Niveditha's Avatar
    Niveditha is offline Senior Member
    Join Date
    May 2008
    Posts
    307
    Rep Power
    6

    Default

    Preethi, kindly mark the thread as solved.. :)
    To finish sooner, take your own time....
    Nivedithaaaa

Similar Threads

  1. repaint problem
    By amith in forum Java 2D
    Replies: 2
    Last Post: 07-01-2008, 12:10 AM
  2. Problem in repaint
    By Preethi in forum AWT / Swing
    Replies: 16
    Last Post: 03-18-2008, 08:10 PM
  3. Repaint problem
    By swimberl in forum Java 2D
    Replies: 1
    Last Post: 02-16-2008, 09:12 PM
  4. Repaint problem
    By swimberl in forum Java 2D
    Replies: 0
    Last Post: 01-06-2008, 03:28 AM
  5. Mouse Right click option not working in solaris and linux OS
    By dinesh kaushik in forum AWT / Swing
    Replies: 2
    Last Post: 11-21-2007, 04:02 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •