IndexOutofBounds exception creating a maze - java

I'm working on a school assignment where I am supposed to create a maze from a textfile. My problem is creating the maze. I know it's probably an easy fix, but I am really stuck and need to get this done.. Any suggestions?
package application;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import javafx.application.Application;
import javafx.stage.FileChooser;
import javafx.stage.FileChooser.ExtensionFilter;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.GridPane;
public class Main extends Application {
LabyrintRute[][] Labyrint;
int X;
int Y;
int startx;
int starty;
Spiller spilleren;
int sizeX;
int sizeY;
#Override
public void start(Stage primaryStage) {
try {
GridPane root = new GridPane();
Spiller spilleren = new Spiller(startx, starty);
filLeser();
root.add(spilleren.getUtseende(), spilleren.getxPossisjon(), spilleren.getyPossisjon());
for(int x = 0; x<X; x++){
for(int y = 0; y<Y; y++){
root.add(Labyrint[x][y].getUtseende(), x, y);
}
}
Scene scene = new Scene(root, X*10, Y*10);
scene.setOnKeyPressed(new FilLytter(this));
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public void filLeser() {
String teksten = "";
File fila;
int rad = 0;
FileChooser filvelger = new FileChooser();
filvelger.setTitle("Åpne en tekstfil");
filvelger.getExtensionFilters().add(new ExtensionFilter("Text Files", "*.txt"));
fila = filvelger.showOpenDialog(null);
try (Scanner filleser = new Scanner(fila)) {
X = filleser.nextInt();
Y = filleser.nextInt();
teksten = filleser.nextLine();
Labyrint = new LabyrintRute [X][Y];
while (filleser.hasNext()) {
teksten = filleser.nextLine();
for (int i = 0;i< X;i++) {
char tegn = teksten.charAt(i);
switch (tegn) {
case '#':
Labyrint[i][rad] = new Vegg(i, rad);
break;
case ' ':
Labyrint[i][rad] = new Gang(i, rad);
break;
case '-':
Labyrint[i][rad] = new Utgang(i, rad);
break;
case '*':
Labyrint[i][rad] = new Gang(i, rad);
startx = i;
starty = rad;
break;
}
rad++;
}
}
} catch (FileNotFoundException e) {
System.out.println("Kan ikke åpne fila!");
e.printStackTrace();
}
}
public void flyttSpiller(int deltax, int deltay) {
int nyx = spilleren.getxPossisjon() + deltax;
int nyy = spilleren.getyPossisjon() + deltay;
Labyrint[nyx][nyy].flyttHit(spilleren);
}
public static void main(String[] args) {
launch(args);
}
}
The "Vegg"(wall) class:
package application;
import javafx.scene.Node;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
public class Vegg extends LabyrintRute {
private Node utseende;
public Vegg(int xKoordinat, int yKoordinat) {
super(xKoordinat, yKoordinat);
utseende = new Rectangle(10, 10, Color.MEDIUMPURPLE);
}
#Override
public void flyttHit(Spiller spilleren) {
//spilleren.setxPossisjon(getxKoordinat());
//spilleren.setyPossisjon(getyKoordinat());
}
#Override
public Node getUtseende() {
return utseende;
}
}
Abstract class:
package application;
import javafx.scene.Node;
public abstract class LabyrintRute {
private int xKoordinat;
private int yKoordinat;
public LabyrintRute(int xKoordinat, int yKoordinat) {
this.xKoordinat = xKoordinat;
this.yKoordinat = yKoordinat;
}
public int getxKoordinat() {
return xKoordinat;
}
public int getyKoordinat() {
return yKoordinat;
}
public abstract void flyttHit(Spiller spilleren);
public abstract Node getUtseende();
}
I also have a "Gang" (passage), and a "Utgang" (exit) class. They are similiar to the "Vegg" class.
Would really appreciate inputs! Sorry for the norwegian code..

When you read the file and create the lab, your variable rad is incremented in a loop that goes to from 0 to X-1. However, you use it to index the Y dimension. If X > Y, you will run out of bounds there.
Since your code does not fully reveal the rules of the file and how the lab can be formed,I cannot provide a fix directly.
I think you most probably need some kind of nested loop. Maybe you should keep and increment i with the nextLine's and have the inner loop somehow like for (int rad = 0; rad < Y; rad++) but again, it's impossible for me to say without further inside.

Related

Using an updated Path() to draw new lines on Scene

I am new to javafx and was using it to carry out a version of the Ant Colony Optimization algorithm. I have written some code that was supposed to display a small circle as the ant which moves about randomly in the plane. This worked fine. But I could not modify the code to display the path of the ant on the scene. Basically I want the path of the ant to be highlighted by lines as the ant moves on a 2D plane.
Here is the code:
import java.util.ArrayList;
import java.util.Random;
//Line can either be a sound or a shape
import javafx.animation.Animation.Status;
import javafx.animation.PathTransition;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.MoveTo;
import javafx.scene.shape.LineTo;
import javafx.scene.shape.Path;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
import javafx.util.Duration;
import java.lang.Object;
import javafx.scene.Node;
import javafx.scene.shape.Shape;
import javafx.scene.shape.Line;
public class TestMotion extends Application {
int N = 10;
static ArrayList<Integer> move = new ArrayList<Integer>();
private double sceneWidth = 512;
private double sceneHeight = 512;
MyNode node = new MyNode(0,0,5);
Path pathA = new Path();
int n = 10;
#Override
public void start(Stage primaryStage) throws Exception {
Group root = new Group();
root.getChildren().addAll(node, pathA);
Scene scene = new Scene( root, sceneWidth, sceneHeight);
primaryStage.setScene( scene);
primaryStage.show();
animate();
}
//convention conversion (i,j) to node number x
public static int convert(int i, int j, int N)
{
return (j*N +i+1);
}
//convention conversion node number x to (i,j)
//implement in order of definition
public static int reconvertY(int x, int N)
{
return (int) java.lang.Math.floor((x-1)/N);
}
public static int reconvertX(int x, int N, int j)
{
return (x-j*N-1);
}
private void animate() {
nextPos(move, N);
int y1 = reconvertY(move.get(move.size()-1), N);
int x1 = reconvertX(move.get(move.size()-1), N, y1);
MyNode here = new MyNode(10*x1, 10*y1, 1);
System.out.println(move.get(move.size()-1));
pathA.getElements().add (new MoveTo ( node.getTranslateX() + node.getBoundsInParent().getWidth() / 2.0, node.getTranslateY() + node.getBoundsInParent().getHeight() / 2.0));
pathA.getElements().add (new LineTo( here.getTranslateX() + here.getBoundsInParent().getWidth() / 2.0, here.getTranslateY() + here.getBoundsInParent().getHeight() / 2.0));
pathA.setStroke(Color.RED);
pathA.setStrokeWidth(1.0);
PathTransition pathTransitionA = new PathTransition();
pathTransitionA.setDuration(Duration.millis(1000));
pathTransitionA.setNode(node);
pathTransitionA.setPath(pathA);
pathTransitionA.play();
pathA = new Path();
pathTransitionA.setOnFinished( new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
if( pathTransitionA.getStatus() == Status.RUNNING)
{return;}
animate();
}
});
}
//allot new node to the arraylist such that it is adjacent to the previously assigned node
private void nextPos(ArrayList<Integer> array, int N)
{
//I removed this part to save space because it is probably irrelevant the the problem at hand
}
public static void main(String[] args) {
move.add(1);
launch(args);
}
public static class MyNode extends StackPane {
public MyNode(double x, double y, double r) {
// create circle
Circle circle = new Circle();
circle.setRadius(r);
circle.setFill(Color.BLUE);
// set position
setTranslateX( x);
setTranslateY( y);
getChildren().add(circle);
}
}
}
On execution the scene displays the path for the first step only and then stops displaying the path altogether. I cannot figure out why. The path is renewed each time the animate() function runs, and both node and here should be updated regularly. So shouldn't each iteration display the newest path segment of the ant?
Can someone please explain the problem? Is it not possible to exploit Path() and PathTransition() to display these segments and avoid using Line()?
Your approach is not wrong. Only the problem is whenever you are calling the animate(), the path formed is having the coordinates as before. The path coordinates are always returning the following
1.0, 1.0 for moveTo and 5.0,5.0 for LineTo
You need to pass fresh coordinates each time as that the path becomes a new one. Also to keep showing the old path along with the new one, you need to keep adding the newly created path to the Group.
Here's what I have changed in you code. Have a look.
import java.util.ArrayList;
import javafx.animation.Animation.Status;
import javafx.animation.PathTransition;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.MoveTo;
import javafx.scene.shape.LineTo;
import javafx.scene.shape.Path;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
import javafx.util.Duration;
public class TestMotion extends Application {
int N = 10;
static ArrayList<Integer> move = new ArrayList<Integer>();
private double sceneWidth = 512;
private double sceneHeight = 512;
MyNode node = new MyNode(0, 0, 5);
Group root; //Made root as global variable
int n = 10;
#Override
public void start(Stage primaryStage) throws Exception {
root = new Group();
root.getChildren().addAll(node);
Scene scene = new Scene(root, sceneWidth, sceneHeight);
primaryStage.setScene(scene);
primaryStage.show();
animate(0.0f, 0.0f, 5.0f, 5.0f); //Passing initial coordinates to the method
}
//convention conversion (i,j) to node number x
public static int convert(int i, int j, int N) {
return (j * N + i + 1);
}
//convention conversion node number x to (i,j)
//implement in order of definition
public static int reconvertY(int x, int N) {
return (int) java.lang.Math.floor((x - 1) / N);
}
public static int reconvertX(int x, int N, int j) {
return (x - j * N - 1);
}
private void animate(float moveX, float moveY, float lineX, float lineY) {
Path pathA = new Path();
root.getChildren().add(pathA);
nextPos(move, N);
int y1 = reconvertY(move.get(move.size() - 1), N);
int x1 = reconvertX(move.get(move.size() - 1), N, y1);
//Applying coordinates as obtained
pathA.getElements().add(new MoveTo(moveX, moveY));
pathA.getElements().add(new LineTo(lineX, lineY));
pathA.setStroke(Color.RED);
pathA.setStrokeWidth(1.0);
PathTransition pathTransitionA = new PathTransition();
pathTransitionA.setDuration(Duration.millis(100));
pathTransitionA.setNode(node);
pathTransitionA.setPath(pathA);
pathTransitionA.play();
pathTransitionA.setOnFinished(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
if (pathTransitionA.getStatus() == Status.RUNNING) {
return;
}
animate(lineX, lineY, lineX + 2, lineY + 2); //previous path's endpoint makes the start point of the new Path
}
});
}
//allot new node to the arraylist such that it is adjacent to the previously assigned node
private void nextPos(ArrayList<Integer> array, int N) {
//I removed this part to save space because it is probably irrelevant the the problem at hand
}
public static void main(String[] args) {
move.add(1);
launch(args);
}
public static class MyNode extends StackPane {
public MyNode(double x, double y, double r) {
// create circle
Circle circle = new Circle();
circle.setRadius(r);
circle.setFill(Color.BLUE);
// set position
setTranslateX(x);
setTranslateY(y);
getChildren().add(circle);
}
}
}
Hope It helps.

Drawing in class that extends javafx.scene.canvas.Canvas

I am trying to extend a java fx Canvas:
package com.kavricious.math;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;
import java.util.Random;
public class LPlotCanvas extends Canvas{
boolean grid = false;
boolean axes = true;
boolean ticks = true;
boolean startDrawn = false;
double xMin;
double xMax;
double yMin;
double yMax;
int density;
short canvasWidth = 800;
short canvasHeight = 800;
byte colorIndex = 0;
private GraphicsContext gc = null;
public LPlotCanvas(double xmin,double xmax, double ymin, double ymax){
xMin = xmin;
xMax = xmax;
yMin = ymin;
yMax = ymax;
density = 100;
setWidth(canvasWidth);
setHeight(canvasHeight);
gc = getGraphicsContext2D();
drawStart();
}
private void drawStart(){
System.out.println("drawing start");
if(gc == null)System.out.println("it null");
gc.setLineWidth(2);
gc.setStroke(Color.BLACK);
if(axes){
if(xMin<0.0&&xMax>0.0){
drawLine(gc,0,yMin-1,0,yMax+1);
}
if(yMin<0.0&&yMax>0.0){
drawLine(gc,xMin-1,0,xMax+1,0);
}
}
if(grid){
for(int i =(int)((xMin>0)?Math.floor(xMin):Math.ceil(xMin));i<xMax+1;i++){
gc.setLineWidth(0.25);
drawLine(gc,i,yMin,i,yMax);
}
for(int i =(int)((yMin>0)?Math.floor(yMin):Math.ceil(yMin));i<yMax+1;i++){
gc.setLineWidth(0.25);
drawLine(gc,xMin,i,xMax,i);
}
}
startDrawn = true;
}
public void showGrid(boolean b){
grid = b;
}
public void showAxes(boolean b){
axes = b;
}
public void showTicks(boolean b){
ticks = b;
}
public void setCanvasWidth(short width){
this.canvasWidth = width;
}
public void setCanvasHeight(short height){
this.canvasHeight = height;
}
private void plotPoint(double x,double y,Color color){
if(!startDrawn)drawStart();
gc.setFill(color);
gc.fillOval(convertX(x),convertY(y),5,5);
}
public void drawPolynomial(LPolynomial function)throws Exception{
if(!startDrawn)drawStart();
gc.setStroke(getNextColor(colorIndex));
double start = xMin;
double step = (xMax-xMin)/density;
for(int i=0;i<density;i++){
drawLine(gc,start+step*i,function.evaluateAt(start+step*i),start+step*(i+1),function.evaluateAt(start+step*(i+1)));
}
colorIndex++;
}
private void drawLine(GraphicsContext gc,double x1,double y1,double x2, double y2){
int nx1 = convertX(x1);
int ny1 = convertY(y1);
int nx2 = convertX(x2);
int ny2 = convertY(y2);
gc.strokeLine(nx1,ny1,nx2,ny2);
System.out.println("line drawn");
}
private int convertX(double in){
double widthOfPixel = (xMax-xMin)/canvasWidth;//how much space on the real number line is put into one pixel
return (int) Math.floor((in-xMin)/widthOfPixel);//how much greater than left side DIVIDED by pixel size -> how many pixels away from left side
}
private int convertY(double in){
double heightOfPixel = (yMax-yMin)/canvasHeight;
return canvasHeight - (int) Math.floor((in-yMin)/heightOfPixel);
}
public Color getNextColor(byte i){
switch(i){
case 0: return Color.rgb(255,0,0);//red
case 1: return Color.rgb(0,200,0);//lime
case 2: return Color.rgb(0,0,235);//blue
case 3: return Color.rgb(255,255,0);//yellow
case 4: return Color.rgb(255,50,255);//magenta/fuchsia
case 5: return Color.rgb(255,165,0);//orange
case 6: return Color.rgb(0,235,235);
case 7: return Color.rgb(107,30,119);
case 8: return Color.rgb(0,100,0);
default: Random rand = new Random();
int r = rand.nextInt(256);
int g = rand.nextInt(256);
int b = rand.nextInt(256);
return Color.rgb(r,g,b);
}
}
}
and then call it in another application:
package com.kavricious.math;
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;
import javafx.scene.shape.ArcType;
import java.util.ArrayList;
public class LPlotter2 extends Application{
private ArrayList<LPolynomial> functions;
public static void main(String[] args){
launch(args);
}
#Override
public void start(Stage ps){
this.functions = new ArrayList<LPolynomial>();
LPolynomial[] funcs = new LPolynomial[]{new LPolynomial(2.3,3.4,2.2,1.1),new LPolynomial(0,1,-2.3),new LPolynomial(1,2),new LPolynomial(2,2,2.3,1,0.4)};
for(LPolynomial f: funcs){
this.functions.add(f);
}
ps.setTitle("LPlotter");
StackPane layout = new StackPane();
LPlotCanvas canv = new LPlotCanvas(-10,10,-10,10);
for(LPolynomial function : functions){
try{
canv.drawPolynomial(function);
}catch(Exception e){System.out.println("an exception wad caught");e.printStackTrace();}
}
layout.getChildren().add(canv);
ps.setScene(new Scene(layout,800,800));
ps.show();
}
}
(In the above code I have tried putting the for loop that draws the polynomials in every position possible after the LPlotCanvas is initialized, and the same result occurs.)
but then I run it and see nothing:
This is what it should look like (created without extension).
So what is wrong?

Java Agario single player

How do you make a java single player version of agario? I would like to use Object Oriented Programming. So there should be Dot, Blob, and a Controller Object.
(I just want to share my code so don't vote down :)
I made it this way. There is the dots with random color, blob that starts as 20 size. A thread check every couple milliseconds for collision.
Dot:
package agario;
import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.util.Random;
public class Dot extends Component{
public int x;
public int y;
public Color c;
public int size;
Dot(int x, int y){
this.x = x;
this.y = y;
Random rand = new Random();
int r = rand.nextInt(255);
int green = rand.nextInt(255);
int b = rand.nextInt(255);
this.c = new Color(r,green,b);
this.size = rand.nextInt(50);
}
public void paint(Graphics g){
g.setColor(c);
g.fillOval(this.x, this.y, this.size, this.size);
}
}
Blob:
package agario;
import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
public class Blob extends Component{
public int x;
public int y;
public int size;
public Color color;
public static Blob blob = new Blob(800,100,40,Color.blue);
Blob(int x, int y, int size, Color c){
this.x = x;
this.y = y;
this.size = size;
this.color = c;
}
public void paint(Graphics g){
g.setColor(color);
g.fillOval(x, y, size, size);
}
}
Controller:
package agario;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Label;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Random;
public class Controller {
ArrayList<Blob> blobs = new ArrayList<Blob>();
public ArrayList<Dot> dots = new ArrayList<Dot>();
Blob b = new Blob(50,50,10,Color.CYAN);
MyFrame mf = new MyFrame("Agario");
Label l = new Label("10");
int mouseX = 0;
int mouseY = 0;
static int score = 30;
static int xDis = 0;
static int yDis = 0;
public static void main(String[] args){
new Controller().startGame();
}
public void startGame(){
mf.addMouseMotionListener(new MyMouseMoveListener());
mf.add(l,BorderLayout.NORTH);
Refresh rf = new Refresh();
Thread t = new Thread(rf);
t.start();
while(true){
try{
Random r = new Random();
Thread.sleep(r.nextInt(40));
double dis = Math.sqrt(xDis*xDis + yDis*yDis);
double easingAmount = 20;
b.x += xDis / easingAmount;
b.y += yDis / easingAmount;
if(r.nextInt(10) == 5){
int randX = r.nextInt(600);
int randY = r.nextInt(600);
Dot d = new Dot(randX,randY);
synchronized(dots){
dots.add(d);
}
mf.add(d);
mf.repaint();
}
}catch(Exception e){
}
}
}
class Refresh implements Runnable{
public void run() {
while(true){
Random ran = new Random();
try{
Thread.sleep(ran.nextInt(20));
}catch(Exception e){
System.out.println("error");
}
Rectangle r = new Rectangle(b.x,b.y,b.size,b.size);
synchronized(dots){
Iterator i = dots.iterator();
while(i.hasNext()){
Dot d = (Dot) i.next();
Rectangle r1 = new Rectangle(d.x,d.y,d.size,d.size);
if(r1.intersects(r)){
i.remove();
b.size += 1;
l.setText(String.valueOf(Integer.parseInt(l.getText())+1));
score += 1;
}
}
}
mf.repaint();
}
}
}
class MyMouseMoveListener extends MouseMotionAdapter{
public void mouseMoved(MouseEvent m){
mouseX = m.getX();
mouseY = m.getY();
xDis = mouseX-b.x;
yDis = mouseY-b.y;
}
}
class MyFrame extends Frame{
MyFrame(String s){
super(s);
setBounds(0,0,900,900);
add(b);
blobs.add(b);
setVisible(true);
}
public void paint(Graphics g){
for(Blob b : blobs)
b.paint(g);
synchronized(dots){
Iterator i = dots.iterator();
while(i.hasNext()){
Dot d = (Dot) i.next();
d.paint(g);
}
}
}
}
}

JavaFX image ghosting while moving quickly

I stumbled upon a problem: if an image is moving at a high speed across the screen it is rendered incorrectly producing a ghosting effect. I think we can rule out my monitor being the problem as this type of movement was flawless in swing (with the same framerate).
Looks like:
Code (merged from 3 classes):
import javafx.animation.AnimationTimer;
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import java.awt.Dimension;
import java.awt.Toolkit;
public class Constructor extends Application{
Image player, shot;
static Dimension screen = new Dimension(Toolkit.getDefaultToolkit().getScreenSize());
static int wid = screen.width;
static int hei = screen.height;
static boolean up, down, left, right, rotleft , rotright;
static double x = (wid/2)-109;
static double y = (hei/1.5)-132;
static double velx = 0, vely = 0, velx2 = 0, vely2 = 0;
static double forspeed = 0, sidespeed = 0;
static int rotat = 0;
public void load(){
player = new Image("res/sprite.png");
}
#Override
public void start(final Stage frame) throws Exception{
load();
frame.setTitle("DEFAULT");
frame.initStyle(StageStyle.UNDECORATED);
Group root = new Group();
final ImageView ship = new ImageView();
ship.setImage(player);
root.getChildren().add(ship);
frame.setScene(new Scene(root, wid, hei, Color.BLACK));
frame.addEventHandler(KeyEvent.KEY_PRESSED, new EventHandler<KeyEvent>(){
public void handle(KeyEvent key) {
if(key.getCode()==KeyCode.W)
up = true;
if(key.getCode()==KeyCode.S)
down = true;
if(key.getCode()==KeyCode.Q)
left = true;
if(key.getCode()==KeyCode.E)
right = true;
if(key.getCode()==KeyCode.A)
rotleft = true;
if(key.getCode()==KeyCode.D)
rotright = true;
}
});
frame.addEventHandler(KeyEvent.KEY_RELEASED, new EventHandler<KeyEvent>(){
public void handle(KeyEvent key) {
if(key.getCode()==KeyCode.ESCAPE)
{
frame.close();
System.exit(0);
}
if(key.getCode()==KeyCode.W)
up = false;
if(key.getCode()==KeyCode.S)
down = false;
if(key.getCode()==KeyCode.Q)
left = false;
if(key.getCode()==KeyCode.E)
right = false;
if(key.getCode()==KeyCode.A)
rotleft = false;
if(key.getCode()==KeyCode.D)
rotright = false;
}
});
frame.setAlwaysOnTop(true);
frame.setHeight(hei);
frame.setWidth(wid);
frame.setResizable(false);
frame.setFullScreen(true);
frame.show();
new AnimationTimer() {
#Override
public void handle(long now) {
gameloop();
ship.setTranslateX(x);
ship.setTranslateY(y);
ship.setRotate(rotat);
}
}.start();
}
public static void gameloop(){
if(Shared.up)
forspeed += 1;
if(Shared.down)
forspeed -= 1;
if(Shared.right)
sidespeed += 1;
if(Shared.left)
sidespeed -= 1;
if(Shared.rotleft)
rotat -=3;
if(Shared.rotright)
rotat +=3;
velx = Math.cos(Math.toRadians(rotat-90))*forspeed + Math.cos(Math.toRadians(rotat))*sidespeed;
vely = Math.sin(Math.toRadians(rotat-90))*forspeed + Math.sin(Math.toRadians(rotat))*sidespeed;
if(!Shared.up && !Shared.down)
{
if(forspeed > 0)
forspeed -= 0.2;
else if (forspeed < 0)
forspeed += 0.2;
}
if(!Shared.right && !Shared.left)
{
if(sidespeed > 0)
sidespeed -= 0.2;
else if (sidespeed < 0)
sidespeed += 0.2;
}
x += velx;
y += vely;
screencolisions();
}
private static void screencolisions() {
// LEFT RIGHT
if(x < 0)
{
x = 0;
sidespeed = 0;
}
else if (x+218 > Shared.wid)
{
x = Shared.wid-218;
sidespeed = 0;
}
// UP DOWN
if(y < 0)
{
y = 0;
forspeed = 0;
}
else if (y+164 > Shared.hei)
{
y = Shared.hei-164;
forspeed = 0;
}
}
public static void main(String[] args){
Application.launch(args);
}
}
Well first thing is you are using AWT classes in Javafx .. They are not friends (most of the time).
Instead of using Dimension and the AWT toolkit,
Use the provided Javafx Screen class
Screen screen = Screen.getPrimary();
wid = screen.getBounds().getWidth();
hei = screen.getBounds().getHeight();
// Visual bounds will be different depending on OS and native toolbars etc..
// think of it as desktop bounds vs whole screen
For example:
public class ScreenBounds extends Application {
#Override
public void start(Stage primaryStage) {
Screen screen = Screen.getPrimary();
System.out.println("ScreenBounds : " + screen.getBounds() + "\nVisualBounds : " + screen.getVisualBounds());
Platform.exit();
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
prints:
compile-single:
run-single:
ScreenBounds : Rectangle2D [minX = 0.0, minY=0.0, maxX=1680.0, maxY=1050.0, width=1680.0, height=1050.0]
VisualBounds : Rectangle2D [minX = 0.0, minY=40.0, maxX=1680.0, maxY=1050.0, width=1680.0, height=1010.0]
As you can see no Syntax error as you described..
Though that error is probably due to your variables being Integers not Doubles like the method returns;
ScheduledService Timer:
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import javafx.concurrent.ScheduledService;
import javafx.concurrent.Task;
import javafx.util.Duration;
/**
*
* #author jdub1581
*/
public class NanoTimer extends ScheduledService<Void> {
private final long ONE_NANO = 1000000000L;
private final double ONE_NANO_INV = 1f / 1000000000L;
private long startTime, previousTime;
private double frameRate, deltaTime;
private final NanoThreadFactory tf = new NanoThreadFactory();
public NanoTimer() {
super();
this.setPeriod(Duration.millis(16));// equivalent to 60 fps
this.setExecutor(Executors.newCachedThreadPool(tf));
}
public double getTimeAsSeconds() {
return getTime() * ONE_NANO_INV;
}
public long getTime() {
return System.nanoTime() - startTime;
}
public long getOneSecondAsNano() {
return ONE_NANO;
}
public double getFrameRate() {
return frameRate;
}
public double getDeltaTime() {
return deltaTime;
}
private void updateTimer() {
deltaTime = (getTime() - previousTime) * (1.0f / ONE_NANO);
frameRate = 1.0f / deltaTime;
previousTime = getTime();
}
#Override
public void start() {
super.start();
if (startTime <= 0) {
startTime = System.nanoTime();
}
}
#Override
public void reset() {
super.reset();
startTime = System.nanoTime();
previousTime = getTime();
}
private boolean init = true;
#Override
protected Task<Void> createTask() {
return new Task<Void>() {
#Override
protected Void call() throws Exception {
updateTimer();
// perform NON UI calculations here
return null;
}
};
}
#Override
protected void succeeded() {
super.succeeded();
//update the UI here
}
#Override
protected void failed() {
getException().printStackTrace(System.err);
}
#Override
public String toString() {
return "ElapsedTime: " + getTime() + "\nTime in seconds: " + getTimeAsSeconds()
+ "\nFrame Rate: " + getFrameRate()
+ "\nDeltaTime: " + getDeltaTime();
}
/*==========================================================================
creates a daemon thread for use
*/
private class NanoThreadFactory implements ThreadFactory {
public NanoThreadFactory() {
}
#Override
public Thread newThread(Runnable r) {
Thread t = new Thread(r, "NanoTimerThread");
t.setDaemon(true);
return t;
}
}
}//=============================================================================
Whatever class you put it in, just call the start method, probably best nested as a private class so variable can be used inside of it.. Or rewrite as you need..
I use it here: My Cloth Simulation and it works well doing 50k+ calculations per frame

Why am I getting a "java.util.NoSuchElementException"?

Here the error i am getting...
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at Maze.Map.readFile(Map.java:60)
at Maze.Map.<init>(Map.java:23)
at Maze.Board.<init>(Board.java:16)
at Maze.Maze.<init>(Maze.java:15)
at Maze.Maze.main(Maze.java:9)
Below is my code!
package Maze;
import javax.swing.*;
public class Maze
{
public static void main(String[] args)
{
new Maze();
}
public Maze()
{
JFrame f = new JFrame();
f.setTitle("Maze Game");
f.add(new Board());
f.setSize(464, 485);
f.setLocationRelativeTo(null);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
package Maze;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Board extends JPanel implements ActionListener
{
private Timer timer;
private Map m;
public Board()
{
m = new Map();
timer = new Timer(25, this);
timer.start();
}
public void actionPerformed(ActionEvent e)
{
repaint();
}
public void paint(Graphics g)
{
super.paint(g);
for(int y = 0; y < 14; y++)
{
for(int x = 0; x < 14; x++)
{
if(m.getMap(x, y).equals("g"))
{
g.drawImage(m.getFloor(), x * 32, y * 32, null);
}
if(m.getMap(x, y).equals("w"))
{
g.drawImage(m.getWall(), x * 32, y * 32, null);
}
}
}
}
}
package Maze;
import java.awt.*;
import java.io.*;
import java.util.*;
import javax.swing.ImageIcon;
public class Map
{
private Scanner m;
private String Map[] = new String[14];
private Image floor,
wall;
public Map()
{
ImageIcon img = new ImageIcon("C://Test//MazeGame//floor.jpg");
floor = img.getImage();
img = new ImageIcon("C://Test//MazeGame//wall.jpg");
wall = img.getImage();
openFile();
readFile();
closeFile();
}
public Image getFloor()
{
return floor;
}
public Image getWall()
{
return wall;
}
public String getMap(int x, int y)
{
String index = Map[y].substring(x, x + 1);
return index;
}
public void openFile()
{
try
{
m = new Scanner(new File("C://Test//MazeGame//Map.txt"));
}catch(Exception e)
{
System.out.print("Error Loading Map!");
}
}
public void readFile()
{
while(m.hasNext())
{
for(int i = 0; i < 14; i++)
{
Map[i] = m.next();
}
}
}
public void closeFile()
{
m.close();
}
}
I don't know if this is your error (you haven't told us which line is line 60 of your Map class, the line that is causing your exception), but this is dangerous code:
while(m.hasNext())
{
for(int i = 0; i < 14; i++)
{
Map[i] = m.next();
}
}
You're calling hasNext() once per loop iteration, but calling next() 14 times! There should be a strict 1 to 1 correlation in that each next() should match with a preceding hasNext().
Also, there's no reason for the nested for loop since the while loop will handle all you need. You probably would get by with something like:
int i = 0;
while(m.hasNext()) {
Map[i] = m.next();
i++;
}
But it would be safer to use an ArrayList rather than an array.
As an aside, please learn and follow Java coding conventions. Methods and fields/variables/parameters should all start with a lower-case letter, so Map[i] is not allowed, but rather should be map[i]. Doing this will help us to better understand and follow your code and thus help you.

Categories

Resources