Results 1 to 5 of 5
Thread: Problem in mouse click n repaint
- 07-01-2008, 03:35 PM #1
Member
- Join Date
- Jan 2008
- Posts
- 83
- Rep Power
- 0
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); } }
- 07-01-2008, 04:09 PM #2
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()?
-
cross-posted in the sun forums
- 07-04-2008, 06:27 AM #4
Member
- Join Date
- Jan 2008
- Posts
- 83
- Rep Power
- 0
Thank you all for your suggetion and time spent....i solved the issue.I added , super.paintComponent(g); in paintcomponent().
- 07-04-2008, 11:16 AM #5
Similar Threads
-
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 -
Mouse Right click option not working in solaris and linux OS
By dinesh kaushik in forum AWT / SwingReplies: 2Last Post: 11-21-2007, 04:02 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks