1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Solved: PLEASE READ! Java applet, draw shapes at

Discussion in 'Software Development' started by ElementOfMagic, Jan 29, 2013.

Thread Status:
Not open for further replies.
Advertisement
  1. ElementOfMagic

    ElementOfMagic Thread Starter

    Joined:
    Dec 30, 2012
    Messages:
    52

    Full topic name: PLEASE READ! Java applet, draw shapes at mouse click centre.

    Currently in the below program, the the mouse coordinates represent the top left hand corner of the rectangle
    or oval. I need to alter the program, so that oval or rectangle are drawn with the mouse coordinates in the centre of the shape.

    Code:
    import java.applet.Applet;
    import java.awt.*;
    import java.awt.event.*;
    public class Mouse2 extends Applet implements MouseListener 
    { 
    
    	int xcord, ycord, which_shape=-1, size=20;
    
    	public void start( )
    	{
    		addMouseListener(this); 
    		setSize(200,200);
    	}
    
        public void paint ( Graphics g)
        {
    		switch(which_shape)
    	    {
    			case(0):
    				g.setColor(Color.red);
    				g.fillRect(xcord,ycord,size,size);
    				showStatus("Rectangle at " + xcord + " " + ycord);
    				break;
    			case(1):
    				g.setColor(Color.blue);
    				g.fillOval(xcord,ycord,size,size);
    				showStatus("Rectangle at " + xcord + " " + ycord);
    			break;
    	    }
        }
    
    
        public void mouseClicked( MouseEvent e)
        {
    		xcord=e.getX();
    		ycord=e.getY();
    		which_shape++;
    		which_shape=which_shape%2;
    		repaint();
        }
    
        public void mousePressed(MouseEvent e){}
        public void mouseEntered(MouseEvent e){}
    
        public void mouseReleased(MouseEvent e){}
        public void mouseExited(MouseEvent e){}
    
    }
    
    Any help/guidance will be much appreciated. :) (y)
     
  2. ElementOfMagic

    ElementOfMagic Thread Starter

    Joined:
    Dec 30, 2012
    Messages:
    52
    Please only look at this thread if you plan on contributing something to help me out.
     
  3. andythepandy

    andythepandy

    Joined:
    Jul 2, 2006
    Messages:
    379
    To make the centre of the shape be drawn where the mouse is, you need to subtract half the width of the shape from the xcord and half the height from the ycord. This is mean you are shifting it left and up so that the centre of the shape will be where the mouse is.

    Code:
    g.fillRect(xcord - (size / 2),ycord - (size / 2),size,size);
    
     
  4. ElementOfMagic

    ElementOfMagic Thread Starter

    Joined:
    Dec 30, 2012
    Messages:
    52
    That's great, thank-you!! :D
     
As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/1087440

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice