Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-05-2008, 01:26 PM
Member
 
Join Date: Jul 2008
Location: CA
Posts: 15
obdi is on a distinguished road
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 12:42 PM.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-05-2008, 03:24 PM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: SW MO, USA
Posts: 1,489
Norm is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 07-06-2008, 12:45 AM
Member
 
Join Date: Jul 2008
Location: CA
Posts: 15
obdi is on a distinguished road
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 12:41 PM.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 07-06-2008, 12:52 AM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: SW MO, USA
Posts: 1,489
Norm is on a distinguished road
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-06-2008 at 12:55 AM.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 07-06-2008, 12:58 AM
Member
 
Join Date: Jul 2008
Location: CA
Posts: 15
obdi is on a distinguished road
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 01:54 AM.
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 07-06-2008, 01:11 AM
Member
 
Join Date: Jul 2008
Location: CA
Posts: 15
obdi is on a distinguished road
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 01:26 AM.
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 07-06-2008, 01:47 AM
tim's Avatar
tim tim is offline
Senior Member
 
Join Date: Dec 2007
Location: South Africa
Posts: 334
tim is on a distinguished road
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:
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
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
__________________
If your ship has not come in yet then build a lighthouse.
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 07-06-2008, 01:48 AM
Member
 
Join Date: Jul 2008
Location: CA
Posts: 15
obdi is on a distinguished road
Thank you, I got it
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 07-06-2008, 01:53 AM
tim's Avatar
tim tim is offline
Senior Member
 
Join Date: Dec 2007
Location: South Africa
Posts: 334
tim is on a distinguished road
Quote:
Originally Posted by obdi
Thank you, I got it
Cool. 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 .
__________________
If your ship has not come in yet then build a lighthouse.

Last edited by tim : 07-06-2008 at 01:56 AM.
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 07-06-2008, 02:01 AM
Member
 
Join Date: Jul 2008
Location: CA
Posts: 15
obdi is on a distinguished road
I tried the new math way to do the (#3 (permalink)) problem i posted, after the math, i did
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,
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 02:05 AM.
Bookmark Post in Technorati
Reply With Quote
  #11 (permalink)  
Old 07-06-2008, 07:00 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,189
hardwired is on a distinguished road
For your AutoBall question:
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(); } }
Bookmark Post in Technorati
Reply With Quote
  #12 (permalink)  
Old 07-06-2008, 07:53 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,189
hardwired is on a distinguished road
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:
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(); } }
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(); } }
Bookmark Post in Technorati
Reply With Quote
  #13 (permalink)  
Old 07-06-2008, 07:59 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,189
hardwired is on a distinguished road
For the ball to follow an oval track instead of a circle replace the paintComponent code in CircleBall with:
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);
Bookmark Post in Technorati
Reply With Quote
  #14 (permalink)  
Old 07-06-2008, 10:11 AM
Member
 
Join Date: Jul 2008
Location: CA
Posts: 15
obdi is on a distinguished road
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!
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


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

vB 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
Java Syntax for making 2 divs same height dynamically. jatrant New To Java 4 06-25-2008 09:09 PM
total beginner needs little help asambasamba New To Java 1 06-18-2008 06:33 PM
Cannot get passed these syntax errors MrKP New To Java 1 05-12-2008 08:05 AM
beginner to Java notwist New To Java 15 04-18-2008 10:41 AM
syntax error gabriel New To Java 3 08-03-2007 04:26 PM


All times are GMT +3. The time now is 09:43 PM.


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