View Single Post
  #1 (permalink)  
Old 07-01-2008, 05:35 PM
Preethi Preethi is offline
Member
 
Join Date: Jan 2008
Posts: 83
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); } }
Reply With Quote
Sponsored Links