Results 1 to 8 of 8
  1. #1
    cselic is offline Senior Member
    Join Date
    Apr 2010
    Location
    Belgrade, Serbia
    Posts
    278
    Rep Power
    5

    Default Draw rectangle in applet with coordinates of two mouse clicks

    Hi. I need help with my applet.
    When I click with mouse first time it should get coordinates of
    top left position of my rectangle. Then when I click with mouse
    second time it should get coordinates of bottom right position of
    my rectangle. And finally I want to paint that rectangle.
    I wrote some code but it's not good.

    How it can be done?

    Java Code:
    import java.awt.Graphics;
    
    import java.awt.event.MouseListener;
    import java.awt.event.MouseEvent;
    
    import javax.swing.JApplet;
    
    public class MyApplet extends JApplet implements MouseListener{
    	boolean isClicked = false;
    	boolean twoClicks = false;
    	int firstXCoord = 0;
    	int firstYCoord = 0;
    	int secondXCoord = 0;
    	int secondYCoord = 0;
    
    	public void mouseClicked(MouseEvent event) {
    		if(!isClicked) {
    			firstXCoord = event.getX();
    			firstYCoord = event.getY();
    			repaint();
    		}
    		else {
    			secondXCoord = event.getX();
    			secondYCoord = event.getY();
    		}
    		twoClicks = true;
    		repaint();
    	}
    	
    	public void paint(Graphics g) {
    		if(twoClicks)
    			g.drawRect(firstXCoord, firstYCoord, secondXCoord - firstXCoord, secondXCoord - firstXCoord);
    	}
    
    	
    	public void mouseEntered(MouseEvent e) {}
    	public void mouseExited(MouseEvent e) {}
    	public void mousePressed(MouseEvent e) {}
    	public void mouseReleased(MouseEvent e) {}
    
    }

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

    Default

    You must do more than just create a MouseListener; you also have to add a MouseListener to a component that will accept it. I recommend that you create a JPanel and add the mouse listener to the JPanel and also do the drawing in the JPanel's paintComponent override. Then add this JPanel to your JApplet's contentPane.

  3. #3
    cselic is offline Senior Member
    Join Date
    Apr 2010
    Location
    Belgrade, Serbia
    Posts
    278
    Rep Power
    5

    Default

    Thank you. I'll try that right now.

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

    Default

    You'll also want to check your logic again. You check if isClicked is true or not, but do you ever change the state of this field?

  5. #5
    cselic is offline Senior Member
    Join Date
    Apr 2010
    Location
    Belgrade, Serbia
    Posts
    278
    Rep Power
    5

    Default

    Thank you so much.

    My final code is working. Final code:

    Java Code:
    import java.awt.Graphics;
    import java.awt.Container;
    
    import java.awt.event.MouseListener;
    import java.awt.event.MouseEvent;
    
    import javax.swing.JApplet;
    import javax.swing.JPanel;
    
    public class MyApplet extends JApplet {
    	MyPanel panel = new MyPanel();
    	
    	public void init() {
    		Container content = getContentPane();
    		content.add(panel);
    	}
    	
    	class MyPanel extends JPanel implements MouseListener {
    		
    		boolean isClicked = false;
    		boolean twoClicks = false;
    		int firstXCoord = 0;
    		int firstYCoord = 0;
    		int secondXCoord = 0;
    		int secondYCoord = 0;
    		
    		MyPanel() {
    			addMouseListener(this);
    		}
    		
    		public void mouseClicked(MouseEvent event) {
    			if(!isClicked) {
    				firstXCoord = event.getX();
    				firstYCoord = event.getY();
    				isClicked = true;
    				
    			}
    			else {
    				secondXCoord = event.getX();
    				secondYCoord = event.getY();
    				repaint();
    			}
    			twoClicks = true;
    			repaint();
    		}
    		
    		public void mouseEntered(MouseEvent e) {}
    		public void mouseExited(MouseEvent e) {}
    		public void mousePressed(MouseEvent e) {}
    		public void mouseReleased(MouseEvent e) {}
    		
    		@Override public void paintComponent(Graphics g) {
    	         super.paintComponent(g); 
    		 g.drawRect(firstXCoord, firstYCoord, secondXCoord - firstXCoord, secondYCoord - firstYCoord);
    		}
    	}
    }
    Last edited by cselic; 04-10-2010 at 03:04 PM.

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

  7. #7
    cselic is offline Senior Member
    Join Date
    Apr 2010
    Location
    Belgrade, Serbia
    Posts
    278
    Rep Power
    5

    Default

    it's very useful example.

    For practice, I'll try to do with my applet few things more.
    First, I'm going to add applet parameters: rowNumber, columnNumber.
    Second, I'll split existing rectangle into small rectangles.
    rowNuber would give me a number of small rectangles into row, and
    columnNumber would give me a number of small rectangles into column of big rectangle.
    If I succeed, I'll post code.

    html page:
    Java Code:
    <html>
    <head>
    <title>rectangle</title>
    </head>
    <body>
    	<APPLET CODE="MyApplet" WIDTH=300 HEIGHT=300> 
    		<param name="rowNumber" value="5"> 
    		<param name="columnNumber" value="7"> 
    	</APPLET>
     </body>
     </html>

  8. #8
    cselic is offline Senior Member
    Join Date
    Apr 2010
    Location
    Belgrade, Serbia
    Posts
    278
    Rep Power
    5

    Default

    MyApplet.java

    Java Code:
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Container;
    
    import java.awt.event.MouseListener;
    import java.awt.event.MouseEvent;
    import java.awt.geom.Rectangle2D;
    
    import javax.swing.JApplet;
    import javax.swing.JPanel;
    
    public class MyApplet extends JApplet {
    	int rowNumber = 3;
    	int columnNumber = 3;
    	
    	MyPanel panel = new MyPanel();
    	
    	public void init() {
    		String numberOfRows = getParameter("rowNumber");
    		String numberOfColumns = getParameter("columnNumber");
    		
    		if(numberOfRows != null && numberOfColumns != null) {
    			rowNumber = Integer.parseInt(numberOfRows);
    			columnNumber = Integer.parseInt(numberOfColumns);
    		}
    		
    		Container content = getContentPane();
    		content.add(panel);
    	}
    	
    	class MyPanel extends JPanel implements MouseListener {
    		
    		boolean isClicked = false;
    		boolean twoClicks = false;
    		int firstXCoord = 0;
    		int firstYCoord = 0;
    		int secondXCoord = 0;
    		int secondYCoord = 0;
    		
    		MyPanel() {
    			addMouseListener(this);
    		}
    		
    		public void mouseClicked(MouseEvent event) {
    			if(!isClicked) {
    				firstXCoord = event.getX();
    				firstYCoord = event.getY();
    				isClicked = true;
    				
    			}
    			else {
    				secondXCoord = event.getX();
    				secondYCoord = event.getY();
    				repaint();
    			}
    			twoClicks = true;
    		}
    		
    		public void mouseEntered(MouseEvent e) {}
    		public void mouseExited(MouseEvent e) {}
    		public void mousePressed(MouseEvent e) {}
    		public void mouseReleased(MouseEvent e) {}
    		
    		@Override 
    		public void paintComponent(Graphics g) {
    	         super.paintComponent(g);
    	         Graphics2D g2D = (Graphics2D)g;
    	         	
    	         	if(twoClicks) {
    	         		float widthOfLittleRectangle = (secondXCoord - firstXCoord) / rowNumber;
    	         		float heightOFLittleRectangle = (secondYCoord - firstYCoord) / columnNumber;
    	         		
    	         		for(int i = 0; i < rowNumber; i++)
    	         			for(int j = 0; j < columnNumber; j++) {
    	         				g2D.draw(new Rectangle2D.Float(firstXCoord + widthOfLittleRectangle * i, firstYCoord + heightOFLittleRectangle * j,
    	         						widthOfLittleRectangle, heightOFLittleRectangle));
    	         			}
    	         		twoClicks = false;
    	         		isClicked = false;
    	         	}
    		}
    	}
    }

Similar Threads

  1. Getting mouse coordinates
    By nishant.4545 in forum Advanced Java
    Replies: 3
    Last Post: 07-21-2009, 12:28 AM
  2. Draw String in Rectangle
    By DavidG24 in forum AWT / Swing
    Replies: 3
    Last Post: 05-20-2009, 08:05 AM
  3. Replies: 7
    Last Post: 04-14-2009, 08:12 PM
  4. [SOLVED] Intercepting mouse clicks
    By carderne in forum New To Java
    Replies: 6
    Last Post: 02-28-2009, 08:10 PM
  5. How to Draw a Rectangle in Java
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 06-23-2008, 12:09 AM

Posting Permissions

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