Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Help in Java

  1. #1
    Junior Member
    Join Date
    Mar 2010
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Help in Java

    hello everyone : I dont understand why we need to first declare the boolean as false :boolean clickControl = false;
    and then true and then true again please help .... here is the code:
    import java.awt.*;
    import java.awt.geom.*;
    import javax.swing.*;
    import java.awt.event.MouseListener;
    import java.awt.event.MouseEvent;
     
     
    public class Week1MouseEventsQ6 extends JFrame
    {
    	DrawPanel canvas;
     
    	public static void main(String[] args)
    	{
    		Week1MouseEventsQ6 w = new Week1MouseEventsQ6();
    		w.setVisible(true);
    	}
     
    	public Week1MouseEventsQ6()
    	{
    		setTitle("Week2MouseEvents: starting code");
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setSize(500,220);
    		setLocation(300,300);
    		canvas = new DrawPanel(7, 5);
    		add(canvas);
     	}
     
    	class DrawPanel extends JPanel implements MouseListener
    	{
    		int numCols;
    		int numRows;
     
    		int clickColumnIndex = -1;
    		int clickRowIndex = -1;
     
    		int buttonNumber = 0;
     
    		boolean clickControl = false;
     
    		public void mouseReleased(MouseEvent event)
    		{
    			System.out.println("Mouse Released");
    			System.out.println("X: " + event.getX() + "Y: " + event.getY());
    		}
    		public void mousePressed(MouseEvent event)
    		{
    			System.out.println("Mouse Pressed");
    			System.out.println("X: " + event.getX() + "Y: " + event.getY());
    		}
    		public void mouseClicked(MouseEvent event)
    		{
    			buttonNumber = event.getButton();
     
    			clickColumnIndex = (numCols) * (event.getX()) / (getWidth());
    			clickRowIndex = (numRows) * (event.getY()) / (getHeight());
     
    			clickControl = true;
     
    			System.out.println("Mouse Clicked");
    			System.out.println("X: " + event.getX() + "Y: " + event.getY());
    			System.out.println("Row " + clickRowIndex + "Column " + clickColumnIndex);
     
    			repaint();
     
    		}
    		public void mouseEntered(MouseEvent event)
    		{
    			System.out.println("Mouse Entered");
    			System.out.println("X: " + event.getX() + "Y: " + event.getY());
    		}
    		public void mouseExited(MouseEvent event)
    		{
    			System.out.println("Mouse Exited");
    			System.out.println("X: " + event.getX() + "Y: " + event.getY());
    		}
     
     
    		public DrawPanel(int nc, int nr)
    		{
    			numCols = nc;
    			numRows = nr;
     
    			addMouseListener(this);
    		}
     
    		Rectangle getRect(int thisCol, int thisRow)
    		{
    			// if input is out of range, return "null"
    			if(thisCol <0 || thisRow < 0)
    				return null;
    			if(thisCol>=numCols || thisRow>=numRows)
    				return null;
     
    			// otherwise, make and return the Rectangle
    			int w = getWidth()/numCols;
    			int h = getHeight()/numRows;
     
    			int x = thisCol*w;
    			int y = thisRow*h;
     
    			Rectangle myRect = new Rectangle(x,y,w,h);
    			return myRect;
    		}
     
    		public void paint(Graphics g)
    		{
    			g.setColor(Color.gray);
    			g.fillRect(0,0,getWidth(), getHeight());
    			g.setColor(Color.black);
     
     
    			Graphics2D g2 = (Graphics2D)g;
    			// we'll use Graphics2D for it's "draw" method -
    			// neater than the Graphics "drawRect" suppled
    			// (which you could also use)
     
    			for (int i = 0;i<numCols;i++)
    				for(int j = 0;j<numRows;j++)
    					g2.draw(getRect(i,j));
     
    			if (clickControl == true)
    			{
    			Rectangle r2 = getRect(clickColumnIndex,clickRowIndex);
    			g.setColor(Color.red);
    			g.fillOval(r2.x,r2.y,r2.width, r2.height);
    			}
     
    			if (buttonNumber == 3)
    			{
    			Rectangle r3 = getRect(clickColumnIndex,clickRowIndex);
    			g.setColor(Color.yellow);
    			g.fillOval(r3.x,r3.y,r3.width, r3.height);
    			}
     
    		}
     
    	}
     
     }
    Last edited by helloworld922; March 22nd, 2010 at 07:38 PM.


  2. #2
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    495
    Thanks
    8
    Thanked 48 Times in 46 Posts

    Default Re: Help in Java


  3. The Following User Says Thank You to Darryl.Burke For This Useful Post:

    Kong_Han_Dao (March 21st, 2010)

  4. #3
    Junior Member
    Join Date
    Mar 2010
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help in Java

    i cannot find it can you help me