Results 1 to 14 of 14
- 07-05-2008, 12:26 PM #1
Member
- Join Date
- Jul 2008
- Location
- CA
- Posts
- 15
- Rep Power
- 0
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.
- 07-05-2008, 02:24 PM #2
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.
- 07-05-2008, 11:45 PM #3
Member
- Join Date
- Jul 2008
- Location
- CA
- Posts
- 15
- Rep Power
- 0
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.
- 07-05-2008, 11:52 PM #4
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.
- 07-05-2008, 11:58 PM #5
Member
- Join Date
- Jul 2008
- Location
- CA
- Posts
- 15
- Rep Power
- 0
this is the Pendulum, SwingBall.java, how to make it swing back and forth? or should i ask how to loop it?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 PendulumLast edited by obdi; 07-06-2008 at 12:54 AM.
- 07-06-2008, 12:11 AM #6
Member
- Join Date
- Jul 2008
- Location
- CA
- Posts
- 15
- Rep Power
- 0
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 youLast edited by obdi; 07-06-2008 at 12:26 AM.
- 07-06-2008, 12:47 AM #7
Math, code tags and primitive types
Hello obdi
To use code tags:
- Go to advanced mode if you are not using it
- Select the code you want to wrap with code tags
- Click the "sharp" button or "#" in the tool bar
- You can type in the tags manually if you want
To do power operations in Java, we use the Math class. In the Math class is a static method pow:
Originally Posted by obdi
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 getJava Code:public double pow(double base, double power)
The (double) cast is not really necessary, but I like being consistent with method parameter types.Java Code:int ROW = 3, COL = 4, RunDistance = (int) Math.pow( (double)(ROW*ROW+COL*COL), 0.5d );
Good luck ;)Eyes dwelling into the past are blind to what lies in the future. Step carefully.
- 07-06-2008, 12:48 AM #8
Member
- Join Date
- Jul 2008
- Location
- CA
- Posts
- 15
- Rep Power
- 0
Thank you, I got it ;)
- 07-06-2008, 12:53 AM #9
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. ;)
Originally Posted by obdi
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.
- 07-06-2008, 01:01 AM #10
Member
- Join Date
- Jul 2008
- Location
- CA
- Posts
- 15
- Rep Power
- 0
I tried the new math way to do the (#3 (permalink)) problem i posted, after the math, i did
and the 4 Balls are not moving,Java Code:private class TimeStamp implements ActionListener { public void actionPerformed(ActionEvent e) { Action += 2; repaint(); if (Action >= RunDistance) { Action = 0; } } }
is what i did in paint(). help.Java Code:RunDistance = (int) Math.pow( (double)(ROW*ROW+COL*COL), 0.5d );
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.
- 07-06-2008, 06:00 AM #11
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(); } }
- 07-06-2008, 06:53 AM #12
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(); } }
- 07-06-2008, 06:59 AM #13
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);
- 07-06-2008, 09:11 AM #14
Member
- Join Date
- Jul 2008
- Location
- CA
- Posts
- 15
- Rep Power
- 0
Similar Threads
-
Java Syntax for making 2 divs same height dynamically.
By jatrant in forum New To JavaReplies: 4Last Post: 06-25-2008, 08:09 PM -
total beginner needs little help
By asambasamba in forum New To JavaReplies: 1Last Post: 06-18-2008, 05:33 PM -
Cannot get passed these syntax errors
By MrKP in forum New To JavaReplies: 1Last Post: 05-12-2008, 07:05 AM -
beginner to Java
By notwist in forum New To JavaReplies: 15Last Post: 04-18-2008, 09:41 AM -
syntax error
By gabriel in forum New To JavaReplies: 3Last Post: 08-03-2007, 03:26 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks