Referencing indexed Arrays [duplicate] - java

This question already exists:
Closed 10 years ago.
Possible Duplicate:
Checking against indexed arrays
So i have my code, every time the mouse is pressed it draws a new particle at mouseX and mouseY and stores the position in an array so it can update until told to stop when it reaches the bottom of the screen. What i would like to do is check the current position against that of an already 'made' particle and have it stop sooner if it has the same coordinates, to give a stacking effect, can someone please help me?
import java.awt.Point;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;
public class Control extends BasicGameState {
public static final int ID = 1;
public Methods m = new Methods();
public Point[] point = new Point[(800 * 600)];
int pressedX;
int pressedY;
int num = 0;
String Build = "1.1";
public void init(GameContainer container, StateBasedGame game) throws SlickException{
}
public void render(GameContainer container, StateBasedGame game, Graphics g) throws SlickException {
for (int index = 0; index < point.length; index++) {
Point p = point[index];
if (p != null) {
if (p.y >= 598) {
m.drawParticle(point[index].x,(point[index].y));
} else {
m.drawParticle(p.x, p.y);
p.y++;
}
}
}
g.drawString("Particle Test", 680, 0);
g.drawString("Build: " + Build, 680, 15);
g.drawString("Pixels: " + num, 10, 25);
}
public void update(GameContainer container, StateBasedGame game, int delta) {
}
public void mousePressed(int button, int x, int y) {
pressedX = x;
pressedY = y;
num = num + 1;
point[num] = new Point(pressedX, pressedY);
}
public int getID() {
return ID;
}
}

You can use the coordinates as a hash and store the points in a HashSet, just overload the hashing function of Point with something like x*y

You can write a new method that checks to see if the point is in the array at an index of less than "Index".
public boolean isInArray(int index)
{
for (int index2 = 0; index2 < index; index2++)
{
if( (point[index2].x == point[index].x) &&
(point[index2].y == point[index].y) )
{
return true; //point(index) is previously in array
}
}
return false; //point(index) is NOT previously in array
}
Then in your for..next loop, you'll need to use the break statement
if (isInArray(index))
{
//the break statement causes the program to exit the current loop
break;
}
However, you might be better served by testing it in your MousePressed method to avoid adding the point to the array in the first place if it's a duplicate.
Good luck!

Related

Moving Black and White Balls

I'm kind of new to Java and OO programming, here is the code of moving black and white balls problem. First let me explain the program that I want in the output: there are n balls(for example 6 balls) on the window, one black and one white, in each move we only are allowed to move just one ball and this movement should be shown on the screen, and at the end all the white balls should be on one side and all the black balls should be on the other side. Here is an example of six balls:
I have written the program and it seems working good and no flaws in the algorithm, but my problem is that I can't show animation of the movement of the balls, in each movement one ball should swap its place with its neighbor ball, but all I get is the final arrangements of the balls. Please someone help me with the animation part. I would be really thankful for that.
code:
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Ellipse2D;
import javax.swing.*;
public class DrawPanel extends JPanel implements ActionListener
{
Timer myTimer = new Timer(2000, this);
public static final int NUMBER_OF_CIRCLES = 10; //number of circles which are to moved
static int[] circles = new int[NUMBER_OF_CIRCLES];
public void paintComponent(Graphics g)
{
int x = 0; //start point of circles;
int length = 40; //diagonal of the circles
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
Ellipse2D circle;
//painting n circles based on the array
for(int index = 0; index<10; index++)
{
if(circles[index] == 0){ //if the element of the arrayy is 0 then draw a void circle
circle = new Ellipse2D.Double(x, 120, length, length);
g2.draw(circle);
}
else if(circles[index] == 1){ //if the element of the array is 1 them draw a filled circle
circle = new Ellipse2D.Double(x, 120, length, length);
g2.fill(circle);
}
x += 45; //increas start pont of the next circle 45 pixles
}
myTimer.start();
}
public void actionPerformed(ActionEvent e)
{
int tmp; //template for swaping elements
int condition; //condition of the forS
arrayFill(circles); //fills the array based on the writen method, one 1 and one 0 like: 0 1 0 1 0 1 0 1
//here is the part which works good, it changes palces of an elemen at time.
//at the end of this part the array would be like: 1 1 1 1 0 0 0 0
if(NUMBER_OF_CIRCLES % 2 == 0)
condition = circles.length/2 -1;
else
condition = circles.length/2;
for(int i = circles.length-1, k = 1; i>condition; i--, k++)
{
for(int j = i - k; j<i ;j++)
{
tmp = circles[j];
circles[j] = circles[j+1];
circles[j+1] = tmp;
//if we call arrayPrint method it will print the array but I don't know why repaint is not working here
//arrayPrint(circles);
repaint();
}
}
}
//fills the array, one 1 and one 0. Example: 0 1 0 1 0 1 0 1 0 1
public static void arrayFill(int[] array)
{
for(int i = 0; i<array.length; i++)
{
if( i%2 == 0)
array[i] = 0;
else
array[i] = 1;
}
}
}//end of class
And the main Class:
import javax.swing.JFrame;
public class BlackAndWhiteBallsMoving {
public static void main(String[] args)
{
DrawPanel myPanel = new DrawPanel();
JFrame myFrame = new JFrame();
myFrame.add(myPanel);
myFrame.setSize(600, 500);
myFrame.setTitle("Black And White Balls Moving");
myFrame.setVisible(true);
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}//end of class
The events triggered by the Timer are performed on the same event thread as the repaints. Calling repaint does not actively perform a paint event, rather it queues one for later. When you call your repaints from within the timer event, they will only get executed once the timer event is completed.
What you need to do is refactor your loop so that only a single swap is performed each time the timer triggers. I've done this for you as an example:
public class DrawPanel extends JPanel implements ActionListener {
public static final int NUMBER_OF_CIRCLES = 10;
Timer myTimer = new Timer(500, this);
int[] circles = new int[NUMBER_OF_CIRCLES];
public DrawPanel() {
arrayFill(circles);
if(NUMBER_OF_CIRCLES % 2 == 0) {
condition = circles.length/2 -1;
} else {
condition = circles.length/2;
}
i = circles.length - 1;
k = 1;
myTimer.start();
}
int i, j, k;
int condition;
boolean outer = true;
#Override
public void actionPerformed(ActionEvent e) {
if(outer) {
if(i > condition) {
j = i - k; // set j
outer = false; // and move to the inner loop swap
} else {
myTimer.stop(); // the outer loop is done so stop the timer
}
}
if(!outer) {
int tmp = circles[j];
circles[j] = circles[j+1];
circles[j+1] = tmp;
repaint();
j++;
if(j >= i) {
i--;
k++;
outer = true; // move to the outer condition
} // next time the timer triggers
}
}
#Override
protected void paintComponent(Graphics g) {
int x = 0;
int length = 40;
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
Ellipse2D circle;
for(int index = 0; index<10; index++) {
if(circles[index] == 0){
circle = new Ellipse2D.Double(x, 120, length, length);
g2.draw(circle);
} else if(circles[index] == 1){
circle = new Ellipse2D.Double(x, 120, length, length);
g2.fill(circle);
}
x += 45;
}
//myTimer.start();
}
public static void arrayFill(int[] array) {
for(int i = 0; i<array.length; i++) {
if( i%2 == 0) {
array[i] = 0;
} else {
array[i] = 1;
}
}
}
}
(I'm sure it could be factored another way.)
Also:
I added #Override annotations which you should use. Doing so will warn you when you make certain mistakes. (Like misspelling a method name or incorrectly declaring its signature.)
I moved circles to an instance variable because I don't see a reason it should be static. It is part of the state of the DrawPanel instance.
I created a constructor which initializes variables such as circles.
paintComponent is a protected method and it should remain so unless there is a reason to promote it to public.
(I removed your comments and changed the bracing style just to condense the code for my answer.)
As a side note, you should read the tutorial Initial Threads. You are not creating your GUI on the Swing event thread. Basically you need to wrap your code in main inside a call to invokeLater:
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
// create and show your GUI
}
});
}
The basic problem is in your actionPerformed method. Your two for loops are rearranging the array to its final arrangement very quickly. Each loop iteration will take a matter of nanoseconds to milliseconds to complete (it depends on how the repaint() method works). The entire process is finished in less than 50 milliseconds or so. That's too fast for your eyes to keep up.
Basically, the repaint() method is working, but it's working too fast for human eyes to keep up.
If you break up the for loops into something that does one step of the algorithm each time it's called, you can trigger that from a timer and see the animation at a human-detectable speed.
add a paint thread. it should always call repaint() like,
new Thread(){ // this can be started on main or constructor of object
public void run(){
while(true){
repaint();
try {
Thread.sleep(50);
} catch(Exception e){ } 
}
  }
}.start();
and then, on action performed, mark moving objects like movingObjects, keep a animate_x = 0 and keep a boolean variable like existAnimation
then on paintComponent, increase animate_x
animate_x = animate_x + 1;
if (animate_x >= MAX_WIDTH_OF_ANIMATION){
existAnimation = false;
}
and use this existAnimation, animate_x and movingObjects
like,
public void paintComponent(Graphics g)
{
int x = 0; //start point of circles;
int length = 40; //diagonal of the circles
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
Ellipse2D circle;
//painting n circles based on the array
for(int index = 0; index<10; index++)
{
int paint_x = x;
if (movingObjects.has(circles[index])){
paint_x += animate_x;
}
if(circles[index] == 0){ //if the element of the arrayy is 0 then draw a void circle
circle = new Ellipse2D.Double(paint_x, 120, length, length);
g2.draw(circle);
}
else if(circles[index] == 1){ //if the element of the array is 1 them draw a filled circle
circle = new Ellipse2D.Double(paint_x, 120, length, length);
g2.fill(circle);
}
x += 45; //increas start pont of the next circle 45 pixles
}
myTimer.start();
}

Problems with ArrayIndexOutOfBoundsException

I'm quite new to java, and I'm going to need some help here:
package com.game.libs;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
public class Animation {
private int speed = 0; // defines in how many ticks the animation should go to the next frame
private int amountOfFrames = 0; //amount of frames in the animation
private int index = 0; //time value used in the method runAnimation()
private int currentFrameNumber = 0; //the number of the current frame
private BufferedImage frames[]; //images in the animation
private BufferedImage currentFrameImage; //current frame's image
//13 frame animation
public Animation(BufferedImage[] img, int fps){
frames = img;
amountOfFrames = img.length-1;
speed = 60/fps;
for (int i=0;i<img.length-1;i++){
this.frames[i] = img[i];
currentFrameNumber = 0;
currentFrameImage = img[0];
}
}
public void runAnimation(){
index++;
if(index > speed){
index = 0;
nextFrame();
}
}
public void nextFrame(){
currentFrameNumber++;
if(currentFrameNumber > amountOfFrames)
currentFrameNumber = 1;
currentFrameImage = frames[currentFrameNumber]; // THIS IS THE LINE WITH THE ERROR
}
public void drawAnimation(Graphics graphics, int x, int y, int offset){
graphics.drawImage(currentFrameImage, x - offset, y, null);
}
public void setCurrentFrame(int currentFrame){
this.currentFrameNumber = currentFrame;
}
public int getCurrentFrameNumber(){
return currentFrameNumber;
}
public int getFps(){
return speed;
}
public void setFps(int fps){
this.speed = 60/fps;
}
public void setFrames(BufferedImage[] img){
this.frames = img;
amountOfFrames = img.length;
}
public BufferedImage[] getFrames(){
return frames;
}
}
the error that im getting is array index out bounds. in line 38 (nextFrame method, its in the code)
I've encountered this error before, and I know how to (and tried) to fix it but it says 1 and even 0 are out of bounds...
Plox help, I know this is vague :(, but i tried to make my question (and the code) clear.
remember, im new to java, have had experience with waaaaaaay easier languages/engines but understand quite a bit.
Do note that Java arrays are indexed starting at 0, so it is strange (but not wrong) that in nextFrame() you reset currentFrameNumber to 1 instead of 0 when it runs over.
Also, it's pretty redundant to use amountOfFrames to control the iteration count. The usual Java idioms involve using the array .length directly, maybe
if(currentFrameNumber >= frames.length) {
currentFrameNumber = 0;
}
None of that really explains the exception you get, though, so there must be more to it.
Or something else to it: perhaps the class that is throwing the exception was not compiled from the same version of the code that you present.

Animation supposed to change according to a variable in another java file

So basically I have my main code (TuringSimLS.java) and my animation code (Animation.java)
Animation.java creates a number of boxes (dependent on the length of an array in my TuringSimLS.java. The length of this array does not change). Now there is a variable in my TuringSimLS.java which has a variable pointer (this one changes). This pointer decides which box in my animation is in the middle. For example if there are 7 boxes, and the pointer is 6, the box in the middle will have one box to it's right and five to it's left (making it the 6th of boxes). When the pointer changes all the boxes move until the required box is in the middle. The problem is that my method (which changes the value of pointer) finishes running and then my animation starts. So no animation ends up happening (as the pointer's value returns to the value it initially was in the beginning). I need my animation to happen as soon as the variable pointer's value changes.
Part of my main code (TuringSimLS.java)
public static int[] POINTER = new int[1]; // My global variable. How i tell my Animation.java
// What the current value of pointer is
static String[] processTape(String[][] Quints, String[] Tape, int TapeInitial) {
int pointer = TapeInitial;
POINTER[0] = pointer;
...
...
Animation.main(Quints, Tape, TapeInitial); // <- animation started here
while (!currentDir.equals("H")) { // <- IMPORTANT WHILE LOOP as mentioned below
for (String[] Quint : Quints) {
if (Quint[0].equals(currentState) && Quint[1].equals(Tape[pointer])) {
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
Logger.getLogger(TuringSimLS.class.getName()).log(Level.SEVERE, null, ex);
}
Tape[pointer] = Quint[3];
...
...
if (currentDir.equals("R")) {pointer += 1;} // pointer changes here
if (currentDir.equals("L")) {pointer -= 1;}
symbolFound = true;
startFound = true;
POINTER[0] = pointer;
//Here is where i want the animation to start as in
//moving of the already drawn boxes
//System.out.println(POINTER[0]);
break;
}
}
}
return Tape;
}
Here is my Animition.java:
package turingsimls;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.*; // Imports ActionEvent, ActionListener
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
public class Animation extends JPanel implements ActionListener
{
Timer tm = new Timer(5, this);
int x = 0;
int xtri = 300;
int ytri = 30;
public static int[] C = new int[1];
public static int[] Number = new int[1];
public static int[] POINTER2 = new int[1];
public void paintComponent(Graphics g) {
super.paintComponent(g); //9
g.setColor(Color.WHITE);
for (int i = 0; i < Number[0]; i++)
g.fillRect((280 + (40 + 20)*i) + C[0], 50, 40, 30);
int xpoly[] = {xtri , xtri + 10, xtri - 10 };
int ypoly[] = {ytri, ytri - 10, ytri - 10};
g.drawPolygon(xpoly, ypoly, xpoly.length);
tm.start();
}
public void actionPerformed (ActionEvent e) {
//Here C changes according to the pointer
//Which in turn changes the XPos of the rectangles
if (!(C[0] == -60 * TuringSimLS.POINTER[0])){
if ((C[0] < -60 * TuringSimLS.POINTER[0]))
C[0] += 10;
if ((C[0] > -60 * TuringSimLS.POINTER[0]))
C[0] -= 10;
}
System.out.println(C[0]);
System.out.println(TuringSimLS.POINTER[0]);
repaint();
}
public static void main(String[][] Quints, String[] Tape, int TapeInitial) {
Animation t = new Animation(); //2
JFrame jf = new JFrame();
jf.setTitle("Animation");
jf.setSize(600, 400);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.add(t);
Number[0] = Tape.length - 1;
C[0] = -60 * TapeInitial;
}
}
processTape() starts the animation and produces the first rectangles correctly. The problem is the entire "Important while loop" runs before actionPerformed() runs for the second time. So the animation doesnt process the changes of pointer.

Position and orientation of robot in a grid

I want to make a 10x10 grid and put the robot in position (10,1) (bottom left). I want this robot to be able to move forward, turn left/right and to pick up/put objects in a grid. When put in any position, there should be number in a grid which shows how many objects is put in this position, just like this:
..........
...1......
..2.......
....3.....
..........
..........
......9...
.....4....
.........1
..........
We will not see the robot in a grid. I have two classes. Class Robot:
public class Robot {
private Area area;
private Robot rob;
public Robot(Area area){
this.area = area;
rob = new Robot(area);
}
public void Right(){
}
public void Left(){
}
public void Forward(){
}
public void Put(){
}
public void PickUp(){
}
public (?) getPosition(){ // should return robot's position
}
}
Class Area:
private int numberOfObjects;
private Robot robot;
private static final int X = 10;
private static final int Y = 10;
private Object [][] area; // grid
public Area(){ // defines a grid and robot
area = new Area[X][Y];
for(int a=0;a<X;a++){
for(int b=0;b<Y;b++)
area[a][b]=".";
}
numberOfObjects = 0; // grid is initially empty
Area ar = new Area();
robot = new Robot(ar);
}
public void Put(int x,int y){ // put the object to position (x,y)
area[x][y]=numberOfObjects++;
}
public void PickUp(int x,int y){ // pick up the object in position (x,y)
if(area[x][y]!=null){
area[x][y]=numberOfObjects--;
}
}
public void PrintAGrid(){
for(int r=0;r<X;r++){
for(int c=0;c<Y;c++)
System.out.print(area[r][c]+" ");
System.out.println();
}
System.out.println();
}
}
How can I put the robot in position (10,1)? How can I declare and set its orientation (i.e. on the right)? I guess it will be easy to write other methods, so I do not focus on it.
There are several issues with your code.
Why do you have an instance of Robot inside the class Robot? You have not used that instance at all!
private Object [][] area; should be int[][] area. You always save int in this, right?
If I understand your requirements correctly, Your implementation of pick and put is not correct.
Here is a help how you can solve the problems. I had to think several times if Robot should be in Grid or it should be the other way. I ended up with Grid in Robot.
May be Grid could be a singleton.
Here is our Grid
public class Grid {
private int[][] numberOfObjects = new int[10][10];
public void put(int x, int y) {
numberOfObjects[y][x]++;
}
public void pick(int x, int y) {
numberOfObjects[y][x]--;
}
}
You can replace parameters int x, int y with a Point.
And here is the robot
public class Robot {
private static final int NORTH = 0, EAST = 1, SOUTH = 2, WEST = 3;
private int direction;
private int x, y;
private Grid grid;
public Robot(Grid grid) {
this.x = 0;
this.y = 0;
this.grid = grid;
direction = NORTH;
}
public void right() {
direction++;
if (direction == 4) {
direction = 0;
}
}
public void left() {
direction--;
if (direction == -1) {
direction = 3;
}
}
public void forward() {
if (direction == NORTH) {
y--;
} else if (direction == SOUTH) {
y++;
} else if (direction == EAST) {
x++;
} else if (direction == WEST) {
x--;
}
}
public void put() {
grid.put(x, y);
}
public void pick() {
grid.pick(x, y);
}
}
You need to represent the curent location with a variable and initialize it to the 10 1 postion, though your array goes 0-9 and 0-9 so this may be 9,0. to store this position maybe try a Point object that contains a Point x,y.
If someone is interested in a JavaScript version, you can have a look at this repo right here. In general:
The robot must have a facing direction (left, up, right, down).
The are three possible commands: left, right, move.
With that being said, the algorithm is quite straightforward:
totalScore = 0
Foreach i in input
computeCurrentDirection()
if input != MOVE: continue
totalScore += i
return totalScore
There are several sweet-tricks that someone might do to optimize the functions. Take a look at switchDirection.
const directionArray = [Directions.RIGHT, Directions.DOWN, Directions.LEFT, Directions.UP];
const switchDirection = (currDirection, command) => {
if (command === Commands.MOVE) {
return currDirection
}
const currDirectionIndex = directionArray.indexOf(currDirection);
if (command === Commands.RIGHT) {
return directionArray[(currDirectionIndex + 1) % 4];
}
return directionArray[((currDirectionIndex - 1) + 4) % 4];
}
Instead of an exhaustive approach, someone might use an array to help compute the upcoming direction of the robot. This significantly reduces the amount of needed code.
Note this implementation can be easily expanded to accommodate any new requirements needed for project expansion. When faced with such questions, try to architect your codebase in a testable and expandable way, because it's usually the case where reviewers are interested in your coding organizational skills, rather than whether you are able to solve the problem or not.

Having trouble with methods in an array

I am trying to add and remove balls into an array, I'm only allowed to use an array (not an arraylist).
I'm having trouble with the add and remove ball methods and keep getting an " insert "AssignmentOperator Expression" " error on length for add and remove ball methods. Not sure how to fix the problem.
Help appreciated.
private final int DIAMETER = 60;
private java.awt.Color _currentColor;
//private SmartEllipse _ball;
SmartEllipse [] myBalls = new SmartEllipse [25];
public BallPanel () {
super();
this.setBackground(java.awt.Color.white);
myBalls = new SmartEllipse[0];
_currentColor = java.awt.Color.red;
this.addMouseListener(new MyMouseListener());
}
//translate the click coordinates to a grid coordinate
public java.awt.Point translatePoint(java.awt.Point aPoint)
{
int x = aPoint.x;
int y = aPoint.y;
aPoint.x = x / 20;
aPoint.y = y / 20;
return aPoint;
}
//find a ball in the array
public int findBall(java.awt.Point p)
{
for (SmartEllipse se : myBalls )
{
java.awt.Point translatedPoint = translatePoint(se.getLocation());
if (translatedPoint.x == p.x && translatedPoint.y == p.y)
return myBalls.length;
}
return -1;
}
// remove a ball from the array
public void removeBall(SmartEllipse p)
{
myBalls.length-1;
this.repaint();
this.revalidate();
}
//add a peg to the array
public void addBall(SmartEllipse p)
{
myBalls.length;
this.repaint();
this.revalidate();
}
public void paintComponent(java.awt.Graphics aBrush)
{
super.paintComponent(aBrush);
java.awt.Graphics2D betterBrush = (java.awt.Graphics2D) aBrush;
for (SmartEllipse se : myBalls )
{
se.draw(betterBrush);
se.fill(betterBrush);
}
}
private class MyMouseListener extends javax.swing.event.MouseInputAdapter
{
public void mouseClicked(MouseEvent e)
{
// get X and Y of click
// translate X and Y to grid
// check peg array for duplicate
// if duplicate, remove ball
// else add ball to ball array
java.awt.Point p = translatePoint(e.getPoint());
// Adjust the coordinates
int index = findBall(p);
for (int i = 0; i< myBalls.length; i++)
{
myBalls[i] = null;
if ( index != -1)
remove(myBalls.length);
else
addBall(new SmartEllipse(_currentColor, p.x, p.y));
}
}
}
}
I'm not sure I understand your question since you don't specify very well where you're getting the error, but it looks as if you are declaring myBalls as an array of 25 elements, but then in the constructor your are assigning it a new array of zero elements. You can't add anything to an array of zero elements.

Categories