Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-06-2008, 11:44 AM
Member
 
Join Date: Jul 2008
Location: CA
Posts: 15
Rep Power: 0
obdi is on a distinguished road
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!


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 12:06 PM.
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 07-06-2008, 12:20 PM
tim's Avatar
tim tim is offline
Senior Member
 
Join Date: Dec 2007
Location: South Africa
Posts: 337
Rep Power: 2
tim is on a distinguished road
Default Solution
Hello obdi

Here is the solution:
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); // You declared your timer again here. 
				T.start();
			}
			private class TimeStamp implements ActionListener
			{
				private int orbitCount = 3; // Set to -1 if you want it to repeat forever
				private int direction = 1;
				public void actionPerformed(ActionEvent e)
				{
					Action += direction;
					repaint();
					if ((Action > 360) || (Action < 0)){
						direction *= -1;
						if (--orbitCount == 0) T.stop();
					}
				}
			}
		}
		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.
__________________
Grudges are heavy. Why hold them when you can let them go?
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 07-07-2008, 01:41 AM
Member
 
Join Date: Jul 2008
Location: CA
Posts: 15
Rep Power: 0
obdi is on a distinguished road
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...

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;
	}
}
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
For loop problem mcal New To Java 32 01-25-2008 03:51 PM
Help with if statement carl New To Java 1 08-06-2007 07:53 AM
Problem with a switch statement in Java baltimore New To Java 2 08-02-2007 04:43 AM
Statement or Prepared Statement ? paty Database 3 08-01-2007 04:45 PM
eternal loop problem sandor New To Java 3 04-29-2007 03:55 PM


All times are GMT +2. The time now is 11:17 PM.



VBulletin, Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org