Results 1 to 3 of 3
  1. #1
    obdi is offline Member
    Join Date
    Jul 2008
    Location
    CA
    Posts
    15
    Rep Power
    0

    Default Beginner's Problem on Loop/If statement

    New problem on Loop/If statement. Need to make the circle orbiting from the center by going counter wise 1 circle, clock wise 1 circle, so on for several times. I tried to put if statement in paint() but not working, and i did if in actionPerformed() still not working, please help!


    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.geom.*;
    
    public class CirclingBall extends JFrame
    {
    	public void lauchFrame()
    	{
    		int Row,Col;
    		Toolkit MyKit   = Toolkit.getDefaultToolkit();
    		Dimension XY    = MyKit.getScreenSize();
    		Row             = XY.width;
    		Col             = XY.height;
    		Image MyImage   = MyKit.getImage(":");
    		this.setIconImage(MyImage);
    		this.setTitle("Circle-Circle");
    		
    		this.setSize(7*Col/8, 7*Col/8);
    		this.setLocation(Row/32,Col/32);
    		this.setResizable(false);
    		
    		MyPanel Panel01 = new MyPanel();
    		Container ContentPanel01 = getContentPane();
    		ContentPanel01.add(Panel01);
    		
    		this.addWindowListener(new WindowAdapter()
    		{
    			public void windowClosing(WindowEvent e) 
    			{
    				System.exit(0);
    			}	
    		});
    		setVisible(true);
    	}
    	private class MyPanel extends JPanel
    	{
    		public void paint(Graphics g)
    		{
    			super.paint(g);
    			Graphics2D gr = (Graphics2D) g;
    			
    			int X,Y,Z,x1,x2,y1,y2;
    			int Row,Col,RadiusCenter,RadiusApart,RadiusOutter;
    			int CenterX, CenterY;
    			int Ax,Ay,Bx,By,Cx,Cy,Dx,Dy,RunLine;
    			
    			Dimension RowCol;
    			RowCol = getSize();
    			ROW = RowCol.width;
    			COL = RowCol.height;
    			double Radian = Math.PI/180;
    			CenterX = ROW/2;
    			CenterY = COL/2;
    			RadiusCenter = 25;
    			RadiusApart = COL/4;
    			RadiusOutter = 45;
    			
    			//Center Ball
    			g.setColor(Color.red);
    			Ellipse2D C1 = new Ellipse2D.Double();
    			C1.setFrameFromCenter(CenterX, CenterY, CenterX+RadiusCenter, CenterY-RadiusCenter);
    			gr.fill(C1);
    			//Orbiting Ball
    			g.setColor(Color.blue);
    			Ellipse2D C2 = new Ellipse2D.Double();
    			x1 = CenterX + (int)(RadiusApart*Math.cos(Action*Radian));
    			y1 = CenterY - (int)(RadiusApart*Math.sin(Action*Radian));
    			C2.setFrameFromCenter(x1,y1,x1+COL/12,y1+COL/12);
    			gr.fill(C2);
    			
    			/*if (Action == 360)
    			{
    				Action --;
    				g.setColor(Color.blue);
    				Ellipse2D C3 = new Ellipse2D.Double();
    				x1 = CenterX + (int)(RadiusApart*Math.sin(Action*Radian));
    				y1 = CenterY - (int)(RadiusApart*Math.cos(Action*Radian));
    				C3.setFrameFromCenter(x1,y1,x1+COL/12,y1+COL/12);
    			}*/
    			/*if (Action == 360)
    			{
    				g.setColor(Color.blue);
    				Ellipse2D C3 = new Ellipse2D.Double();
    				x1 = CenterX + (int)(RadiusApart*Math.cos(Action*Radian));
    				y1 = CenterY - (int)(RadiusApart*Math.sin(Action*Radian));
    				C3.setFrameFromCenter(x1,y1,x1+COL/12,y1+COL/12);
    			}*/
    		}
    		public MyPanel()
    		{
    			Action = 0;
    			JButton StartButton = new JButton("Start Running");
    			add(StartButton);
    			
    			ClickStart Start = new ClickStart();
    			StartButton.addActionListener(Start);
    		} 
    		private class ClickStart implements ActionListener
    		{
    			public void actionPerformed(ActionEvent e)
    			{
    				ActionListener WakeUp = new TimeStamp();
    				Timer T = new Timer(10,WakeUp);
    				T.start();
    			}
    			private class TimeStamp implements ActionListener
    			{
    				public void actionPerformed(ActionEvent e)
    				{
    					Action ++;
    					repaint();
    					/*if (Action == 360)
    					{
    						Action = 0;
    						T.stop();
    					}*/
    					//Action --;
    					/*if (Action >= 0);
    					Action ++;
    					if (Action <= 360);
    					Action --;
    					if (Action >= 0);
    					Action ++;
    					if (Action <= 360);
    					Action --;
    					if (Action >= 0);
    					*/
    				}
    			}
    		}
    		private int Action;
    		private int ROW, COL, Side, RunLine;
    		private Timer T;
    	}
    }
    Last edited by obdi; 07-06-2008 at 01:06 PM.

  2. #2
    tim's Avatar
    tim
    tim is offline Senior Member
    Join Date
    Dec 2007
    Posts
    435
    Rep Power
    8

    Default Solution

    Hello obdi

    Here is the solution:
    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.geom.*;
    
    public class CirclingBall extends JFrame
    {
    	public void lauchFrame()
    	{
    		int Row,Col;
    		Toolkit MyKit   = Toolkit.getDefaultToolkit();
    		Dimension XY    = MyKit.getScreenSize();
    		Row             = XY.width;
    		Col             = XY.height;
    		Image MyImage   = MyKit.getImage(":");
    		this.setIconImage(MyImage);
    		this.setTitle("Circle-Circle");
    		
    		this.setSize(7*Col/8, 7*Col/8);
    		this.setLocation(Row/32,Col/32);
    		this.setResizable(false);
    		
    		MyPanel Panel01 = new MyPanel();
    		Container ContentPanel01 = getContentPane();
    		ContentPanel01.add(Panel01);
    		
    		this.addWindowListener(new WindowAdapter()
    		{
    			public void windowClosing(WindowEvent e) 
    			{
    				System.exit(0);
    			}	
    		});
    		setVisible(true);
    	}
    	private class MyPanel extends JPanel
    	{
    		public void paint(Graphics g)
    		{
    			super.paint(g);
    			Graphics2D gr = (Graphics2D) g;
    			
    			int X,Y,Z,x1,x2,y1,y2;
    			int Row,Col,RadiusCenter,RadiusApart,RadiusOutter;
    			int CenterX, CenterY;
    			int Ax,Ay,Bx,By,Cx,Cy,Dx,Dy,RunLine;
    			
    			Dimension RowCol;
    			RowCol = getSize();
    			ROW = RowCol.width;
    			COL = RowCol.height;
    			double Radian = Math.PI/180;
    			CenterX = ROW/2;
    			CenterY = COL/2;
    			RadiusCenter = 25;
    			RadiusApart = COL/4;
    			RadiusOutter = 45;
    			
    			//Center Ball
    			g.setColor(Color.red);
    			Ellipse2D C1 = new Ellipse2D.Double();
    			C1.setFrameFromCenter(CenterX, CenterY, CenterX+RadiusCenter, CenterY-RadiusCenter);
    			gr.fill(C1);
    	                           
                                           //Orbiting Ball
    			g.setColor(Color.blue);
    			Ellipse2D C2 = new Ellipse2D.Double();
    			x1 = CenterX + (int)(RadiusApart*Math.cos(Action*Radian));
    			y1 = CenterY - (int)(RadiusApart*Math.sin(Action*Radian));
    			C2.setFrameFromCenter(x1,y1,x1+COL/12,y1+COL/12);
    			gr.fill(C2);
    		}
    		public MyPanel()
    		{
    			Action = 0;
    			JButton StartButton = new JButton("Start Running");
    			add(StartButton);
    			
    			ClickStart Start = new ClickStart();
    			StartButton.addActionListener(Start);
    		} 
    		private class ClickStart implements ActionListener
    		{
    			public void actionPerformed(ActionEvent e)
    			{
    				ActionListener WakeUp = new TimeStamp();
    				T = new Timer(10,WakeUp); [COLOR="RoyalBlue"]// You declared your timer again here. [/COLOR]
    				T.start();
    			}
    			private class TimeStamp implements ActionListener
    			{
    				[COLOR="RoyalBlue"]private int orbitCount = 3;[/COLOR] // Set to -1 if you want it to repeat forever
    				[COLOR="RoyalBlue"]private int direction = 1;[/COLOR]
    				public void actionPerformed(ActionEvent e)
    				{
    					Action += direction;
    					repaint();
    					[COLOR="RoyalBlue"]if ((Action > 360) || (Action < 0)){
    						direction *= -1;
    						if (--orbitCount == 0) T.stop();
    					}[/COLOR]
    				}
    			}
    		}
    		private int Action;
    		private int ROW, COL, Side, RunLine;
    		private Timer T;
    	}
    }
    The code in blue are the important parts. ;)

    I hope this helps you. I think you are overusing inner classes. They make your code difficult to read and use. Also, it is by convention, that attributes are declared in the beginning of a class, not at the end. I would just have created a runnable extension of Panel that does what your program does. So, I would have two classes. This simplifies the problem by using abstraction.

    Other than these points, I would say that your programming is excellent. ;)
    Eyes dwelling into the past are blind to what lies in the future. Step carefully.

  3. #3
    obdi is offline Member
    Join Date
    Jul 2008
    Location
    CA
    Posts
    15
    Rep Power
    0

    Default

    Thanks tim, though it's only 5 lines of ... well... easy code but still took me a while to figure it out ;)

    Thanks for your adivce. The way of declare attributes which i read from the book is exactlly what you said(and i like to do it that way). but in my class teacher doesn't like that... he says all those attributes that we declacred are pretty much once for all, so he wanted us to put them at the end of the code so everytime we open up the code don't have read all those "useless" parts ;o (and if he sees we declare them at start = points off)

    About the inner classes, abstraction(i guess it's a method?) i haven't got a chance to learn them yet, maybe later when i get to put one of my feet at the door step i will start to make my codes look better ;)

    A question for a total beginner, no other languages learned. How do i figure things(codes, methods e.g) out? This sounds really stupid, the class i'm taking is 2nd beginning java class i think. Teacher gives the examples, a template, which i would call a code skeleton that is this animation template i'm learning. During class, we learn the methods of drawing and the math(got no problems on that), but then, on homework projects, even we have books and internet to search for methods and syntax of how to use them, i still feel like i'm doing my own discoveries. Sometimes i'm lucky and i get it to work, others i just have no idea at all. Like the codes you helped me with, those 5 lines i just don't see i have a crank at it.

    Here is another problem i can't figure out. Animate Ball moving in a Sine cruve, and got it to work, but how can i make the ball tangent to the frame window? In my last post hardwired helped me with the TangentBall problem, and i was able to solve the problem by adding other 3 balls in. I tried to put the math method he did in the Sine project but it doesn't work, i tried to "edit" it myself it just gets worse...

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.geom.*;
    
    public class SineCurve extends JFrame
    {
    	public void lauchFrame()
    	{
    		int Row,Col;
    		Toolkit MyKit   = Toolkit.getDefaultToolkit();
    		Dimension XY    = MyKit.getScreenSize();
    		Row             = XY.width;
    		Col             = XY.height;
    		Image MyImage   = MyKit.getImage(":");
    		this.setIconImage(MyImage);
    		this.setTitle("JButton-Sun-Rise Color Test");
    		
    		this.setSize(7*Row/8, 7*Col/8);
    		this.setLocation(Row/32,Col/32);
    		this.setTitle("Sine");
    		this.setResizable(false);
    		
    		MyPanel Panel01 = new MyPanel();
    		Container ContentPanel01 = getContentPane();
    		ContentPanel01.add(Panel01);
    		
    		this.addWindowListener(new WindowAdapter()
    		{
    			public void windowClosing(WindowEvent e) 
    			{
    				System.exit(0);
    			}	
    		});
    		setVisible(true);
    	}
    	private class MyPanel extends JPanel
    	{
    		public void paint(Graphics g)
    		{
    			super.paint(g);
    			Graphics2D gr = (Graphics2D) g;
    			
    			int X, Y, x1, x2, y1, y2;
    			int Row, Col;
    			double Frequency;
    			
    			Dimension RowCol;
    			
    			RowCol = getSize();
    			Row = RowCol.width;
    			Col = RowCol.height;
    			g.setColor(Color.green);
    			g.drawRect(1,1,Row-2,Col-2);
    			g.setColor(Color.blue);
    			g.drawLine(0,Col/2,Row,Col/2);
    			g.drawLine(Row/2,0,Row/2,Col);
    			g.setColor(Color.red);
    			
    			Frequency = 1;
    			Radius = 20;
    			x1 = Action;
    			y1 = Col/2 - (int)(Col/2*Math.sin(Frequency*2.0*Math.PI/(Row)*x1));
    			
    			/*if (Action < Row/2)
    			{
    				x1 = Action+Radius;
    				y1 = Col/2+Radius - (int)(Col/2*Math.sin(Frequency*2.0*Math.PI/(Row)*x1));
    			}*/
    			
    			g.setColor(new Color(0,150,150));
    			Ellipse2D C1 = new Ellipse2D.Double();
    			C1.setFrameFromCenter(x1,y1,x1+Radius,y1+Radius);
    			gr.fill(C1);
    		}
    		public MyPanel()
    		{
    			Action = 0;
    			JButton StartButton = new JButton("Start Running");
    			add(StartButton);
    			
    			ClickStart Start = new ClickStart();
    			StartButton.addActionListener(Start);
    		} 
    		private class ClickStart implements ActionListener
    		{
    			public void actionPerformed(ActionEvent e)
    			{
    				ActionListener WakeUp = new TimeStamp();
    				Timer T = new Timer(10,WakeUp);
    				T.start();
    			}
    			private class TimeStamp implements ActionListener
    			{
    				public void actionPerformed(ActionEvent e)
    				{
    					Action ++;
    					repaint();
    					if (Action == Row-40)
    					{
    						Action = 0;
    					}
    				}
    			}
    		}
    		private int Action,Frequency,y2,Row,Radius;
    	}
    }

Similar Threads

  1. For loop problem
    By mcal in forum New To Java
    Replies: 32
    Last Post: 01-25-2008, 04:51 PM
  2. Help with if statement
    By carl in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 08:53 AM
  3. Problem with a switch statement in Java
    By baltimore in forum New To Java
    Replies: 2
    Last Post: 08-02-2007, 05:43 AM
  4. Statement or Prepared Statement ?
    By paty in forum JDBC
    Replies: 3
    Last Post: 08-01-2007, 05:45 PM
  5. eternal loop problem
    By sandor in forum New To Java
    Replies: 3
    Last Post: 04-29-2007, 04:55 PM

Posting Permissions

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