Solved: PLEASE READ! Java applet, draw shapes at

Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

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)
 
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);
 

ElementOfMagic

Thread Starter
Joined
Dec 30, 2012
Messages
52
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);
That's great, thank-you!! :D
 
Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

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 807,865 other people just like you!

Latest posts

Staff online

Top