Java Program - Track Position Of Imaginary Ball As It Bounces Around Imaginary Box

Oct 3, 2014

I am working on a program that asks me to track the position of an imaginary ball as it bounces around an imaginary box. The user gives me input for the starting position of the ball (x,y), the bottom-left boundaries of the box (xl, yl), the top right boundaries (xr, yr) and the angle. The program then needs to find where the ball will hit the box, and then print that location and find the new angle for the next point. My program currently works in most cases if the angle is less than pi.

Here is my code

public class BouncyBall {
public static void main(String args[]){
if (args.length != 8) {
System.out.println("Error: usage is X, Y, Angle, Xl, Yl, Xr, Yr,N");
System.exit(1);
}
double x,y,xl,yl,xr,yr,angle;
int n;
x=Double.parseDouble(args[0]);
y=Double.parseDouble(args[1]);

[Code] .....

View Replies


ADVERTISEMENT

Java Math - Find Imaginary Number

Nov 2, 2014

I am doing this math for my assignment where you have to find the imaginary number.

double dem = (b*b-4*a*c)*-1;
double dem2 = Math.sqrt(dem);
realPart = (-b/(2*a));
imagine = (dem2)/(2*a);
imagineneg = (dem2)/(2*a);

So basically I am trying to get it that the number inside the square root isnt negative. So lets say I enter a: 1, b: -1, c: 4.25. When you do the discriminant it would be -16 but I want that to be positive so I multiple it by -1 (thats the dem part). Then I sqaure root the whatever the answer is and then go to the imagine and imagineneg part where you find the imaginary number.

My problem is that instead of it being -16*-1 to get 16, some how I get 15.

View Replies View Related

Coordinates Are Inside Of A Right Triangle On Imaginary Coordinate Plane

May 24, 2014

I'm having a random x and y coordinate generate and I need to check if the coordinates are inside of a right triangle on this imaginary coordinate plane.

View Replies View Related

Priority Queue In Java Which Keeps Track Of Each Node Position

May 4, 2014

I'm working on a lab for my class. I need to create a Priority Queue in Java using an array list. The kicker is that each node has to have a "handle" which is just an object which contains the index of of the node that it's associated with. I know that sounds weird but we have to implement it this way. Anyway, my priority queue seem to work fine except the handle values apparently aren't updating correctly because I fail the handle test. Also, I run into problems with handles only after extractMin() is called, so I figured the problem would be somewhere in that method but I've been through it a million times and it looks good to me.

Here is my Priority Queue Class:

package lab3;
 import java.util.ArrayList;
 /**
* A priority queue class supporting operations needed for
* Dijkstra's algorithm.
*/
class PriorityQueue<T> {
final static int INF = 1000000000;
ArrayList<PQNode<T>> queue;
 
[Code] ....

I can post the tests too but I don't know if that would do much good. Just know that the queue works as is should, but the handles don't point to the right nodes after extractMin() is utilized.

View Replies View Related

Keeping Track Of Player Position In Monopoly Board Game

Jun 28, 2014

I am trying to design a monopoly board game with a class and a main program. I can not make the method to keep track of the player's position after every roll. After every roll it prints "Previous position: 0".The player should also not go over 14th spot because the board is just 15 including 0. That is what I have (just the particular method and the part from the main program which call it).

public int getpospl1()
{
System.out.println( getplayer1name() + " Rolls "
+ "Dice 1: " + getrolld1() + "" + "Dice 2: " + getrolld2() + "" );
int spot1 = 14; //The end spot
int start = 0;
int previousPosition = start;

[Code] .....

View Replies View Related

Java Program That Keeps Track Of People Names

Jan 13, 2015

I need to make a program that keeps track of peoples names, allows you to add a note to each name and preferably would reorganize whatever you put in by date or by spelling. (I suppose the phonebook application in cell phones is a good match, a supermarket's list of foods and prices would work as well).

View Replies View Related

Ball Object Which Implements Runnable Interface And Traces Various Positions Of A Ball

Feb 11, 2014

a) I have a Ball Object which implements the Runnable interface and traces the various positions of a ball.

b) I then have a Ball_Bounce JPanel inside a JFrame which creates two instances of the Ball object and then paints them to the JPanel.

As per my understanding, when the main() program in Ball_Bounce.java is started, there a total of three threads running in this program, one for each ball and one for the main(). What I cannot understand is whenever the balls collide, I end up getting the "Collision" message twice even though the collision is checked only in the main() thread.

[#]public class Ball implements Runnable
{
private boolean xUp, yUp, xUp1, yUp1;
private int x, y, xDx, yDy;
private final int MAX_X = 500, MAX_Y = 500;
private boolean flag = true;
private static Thread ball;
 
[code]...

View Replies View Related

Create A Program That Keeps Track Of Information Input By User

Sep 17, 2014

I am new to Java an have to Create a program that keeps track of the following information input by the user: First Name, Last Name, Phone Number, and Age. Now - let's store this in a multidimensional array that will hold 10 of these contacts. So our multidimensional array will need to be 10 rows and 4 columns.You should be able to add and remove contacts in the array.

View Replies View Related

Program That Will Allow To Enter Information From Form To Keep Track Of Attorney Who Signed Up

Jan 13, 2015

I work for my local probate court, and one service we offer is email reminders. They fill out a form, it's entered in the system, and they will be notified when something is due. I've been allowed to create a program that will allow me to enter the information from the form to keep track of the attorney's who have signed up.

import java.sql.*;
import java.util.*;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;

public class Attempt1 {
public static void setConnection() throws SQLException {
Connection conn = null;
Statement state = null;

[Code] ....

The issue I have is the method setQuery. It displays a table with the data entered. Where it currently sits at line 21, it does not display the table, nor does it seem to terminate the program. The goal was to update the table with a few entries, and then display the Jtable. When placed above the setInsert method, it displays just fine, and will reflect the new entries added the next time you start it. I'm still learning Java outside of a classroom environment on my own, so I can only imagine that this is riddled with poor coding, but I just can't seem to figure out why it doesn't display the table after the setInsert method is called. In it's current state, this program is just meant to test functions of SQLite with Java. From there it will be created with a GUI.

View Replies View Related

Dynamically Add Objects To Array In Any Position In Java

Feb 6, 2015

I have an assignment in which I have to design a method where I add an object at any given position in an array and shift the elements already in the array to make room. For example I have a collection class which holds trading card objects. So I wish to add a new trading card to this collection at a specified index or position with out deleting the current object already stored in the array . All this has to be done without the use of array lists, vectors or any abstract data types besides arrays . My question is how do I accomplish this . Say I wish to add a new trading card in position 4 the new card is added to my array and the card currently in position 4 gets moved to position 5 and the card in position 5 gets moved to 6 etc. The maximum amount of cards my collection can hold is 100. How would I add my trading card object to the specified position without overwriting what is currently there and shifting all other elements?Here is my current code.

public class CardCollection {
public BaseballCard [] collection;
final int MAX_CARDS = 100;
public CardCollection() {
collection = new BaseballCard[MAX_CARDS];
}
public CardCollection(BaseballCard[]c)

[code]...

position refers to the position in the CardCollection and not the position inside the array.

View Replies View Related

Buttons To Add And Remove 1 Ball

Apr 29, 2015

1. BALL

import javax.swing.Timer;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Ball extends JPanel {
private int delay = 10;
private Timer timer = new Timer(delay, new TimerListener());
private int x = 0; private int y = 0;

[Code] ...

View Replies View Related

Magic 8 Ball Code

Dec 5, 2014

I am writing a three part program that essentially works like a magic 8 ball. I have the switch/case part which gives the answers from a random number generator, then I also have a graphics file to create the ball as a visual. Lastly I have the frame to display the ball. The problem I am having is that, I am trying to make the program so that when you type in a question and press return the ball will show the answer and then you can type another question and press return and the ball will show a different answer. To quit you would simply type "quit". Right now however the ball is not showing up in my frame until I type quit, then to ask another question I have to end the run and start over again. Here is my program so far:

Case/Switch:
public class MagicEightBall
{
private int number;
private String answer;

[code]....

View Replies View Related

Ball Stopping At The Edge Of Maze

Mar 7, 2015

I am doing an assignment which involves creating a maze and a ball that needs to go through the maze to an end tile then stop the scenario. The maze is made up of 208 buttons in a Jbutton Grid layout. I am replacing certain buttons with a brown tile icon to make up the maze. The ball needs to run along those brown tiles as its path and not be able to move off the brown tile icon images.

I have built the maze and have got quite far with this but now I am stumped on the concept of keeping the ball within the boundary edges and on the brown tiles. I have been told to use if statements, but not had the process explained to me in a way I can understand.

View Replies View Related

Ball Class For Pong Won't Add To JPanel

Jul 28, 2014

I am trying to make a game of pong and am having trouble adding my ball class to a JPanel. Here is what I have so far.

package pong;
import java.awt.*;
public class Ball{
private int xPos,yPos;
public int dx = 5, dy = -5;
public Ball()

[code].....

I want to be able to add this to a JPanel but it is not allowing me to add the ball. Here is my JPanel

package pong;
import java.awt.*;
import javax.swing.JPanel;
public class BallTestPanel

[Code] ....

It reads panel.add(ball); as an error. I says that I need to change the type ball to component.

View Replies View Related

How To Create Multiple Threads Of Ball

Apr 18, 2014

Actually, i want to create multiple ball using multithreading.I have created a class 'CreateBall' and this class is making ball and second class 'Balls' is a Panel where the ball is display.But only one ball is being displayed.

Here is my code.......

CreateBall.java
import java.awt.*;
public class CreateBall implements Runnable
{
int px,py;
int w=20,h=20;
Graphics g;
public CreateBall(int px,int py)

[Code] .....

View Replies View Related

Bouncing Ball Around The Screen - It Keeps Disappearing

Oct 23, 2014

I'm trying to do is get this ball to bounce around the screen and ive gotten so far but not sure how to proceed. The ball keeps disappearing.

//bouncing ball

float x; //ball x position
float dx = 3; //ball x direction is right, step 5
float y; //ball y position
float dy = 3;
void setup () //runs once at start

[Code] ....

View Replies View Related

How To Make Curve Motion With Ball Object

Jun 25, 2013

I want to make a simply game,actualy not a game but I want to moving object curve just like angry bird. Where I can move an object with curve motion. But I don't know how. I just can make it move straight(left,right,down,up).

View Replies View Related

Swing/AWT/SWT :: Making Ball Bounce Around A Screen

Aug 12, 2014

I've got to make a ball bounce around a screen.... From what I can gather they seem to hold back useful java code.

anyway, i've changed the code over and over, and think this is the most succinct way i can manage it. but i have a problem. where i've marked the code (*****) and the following line are conditional on each other. so one won't provide the correct answer without the other line executing first, and vice versa (ignore the code after, i haven't looked at where that should be yet ). Anyway I don't really want to rip the code apart, again, because i'm pretty sure it's close.

View Replies View Related

How To Make Pong Ball Slowly Increase Speed

Apr 11, 2013

I was making a pong game, part copied code off the internet, part my own code, part my friends code. It works well enough, but the ball starts at one speed, and stays the same. I would like to know how to make the ball slowly increase.

It is a java applet, which shouldn't casue any harm right?

My code:

Pong Applet
 
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
 public class Pong extends Applet implements MouseMotionListener, KeyListener {
 
[Code] .....

View Replies View Related

Random Color Ball And Move Bottom Bar Vector?

Feb 20, 2014

I created this simple vector java program where ball falling and when collide with the bar below, the ball stops.

I need to make the ball falling randomly from different direction and also need to make the bar move left right using keyboard arrow.

Code:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
 //using timer to increase the counter

[code].....

View Replies View Related

Ping Pong Game - Ball Drop And Catch

Feb 26, 2014

I have created this ping pong game but having problem with ball and paddle collusion. After first collusion it gives score 1 but later it never touch the paddle.

Code:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.util.Random.*;
import javax.swing.ImageIcon;
 public class Ball extends JFrame implements ActionListener, KeyListener{
int xSpeed=0;

[Code] ....

I think something is wrong here:

public void actionPerformed(ActionEvent e){
y=y+xSpeed;
repaint();
if(y> this.getWidth()){
y=25;
score -=1;
}
if(blockRect.intersects(ballRect)){
y=40;
score +=1;
x =200;
y = 300;
}

View Replies View Related

Add Color To Ball In Pong - Speed Up When Contact Made With Paddle

Apr 23, 2014

I wrote a code from our text book for Pong. Im trying to figure out how to fill the ball with color. Im also wondering how to speed the ball up once there is contact with the paddle. The initial speed is ok but once contact is made with the paddle, it slows down. Makes it too easy to play. Here is my code so far. As far as the game goes, it works and is good. Just wanted to add a couple customizations.

Java Code:

package pong;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.event.ActionEvent;

[Code] .....

View Replies View Related

Counting Scoreboard - How Many Times A Ball Hits Moving Platform In Pong

May 9, 2014

I need to make a scoreboard that can count how many times a ball hits a moving platform in pong and have no clue how to approach this.

View Replies View Related

How To Track Player Positioning

Nov 25, 2014

So I have a player an enemy and enemy bounds the enemy can go. I need to have the enemy track my position and when I go into the position (I am using a collision method for this) the enemy comes over at a speed of 1. My problem is the enemy jumps to me and then follows at speed of 2 (2 is the player speed). The code is wrong this is why it is jumping and I have my other problems. so my question is what is a good solution for this? I am trying to make a method to track playerposition() so what I am thinking I could do is find x, y of player then store those into an array and return the array to Enemy so he tracks.

player.java
public class Player{
int x = 100; // Location of player
int y = 200; // location of player
int xa = 0; // Representation of where the player goes
int ya = 0; // Representation of where the player goes
private int speed = 2;

[Code] .....

Please note this is not the entire code I have cut some things out that did not need to be there. Also, the code is just to get an idea of what I was thinking of doing. The ideas that are came up with are not meant to be a reflection of what I already have but, what I could add or replace.

View Replies View Related

How To Keep Track Of Percent Full

May 20, 2014

I'm trying to make a canteen class that holds water. It holds two quarts of water and two quarts is 100%.

public double maxVolume;
public int percentFull;

Right now the Canteen is empty.

public void Canteen()
{
percentFull=0;
maxVolume=0;
}

And right now I'm trying to make a constructor that specifies the amount of liquid the canteen can hold and specifies the percent full.

public Canteen(double maxVolume, int percentFull)
{
}

Should I make this second Canteen an integer, a double, or something else? Also, how do I make sure the Canteen never reaches higher than 100%. I'm also curious how I can keep maxVolume and percentFull connected so the % of water is consistent with the number of quarts(2) the Canteen can hold.

View Replies View Related

Who Keeps Track Of Which JComponent Has Focus

Aug 31, 2014

I have two JFrames: frame1 and frame2. frame2 currently has focus, and I want to determine which component of frame1 would have focus if I were to switch focus to frame1, hopefully without actually temporarily changing focus.

Is it the KeyboardFocusManager who keeps track of which element in each frame has focus? Or does each container itself keep track? How does Java figure out which element in a frame gets focus when I switch to that window?

View Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved