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

    Question 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 at #3 (permalink)
    Last edited by obdi; 07-06-2008 at 11:42 AM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,321
    Rep Power
    25

    Default

    Is this a java programing problem?
    Could you post your java code so we could see where you are?
    Also copy and post the error messages you get allong with your questions on why you're getting them.

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

    Default

    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();
    				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 11:41 AM.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,321
    Rep Power
    25

    Default

    What does it mean to "make it tangent when the 4 balls run to the corners of the frame"?
    What is the "it"?
    What is the visual object that "it" is tangent to?
    Do you have an algorithm that defines how to do this? If so, post it with your thoughts on how to code it and we'll help you get your code working.

    What is the Math-syntax problem you are having?

    When posting code, please use the CODE tags to preserve the indents.
    Last edited by Norm; 07-05-2008 at 11:55 PM.

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

    Default

    Java Code:
    private class TimeStamp implements ActionListener
    			{
    				public void actionPerformed(ActionEvent e)
    				{
    					Action += 1;
    					repaint();
    					if(Action > 59)
    					{
    						Action --;
    					}
    				}
    			} //loop
    R = 3*Col/4 - Col/15;
    			Angle = Math.PI/3;
    			
    			Ellipse2D C1 = new Ellipse2D.Double();
    			CenterX = Row/2 - (int)(R*Math.sin(Angle - Action*Math.PI/90));
    			CenterY = (int)(R*Math.cos(Angle - Action*Math.PI/90));
    			RadiusX = CenterX + Col/15;
    			RadiusY = CenterY + Col/15; //math for the Pendulum
    this is the Pendulum, SwingBall.java, how to make it swing back and forth? or should i ask how to loop it?
    Last edited by obdi; 07-06-2008 at 12:54 AM.

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

    Default

    Thanks for the advice, I don't know how to use CODE tags, can you post me an example please?

    The question I typed for "it" was i wanted to make the 4 balls to be perfectly tangent when they move from TopLeft to BottomRight, BotteomRight to TopLeft, TopRight to BottomLeft, BottomLeft to TopRight. I coded them that they could move, but when they moved to the corners they are not tangent to the frame window. ( setResizable(true); )


    What is the Math-syntax problem you are having?
    if i coded RunDistance = (int)((ROW*ROW+COL*COL)^(1/2)); is this the right syntax? thank you
    Last edited by obdi; 07-06-2008 at 12:26 AM.

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

    Default Math, code tags and primitive types

    Hello obdi

    To use code tags:
    1. Go to advanced mode if you are not using it
    2. Select the code you want to wrap with code tags
    3. Click the "sharp" button or "#" in the tool bar
    4. You can type in the tags manually if you want


    Quote Originally Posted by obdi
    if i coded RunDistance = (int)((ROW*ROW+COL*COL)^(1/2)); is this the right syntax? thank you
    To do power operations in Java, we use the Math class. In the Math class is a static method pow:
    Java Code:
    public double pow(double base, double power)
    or something similar. Also 1/2 in Java means that you take the integer one and the integer two and calculate how many times the two divides into one, which is zero. So, to get the half, you can specify the primitive type with a non-case sensitive letter d, like 1d / 2d. Or just use 0.5d. The d will specify that you want the primitive type to be a double precision floating point number. If we put this together we get
    Java Code:
    int ROW = 3,
        COL = 4,
        RunDistance = (int) Math.pow( (double)(ROW*ROW+COL*COL), 0.5d );
    The (double) cast is not really necessary, but I like being consistent with method parameter types.

    Good luck ;)
    Eyes dwelling into the past are blind to what lies in the future. Step carefully.

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

    Default

    Thank you, I got it ;)

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

    Default

    Quote Originally Posted by obdi
    Thank you, I got it ;)
    Cool. :D If you solved the posed problem, then please mark the thread solved. Look in the "thread tools" menu. Otherwise, you can continue with your questions. ;)

    Oh and, warm welcome to Java Forums, obdi :p.
    Last edited by tim; 07-06-2008 at 12:56 AM.
    Eyes dwelling into the past are blind to what lies in the future. Step carefully.

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

    Default

    I tried the new math way to do the (#3 (permalink)) problem i posted, after the math, i did
    Java Code:
    private class TimeStamp implements ActionListener
    			{
    				public void actionPerformed(ActionEvent e)
    				{
    					Action += 2;
    					repaint();
    					if (Action >= RunDistance)
    					{
    						Action = 0;
    					}
    				}
    			}
    and the 4 Balls are not moving,
    Java Code:
    RunDistance = (int) Math.pow( (double)(ROW*ROW+COL*COL), 0.5d );
    is what i did in paint(). help.

    i'm trying to make 4 balls move on the hypo. of the trangle in the rectangle(this is the way i'm thinking), and make them tangent to the frame window when they get to the coners.
    Last edited by obdi; 07-06-2008 at 01:05 AM.

  11. #11
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    For your AutoBall question:
    Java Code:
    import java.awt.*;
    import java.awt.geom.*;
    import javax.swing.*;
    
    public class TangentBall extends JPanel implements Runnable {
        Ellipse2D.Double ball;
        double dx = 2.0;
    
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D)g;
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                                RenderingHints.VALUE_ANTIALIAS_ON);
            int w = getWidth();
            int h = getHeight();
            if(ball == null) {
                int r = 25;
                double x = 0;
                double y = h-2*r;
                ball = new Ellipse2D.Double(x, y, 2*r, 2*r);
            }
            g2.setPaint(Color.blue);
            g2.draw(new Line2D.Double(0,h,w,0)); // sw - ne
            g2.setPaint(Color.red);
            g2.fill(ball);
        }
    
        public void run() {
            boolean keepGoing = true;
            do {
                try {
                    Thread.sleep(40);
                } catch(InterruptedException e) {
                    break;
                }
                keepGoing = moveBall(); 
                repaint();
            } while(keepGoing);
        }
    
        private boolean moveBall() {
            int w = getWidth();
            int h = getHeight();
            double d = ball.width;
            double theta = Math.atan2(-h, w);
            double x = ball.getCenterX() + dx*Math.cos(theta);
            double y = ball.getCenterY() + dx*Math.sin(theta);
            if(x + d/2 < w && y - d/2 > 0) {
                ball.setFrameFromCenter(x,y,x+d/2,y+d/2);
                return true;
            }
            return false;
        }
    
        private void start() {
            Thread thread = new Thread(this);
            thread.setPriority(Thread.NORM_PRIORITY);
            thread.start();
        }
    
        public static void main(String[] args) {
            TangentBall test = new TangentBall();
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(test);
            f.setSize(400,400);
            f.setLocation(200,200);
            f.setVisible(true);
            test.start();
        }
    }

  12. #12
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    2, In SineCurve.java, animates a ball by a sine shaped graph, 1 period, how can I make the ball tangent to the frame window?
    Same technique demonstrated in TangentBall: check for the ball touching the edge of the component and, when it does, stop the timer.
    For the pendulum and Circle questions:
    Java Code:
    import java.awt.*;
    import java.awt.geom.*;
    import javax.swing.*;
    
    public class Pendulum extends JPanel implements Runnable {
        Ellipse2D.Double ball = new Ellipse2D.Double();
        double theta = Math.PI/2;
        double thetaLimit = Math.PI/3;
        double thetaInc = Math.toRadians(2);
    
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D)g;
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                                RenderingHints.VALUE_ANTIALIAS_ON);
            int w = getWidth();
            int h = getHeight();
            double x0 = w/2.0;
            double y0 = 0;
            int r = 25;
            int dist = Math.min(w,h)/2;
            double x = x0 + dist*Math.cos(theta);
            double y = y0 + dist*Math.sin(theta);
            ball.setFrameFromCenter(x, y, x+r, y+r);
            g2.setPaint(Color.blue);
            g2.draw(new Line2D.Double(x0, y0, x, y));
            g2.setPaint(Color.red);
            g2.fill(ball);
         }
    
        public void run() {
            do {
                try {
                    Thread.sleep(75);
                } catch(InterruptedException e) {
                    break;
                }
                if(Math.abs(theta + thetaInc) > Math.PI/2 + thetaLimit ||
                   Math.abs(theta + thetaInc) < Math.PI/2 - thetaLimit)
                    thetaInc *= -1;
                theta += thetaInc;
                repaint();
            } while(isVisible());
        }
    
        private void start() {
            Thread thread = new Thread(this);
            thread.setPriority(Thread.NORM_PRIORITY);
            thread.start();
        }
    
        public static void main(String[] args) {
            Pendulum test = new Pendulum();
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(test);
            f.setSize(400,400);
            f.setLocation(200,200);
            f.setVisible(true);
            test.start();
        }
    }
    Java Code:
    import java.awt.*;
    import java.awt.geom.*;
    import javax.swing.*;
    
    public class CircleBall extends JPanel implements Runnable {
        Ellipse2D.Double ball = new Ellipse2D.Double();
        double theta = 0;
    
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D)g;
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                                RenderingHints.VALUE_ANTIALIAS_ON);
            int w = getWidth();
            int h = getHeight();
            double cx = w/2.0;
            double cy = h/2.0;
            int r = 25;
            int dist = Math.min(w,h)/3;
            double x = cx + dist*Math.cos(theta);
            double y = cy + dist*Math.sin(theta);
            ball.setFrameFromCenter(x, y, x+r, y+r);
            g2.setPaint(Color.blue);
            g2.draw(new Line2D.Double(cx, cy, x, y));
            g2.setPaint(Color.red);
            g2.fill(ball);
        }
    
        public void run() {
            do {
                try {
                    Thread.sleep(75);
                } catch(InterruptedException e) {
                    break;
                }
                theta += Math.toRadians(2);
                repaint();
            } while(isVisible());
        }
    
        private void start() {
            Thread thread = new Thread(this);
            thread.setPriority(Thread.NORM_PRIORITY);
            thread.start();
        }
    
        public static void main(String[] args) {
            CircleBall test = new CircleBall();
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(test);
            f.setSize(400,400);
            f.setLocation(200,200);
            f.setVisible(true);
            test.start();
        }
    }

  13. #13
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    For the ball to follow an oval track instead of a circle replace the paintComponent code in CircleBall with:
    Java Code:
            int w = getWidth();
            int h = getHeight();
            double cx = w/2.0;
            double cy = h/2.0;
            int r = 25;
            int vDist = Math.min(w,h)/3;
            int hDist = Math.min(w,h)/4;
            double x = cx + hDist*Math.cos(theta);
            double y = cy + vDist*Math.sin(theta);
            ball.setFrameFromCenter(x, y, x+r, y+r);
            g2.setPaint(Color.blue);
            g2.draw(new Ellipse2D.Double(cx-hDist, cy-vDist, 2*hDist, 2*vDist));
            g2.setPaint(Color.red);
            g2.fill(ball);

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

    Default

    Hi hardwired, Thank you very much, all problem solved!! Though i still need some time to understand the codes and syntax that i haven't learned like try{}catch e.g. but have the examples you gave me here will help me a lot. Thanks again!

Similar Threads

  1. Replies: 4
    Last Post: 06-25-2008, 08:09 PM
  2. total beginner needs little help
    By asambasamba in forum New To Java
    Replies: 1
    Last Post: 06-18-2008, 05:33 PM
  3. Cannot get passed these syntax errors
    By MrKP in forum New To Java
    Replies: 1
    Last Post: 05-12-2008, 07:05 AM
  4. beginner to Java
    By notwist in forum New To Java
    Replies: 15
    Last Post: 04-18-2008, 09:41 AM
  5. syntax error
    By gabriel in forum New To Java
    Replies: 3
    Last Post: 08-03-2007, 03:26 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
  •