Problem moving multiple balls with RMI? - java

I am working on a distributed animation of moving BALLS with RMI.
My goal is to move multiple balls in a way, so that multiple clients observe the same movement/position of balls, i m using ball object which is remote object.
The ball is moving fine, when it is only one, but when i m trying to increase the number of balls, i m failing.
here are some code snippets where i applied loops to work for multiple balls:
on server:
b[0] = new BallImpl(0, 50, 2, 3 ,Color.YELLOW,20);
b[1] = new BallImpl(50, 50, 4, 3,Color.BLUE,10);
b[2] = new BallImpl(40, 40, 5, 5, Color.PINK,30);
b[3] = new BallImpl(60, 70, 4, 6, Color.GREEN,40);
for (int i = 0; i < currentNumBalls; i++) {
Naming.rebind ("rmi://localhost/BouncingBall", b[i]); // registers Ball object
System.out.println ("remote ball object registered.");
}
on client site :
that how i look up for remote balls:
for (int i = 0; i < currentNumBalls; i++) {
try {
this.aBall[i] = (Ball) Naming.lookup("rmi://localhost/BouncingBall");
} catch (Exception e) {
System.out.println("Exception: " + e);
}
}
start();
thats moving balls code:
public void moveballs() {
for (int i = 0; i < currentNumBalls; i++) {
try {
aBall[i].setBounds(pit.getWidth(), pit.getHeight());
aBall[i].move();
pit.repaint();
} catch (Exception e) {
e.printStackTrace();
}
}
}
and that's the drawing code:
public void drawballs(Graphics g) {
for (int i = 0; i < currentNumBalls; i++) {
try {
g.setColor(aBall[i].getColor());
g.fillOval(aBall[i].getX(), aBall[i].getY(), aBall[i].getradius(), aBall[i].getradius());
} catch (RemoteException e) {
e.printStackTrace();
}
}
}
Can somebody please guide me why i can see only one ball moving, what about other balls or there is some problem in this design and i m using the RMI in wrong way? or recommend me some design by which i can accomplish my goal.
thanks a lot,
jibby

It looks like you are binding all of your balls under the same name. You need to give them different names, like this:
for (int i = 0; i < currentNumBalls; i++) {
Naming.rebind ("rmi://localhost/BouncingBall"+i, b[i]); //add index to the name
System.out.println ("remote ball object registered.");
}
Then when looking them up, use this:
for (int i = 0; i < currentNumBalls; i++) {
try {
this.aBall[i] = (Ball) Naming.lookup("rmi://localhost/BouncingBall"+i);
} catch (Exception e) {
System.out.println("Exception: " + e);
}
}

Related

Java iterator has suddenly stopped working after a certain point in loop

I have suddenly been having a problem in regards to using an iterator while looping where it only goes through about ⅔ (two thirds) of the array before it seemingly just fails to work properly the last 20,000 elements or so after having worked with the same code previously. The relevant code block is as follows:
private void loadInvTable(){
ArrayList<InventoryData.InventoryObject> invArray = new ArrayList<>();
int rowCount = dtm.getRowCount();
try {
invArray = AllJsonData.allQuantityParserTest();
} catch (Exception e) {
e.printStackTrace();
}
Iterator<InventoryData.InventoryObject> invIt = invArray.iterator();
for(int i = 0; i < rowCount; i++){
String upc = dtm.getValueAt(i, dtm.findColumn("UPC")).toString();
while (invIt.hasNext()){
InventoryData.InventoryObject invObj = invIt.next();
String test = invObj.getUpc().toString();
if(invObj.getUpc().equals(upc)){
System.out.println(invObj.getUpc());
dtm.setValueAt(invObj.getNm(), i, dtm.findColumn("NM"));
dtm.setValueAt(invObj.getLp(), i, dtm.findColumn("LP"));
dtm.setValueAt(invObj.getMp(), i, dtm.findColumn("MP"));
dtm.setValueAt(invObj.getHp(), i, dtm.findColumn("HP"));
dtm.setValueAt(invObj.getDam(), i, dtm.findColumn("DAM"));
invIt.remove();
break;
}
}
}
invArray.clear();
}
The data it is comparing is an array parsed from a file and a JTable each with about 100,000 rows. The problem being is that when I do it in other ways it takes much longer but works properly:
private void loadInvTable() {
ArrayList<InventoryData.InventoryObject> invArray = new ArrayList<>();
try {
invArray = AllJsonData.allQuantityParserTest();
} catch (Exception e) {
e.printStackTrace();
}
int rowCount = dtm.getRowCount();
for(InventoryData.InventoryObject inv : invArray){
for(int i = 0; i < rowCount; i++){
if(dtm.getValueAt(i, dtm.findColumn("UPC")).toString().equals(inv.getUpc())){
dtm.setValueAt(inv.getNm(), i, dtm.findColumn("NM"));
dtm.setValueAt(inv.getLp(), i, dtm.findColumn("LP"));
dtm.setValueAt(inv.getMp(), i, dtm.findColumn("MP"));
dtm.setValueAt(inv.getHp(), i, dtm.findColumn("HP"));
dtm.setValueAt(inv.getDam(), i, dtm.findColumn("DAM"));
break;
}
}
}
}
private void loadInvTable() {
ArrayList<InventoryData.InventoryObject> invArray = new ArrayList<>();
InventoryData.InventoryObject invObj;
try {
invArray = AllJsonData.allQuantityParserTest();
//System.out.println(ints);
} catch (Exception e) {
e.printStackTrace();
}
Iterator<InventoryData.InventoryObject> invIt = invArray.iterator();
int count = 0;
int rowCount = dtm.getRowCount();
while(invIt.hasNext()){
invObj = invIt.next();
for(int i = 0; i < rowCount; i++){
if(dtm.getValueAt(i, dtm.findColumn("UPC")).toString().equals(invObj.getUpc())){
dtm.setValueAt(invObj.getNm(), i, dtm.findColumn("NM"));
dtm.setValueAt(invObj.getLp(), i, dtm.findColumn("LP"));
dtm.setValueAt(invObj.getMp(), i, dtm.findColumn("MP"));
dtm.setValueAt(invObj.getHp(), i, dtm.findColumn("HP"));
dtm.setValueAt(invObj.getDam(), i, dtm.findColumn("DAM"));
invIt.remove();
break;
}
}
count++;
}
}
I tried to both rewrite and debug the problem section of the program, but I can't seem to find a way to code it so that it works nearly as quickly the first block and when debugging the code it seems to either hang on the code block and I need to kill the program or it runs and just skips/repeats one of the the rest of the elements in the array.
I have no idea what exactly I'm doing wrong and am even having trouble pinpointing what could be causing the issue or what had changed to break it and have been trying for days to get it working again. I was hoping for some help if anyone can point where the mistake is. If this question has already been asked elsewhere I apologize. I couldn't find a similar question anywhere. Thanks in advance for any help anyone can provide.

I'm trying to delete an object in my world that is saved in an ArrayList but it doesn't work

I use dyn4j physics engine (although I don't think it's relevant for my question).
I don't get any errors but if I press the right mouse button on one of the objects it doesn't delete the object.
Does anyone know what might be the problem?
Here I add the objects to my world:
public void mouseClicked(MouseEvent e) {
double x = e.getPoint().getX()/100;
double y = e.getPoint().getY()/100;
if (addBlocks.isSelected()) {
BodyFixture fblock = new BodyFixture(Geometry.createRectangle(1,1));
dblock = new Body();
if (e.getButton() == MouseEvent.BUTTON1) {
dblock.addFixture(fblock);
dblock.getTransform().setTranslation(x,y);
dblock.setMass(MassType.NORMAL);
dblock.getFixture(0).setRestitution(0.3);
world.addBody(dblock);
gameObjects.add(new GameObject("dirtblock.png", dblock, new Vector2(0, 0), 0.8));
}
}
}
And here I'm trying to delete them:
if (e.getButton() == MouseEvent.BUTTON3) {
for (int i = 0; i < gameObjects.size(); i++) {
if (gameObjects.get(i).contains(e.getPoint())) {
world.removeBody(gameObjects.get(i).body);
gameObjects.remove(i);
}
}
}
Usually when you are deleting items with index in a loop you start the loop at the end of the collection due to indices changing i.e. try this:
if (e.getButton() == MouseEvent.BUTTON3) {
for (int i = gameObjects.size() - 1; i >= 0 ; i--) {
if (gameObjects.get(i).contains(e.getPoint())) {
world.removeBody(gameObjects.get(i).body);
gameObjects.remove(i);
}
}
}

Change the background of a panel multiple times in the same method

Hi i'm trying to change the background of a panel to create something similar to the Simon color games.
Here's what I have for code. The problem I seem to be finding is that the only time the background physically changes only at the end of the button being pressed. I have tried using Repaint(); and it has had no effect.
private void ClickRandomColorActionPerformed(java.awt.event.ActionEvent evt) {
Random gen = new Random();
for(int i = 0; i < 5; i++){
int CaseNum = 0;
CaseNum = gen.nextInt(3) +1;
ChangeBackground(CaseNum);
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException ex) {
Logger.getLogger(Testerino.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
public void ChangeBackground(int i){
System.out.println(i);
if(i == 1){
RandColor.setBackground(blue);
}
else if (i == 2){
RandColor.setBackground(black);
}
else if(i == 3){
RandColor.setBackground(magenta);
}
}

Why does my Loop mechanism not work?

Sorry if this is a very closed question that won't be useful to others, but I'm just stumped by this bug and I haven't been able to solve it for weeks!
I am working on a wave based survival game and am currently working on a spawning mechanism.
The code I wrote works perfectly for one wave, but somehow doesn't restart for further waves.
I have written the code below:
public void run() {
while (ingame) {
if (enemyList.isEmpty()) {
stopSpawn = false;
try {
Thread.sleep(500);
spawnNewEnemy();
} catch (InterruptedException ex) {
System.out.println("Interrupted");
}
} else {
if (!enemyList.isEmpty() && !stopSpawn) {
// getEnemyAmount returns the amount of enemies that should be spawned this wave
for (int enemiesSpawned = 0; enemiesSpawned < getEnemyAmount(); enemiesSpawned++) {
try {
Thread.sleep(500);
} catch (InterruptedException ex) {
}
System.out.println(currentWave);
spawnNewEnemy();
}
stopSpawn = true;
}
}
}
}
Here is the spawnNewEnemy method
public void spawnNewEnemy() {
Random spawn = new Random();
int spawnX = spawn.nextInt(500);
int spawnXTest = b.bX - spawnX;
if (spawnXTest < 20) {
spawnX = spawnX + 20;
} else {
}
int spawnY = spawn.nextInt(500);
int spawnYTest = b.bX - spawnY;
if (spawnYTest < 20) {
spawnY = spawnY + 20;
} else {
}
spawnY = spawnY + 20;
spawnX = spawnX + 20;
enemyList.add(new Enemy(spawnX, spawnY));
}
I can read the following in your code:
If the list of enemies is empty, you set stopSpawn to false and spawn an enemy.
That triggers your else-statement.
There, you spawn enemies based on the enemy count.
stopSpawn is set to true, thus your else-statement doesn't get triggered anymore.
Nothing happens anymore untill your enemylist is empty.
If your enemylist is empty again, you start over.
The logics seems ok, so I'm thinking either the way you spawn enemies through spawnNewEnemy() is faulty, or the way you remove enemies from the enemyList is faulty. I see neither of that code so that is as far as I can go in this answer.
I guess your problem with a loop is in stopSpawn value.
You set it to true after the first wave and likely not setting to `false' before starting the next wave.

Java simple for loop does not iterate

During execution the method is called and the integer i displays as 0 on screen. However, no output is forthcoming from inside the for loop, suggesting the for loop doesnt execute. I have also tested this with breakpoints, and have obtained the same results. Any help is appreciated.
private void decrypt_btnActionPerformed(java.awt.event.ActionEvent evt) {
int ciphertext_length = Ciphertext().length();
String decrypted_char = "";
int i = 0;
System.out.println("increment" + i);
try {
for (i = 0; i == ciphertext_length; i++) {
System.out.println("test" + i);
String cipher_current_char = getLetterAtIndex(Ciphertext(), i);
int pos_char_in_alphabet = getIndexAtLetter(Alphabet(), cipher_current_char);
decrypted_char = decrypted_char +
getLetterAtIndex(Alphabet(), pos_char_in_alphabet - 5);
status_label.setText(100 / i + "%");
}
} catch (Exception e) {
e.getMessage();
}
plain_ta.setText(decrypted_char);
}
for (i = 0; i==ciphertext_length; i++){
should in all likelihood be
for (i = 0; i<ciphertext_length; i++){

Categories