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 6 of 6

Thread: JAVA LOOP HELP!!!

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    8
    My Mood
    Depressed
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default JAVA LOOP HELP!!!

    In this program, I need help in creating a loop in the paint method of Abacus Panel Class whose purpose is to find out the number of counters and draw these counters in the row of the selected column. The purpose of this program is to allow the user to add a counter in the row of the selected column when user clicks on it. (Any help on it or some ideas would be very much helpful for me)

     
    import java.awt.*;
    import java.awt.geom.*;
    import javax.swing.*;
    import java.awt.event.MouseListener;
    import java.awt.event.MouseEvent;
     
    class AbacusPanel extends JPanel implements MouseListener
    {
    	AbacusModel myAbacus;
     
     
     
     
    	int numCols;
    	int numRows;
    	int button;
     
     
    	public static void main(String[] args)
    	{
    		AbacusFrame w = new AbacusFrame();
    		w.setVisible(true);
    	}
     
     
    	public AbacusPanel(int nc, int nr)
    	{
    		numCols = nc;
    		numRows = nr;
    		addMouseListener(this);
    		myAbacus = new AbacusModel(numCols,numRows);
    	}
    	int getCol(int x)
    	{
    		return x*numCols/getWidth();
    	}
    	int getRow(int y)
    	{
    		return y*numRows/getHeight();
    	}
     
    	public void mouseReleased(MouseEvent event)
    	{
    	}
    	public void mousePressed(MouseEvent event)
    	{
    	}
    	public void mouseClicked(MouseEvent event)
    	{
    		int thisCol = getCol(event.getX());
    		int thisRow = getRow(event.getY());
    		System.out.println
    		("you clicked in column " + thisCol);
    		button = event.getButton();
    		if (button ==1)
    		{
    myAbacus.addCounter(getCol(event.getX()));
    		}
    		if (button ==3)
    		{
    myAbacus.removeCounter(getCol(event.getX()));
    		}
    System.out.println("peg selected="+getCol(event.getX())+ " and Counter contained="+myAbacus.getNumCounters(getCol(event.getX())));
    repaint();
     
    	}
    	public void mouseEntered(MouseEvent event)
    	{
    	}
    	public void mouseExited(MouseEvent event)
    	{
    	}
     
     
    	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));
    			}
    		}
    	for(int thisRow = 0;thisRow<numRows;thisRow++)
     
    				{
    					for(int thisCol = 0;thisCol<numCols;thisCol++)
     
    					{
     
     
     
    					}
    			}
    	}
    }

    import java.awt.*;
    import java.util.Arrays;
    import javax.swing.*;
     
    public class AbacusModel extends JFrame
    {
     
    int num_of_pegs;
    int max_num_counters;
    int peg_array[];
     
    public  AbacusModel(int num_pegs, int num_counters)
    {
     
    max_num_counters = num_counters;
    num_of_pegs = num_pegs;
    peg_array = new int[num_of_pegs];
     
    }
     
    public int getNumCounters(int thisPeg)
    {
    	if (thisPeg < 0 || thisPeg >= num_of_pegs)
    	return 0;
    	else
    	return peg_array[thisPeg];
    }
     
    public boolean addCounter(int thisPeg)
    {
    if (thisPeg <0 || thisPeg >= num_of_pegs)
     
    return false;
     
    if (peg_array[thisPeg] >= max_num_counters)
    return false;
     
    peg_array[thisPeg]++;
    return true;
     
    }
    public boolean removeCounter (int thisPeg)
    {
    if (thisPeg <0 || thisPeg >= num_of_pegs)
    return false;
    if( peg_array[thisPeg] <=0)
    return false;
     
    peg_array[thisPeg]--;
    return true;
    }
     
     
     
     
     
     
    }

    import java.awt.*;
    import java.awt.geom.*;
    import javax.swing.*;
     
     
    public class AbacusFrame extends JFrame
    {
    	AbacusPanel theAbacusPanel;
     
    	public static void main(String[] args)
    	{
    		AbacusFrame w = new AbacusFrame();
    		w.setVisible(true);
    	}
     
    	public AbacusFrame()
    	{
    		setTitle("Workshop 3 (Abacus): starting code");
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setSize(500,220);
    		setLocation(300,300);
    		theAbacusPanel = new AbacusPanel(5, 7);
    		add(theAbacusPanel);
     	}
     
     }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: JAVA LOOP HELP!!!

    Can you explain what your problem is?

  3. #3
    Junior Member
    Join Date
    Feb 2012
    Posts
    8
    My Mood
    Depressed
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: JAVA LOOP HELP!!!

    My problem is , I don't know how to use the NumCounters method from the from the "AbacusModel class" inside the paint method of "AbacuPanel class" to create a loop so that the program can understand how many counters to be drawn in each rows of the selected column.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: JAVA LOOP HELP!!!

    Where is the NumCounters() method? I do not find one when I search for it.

  5. #5
    Junior Member
    Join Date
    Feb 2012
    Posts
    8
    My Mood
    Depressed
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: JAVA LOOP HELP!!!

    don't worry,, i will try to solve it myself... sorry for trouble...

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: JAVA LOOP HELP!!!

    Good luck.

    When you ask someone to look at your code, it is very useful if you spell the names correctly and not require any guessing.

Similar Threads

  1. Java loop help
    By jetlag in forum Java Theory & Questions
    Replies: 2
    Last Post: March 10th, 2012, 02:31 PM
  2. loop in java
    By casperl90 in forum Loops & Control Statements
    Replies: 2
    Last Post: August 8th, 2011, 03:17 AM
  3. loop in java
    By jonnitwo in forum Loops & Control Statements
    Replies: 4
    Last Post: July 8th, 2011, 02:13 PM
  4. for loop in java
    By kissmiss in forum Loops & Control Statements
    Replies: 1
    Last Post: December 16th, 2009, 03:47 AM
  5. JAVA for loop
    By tazjaime in forum Loops & Control Statements
    Replies: 2
    Last Post: August 18th, 2009, 07:43 PM