circling text
hi, i need to know how to made this using loop in javafx.
am i need to set the coordinate and rotation manually , or is there something i need to know to make the text circling neatly ?
here's my code so far :
int x = 0;
int y = 100;
int r = -50;
for(int i =0; i<=14; i++){
Text text2 = new Text(x,y, ary[i]);
text2.setRotate(r);
pane.getChildren().add(text2);
x=x+10;
y=y-5;
if(x<=90){
x=x+10;
y=y-5;
r=r+5;
}
else if(x>=100){
x = x-10;
y = y+5;
r = r+3;
}
}
Related
I have this png image:
and a string, say "Hello World". In order to map texture coords for LWJGL, I need to know the X and Y position of each 16x16 character in the PNG. I am completely lost on how one would do that.. Anyone?
Start with something like this:
final int spriteWidth = 16;
final int spriteHeight = 16;
...
int rows = sheet.getWidth()/spriteWidth;
int cols = sheet.getHeight()/spriteHeight;
BufferedImage sheet = ImageIO.read(new File("\\a\b\\c\\sprite_file.png"));
BufferedImage[] images = new BufferedImage[rows * cols];
for(int y = 0; y < cols; y++) {
for(int x = 0; x < rows; x++) {
images[y * x] = sheet.getSubImage(x * rows, y * cols, spriteWidth, spriteHeight);
}
}
Then make final int variables like so:
public static final int SPRITE_0 = 0; public static final int SPRITE_1 = 1;...
and access like so:
images[SPRITE_0]
Edit:
taking into consideration what #MadProgrammer has stated, I would recommend that you split the image into two parts, like so:
(split at the red line)
and then simply altering the code to handle the two different parts. The code will remain the same except for the variables final int spriteWidth and final int spriteHeight. I'm sure you can handle this yourself.
Edit 2:
if you just want the x and y co-ords of the top left corner of each sprite, do the following:
final int spriteWidth = 16;
final int spriteHeight = 16;
...
int rows = sheet.getWidth()/spriteWidth;
int cols = sheet.getHeight()/spriteHeight;
Point[] spriteTopLeftCorner = new Point[rows * cols];
for(int y = 0; y < sheet.getHeight(); y += spriteHeight) {
for(int x = 0; x < sheet.getWidth(); x += spriteWidth) {
spriteTopLeftCorner[y/spriteHeight * x/spriteWidth] = new Point(y, x);
}
}
you would ofcourse still need to make variables representing each sprite's index in this Array, otherwise you wouldn't know what sprite you are taking out.
Do this like so:
public static final int SPRITE_0 = 0; public static final int SPRITE_1 = 1;...
and access like so:
spriteTopLeftCorner[SPRITE_0];
I am trying to display a List of icons (which are referred simply by their path) in a TableView that has x-amount of columns and any number of cells that are required to display all the icons.
The plan was to display the icons in such a way that the TableView acts as a "multi-lined" ListView.. so that they go from left to the right.
It's the first time I'm using the TableView control, and I'm a bit confused of how to achieve this.
Thanks for any pointers.
So, as ItachiUchiha suggested, I ended up using a Pagination control with a GridPane, and it works very nicely.
Here's the code if anyone stumbles upon the same thing..
List<String> allEntries = Icons.getAllEntries();
int numCols = 8;
int numRows = 5;
int numPages = allEntries.size() / (numCols * numRows);
Pagination pagination = new Pagination(numPages);
Collections.sort(allEntries);
pagination.setPageFactory(pageIndex -> {
GridPane layout = new GridPane();
for (int y = 0; y < numRows; y++) {
for (int x = 0; x < numCols; x++) {
int index = numCols * y + x + (pageIndex * numCols * numRows);
String path = allEntries.get(index);
Image image = new Image(path, true);
ImageView imageView = new ImageView(image);
imageView.setFitWidth(64);
imageView.setFitHeight(64);
layout.add(imageView, x, y);
imageView.setOnMousePressed(e -> {
// Do stuff
});
}
}
return layout;
});
I would like to compare two arrays to see if they have the same values.
If I have a array called
public static float data[][]
which holds Y coordinates of a terrain, how can I check that array with another
public static int coords[][]
without iterating through all the coordinates?
Both arrays have over 1000 values in them. Iterating through them is not an option, since I must iterate through them over four times per second.
I am doing this to attempt to find if two objects are colliding. I have attempted using libraries for this, however I cannot find per-coordinate collision detection as specific as I need it.
Edit: Why I am unable to just iterate through this small amount of vertices is this.
The problem is, this is a MultiPlayer game,and I would have to iterate through all 1000 coordinates for every player. Meaning that just 10 players online is 10,000 100 online is 100,000. You can see how easily that would lag or at least take up a large percentage of the CPU.
Input of coordinates into the "Data" variable:
try {
// Load the heightmap-image from its resource file
BufferedImage heightmapImage = ImageIO.read(new File(
"res/images/heightmap.bmp"));
//width = heightmapImage.getWidth();
//height = heightmapImage.getHeight();
BufferedImage heightmapColour = ImageIO.read(new File(
"res/images/colours.bmp"));
// Initialise the data array, which holds the heights of the
// heightmap-vertices, with the correct dimensions
data = new float[heightmapImage.getWidth()][heightmapImage
.getHeight()];
// collide = new int[heightmapImage.getWidth()][50][heightmapImage.getHeight()];
red = new float[heightmapColour.getWidth()][heightmapColour
.getHeight()];
blue = new float[heightmapColour.getWidth()][heightmapColour
.getHeight()];
green = new float[heightmapColour.getWidth()][heightmapColour
.getHeight()];
// Lazily initialise the convenience class for extracting the
// separate red, green, blue, or alpha channels
// an int in the default RGB color model and default sRGB
// colourspace.
Color colour;
Color colours;
// Iterate over the pixels in the image on the x-axis
for (int z = 0; z < data.length; z++) {
// Iterate over the pixels in the image on the y-axis
for (int x = 0; x < data[z].length; x++) {
colour = new Color(heightmapImage.getRGB(z, x));
data[z][x] = setHeight;
}
}
}catch (Exception e){
e.printStackTrace();
System.exit(1);
}
And how coordinates are put into the "coords" variable (Oh wait, it was called "Ship", not coords. I forgot that):
try{
File f = new File("res/images/coords.txt");
String coords = readTextFile(f.getAbsolutePath());
for (int i = 0; i < coords.length();){
int i1 = i;
for (; i1 < coords.length(); i1++){
if (String.valueOf(coords.charAt(i1)).contains(",")){
break;
}
}
String x = coords.substring(i, i1).replace(",", "");
i = i1;
i1 = i + 1;
for (; i1 < coords.length(); i1++){
if (String.valueOf(coords.charAt(i1)).contains(",")){
break;
}
}
String y = coords.substring(i, i1).replace(",", "");;
i = i1;
i1 = i + 1;
for (; i1 < coords.length(); i1++){
if (String.valueOf(coords.charAt(i1)).contains(",")){
break;
}
}
String z = coords.substring(i, i1).replace(",", "");;
i = i1 + 1;
//buildx.append(String.valueOf(coords.charAt(i)));
////System.out.println(x);
////System.out.println(y);
////System.out.println(z);
//x = String.valueOf((int)Double.parseDouble(x));
//y = String.valueOf((int)Double.parseDouble(y));
//z = String.valueOf((int)Double.parseDouble(z));
double sx = Double.valueOf(x);
double sy = Double.valueOf(y);
double sz = Double.valueOf(z);
javax.vecmath.Vector3f cor = new javax.vecmath.Vector3f(Float.parseFloat(x), Float.parseFloat(y), Float.parseFloat(z));
//if (!arr.contains(cor)){
if (cor.y > 0)
arr.add(new javax.vecmath.Vector3f(cor));
if (!ship.contains(new Vector3f((int) sx, (int) sy, (int) sz)))
ship.add(new Vector3f((int) sx, (int) sy, (int) sz));
// arr.add(new javax.vecmath.Vector3f(Float.parseFloat(x), Float.parseFloat(y), Float.parseFloat(z)));
}
Thanks!
You can do like this but only applicable for same data type.
Arrays.deepEquals(data, coords);
For single dimension Array you can use this
Arrays.equals(array1, array1);
Arrays.deepEquals(a, b);
Try this but this will work only if the elements are in order.
No way around it, I'm afraid. Comparing data sets to see if they are identical demands looking at all elements, by definition. On a side note, comparing 1000 values is nothing even on relatively old hardware. You can do it thousands of time per second.
I started to work into a little image processing software. Currently I just need to set a image to black & white and loop through its pixels and generate a report of a counter, coordinates and color of each pixel (#,x,y,color).
It works fine with tiny images I create just to test, but when I use a real picture, it takes several minutes or even crash my software. My code inside the loop seems to be simple to be crashing.
Any tip on how can I improve it? Thanks in advance!
void processImage(BufferedImage image) {
exportStr = "#,x,y,color"+ newline;
Color c = new Color(0);
int imgW = image.getWidth();
int imgH = image.getHeight();
matrix = new int[imgW][imgH];
int currentPx = 1;
for(int x=0; x < imgW; x++)
{
for(int y=0; y < imgH; y++)
{
c = new Color(image.getRGB(x, y));
if(c.equals(Color.WHITE))
{
matrix[x][y] = 1;
}
String color = matrix[x][y]==1 ? "W" : "B";
exportStr += formatStringExport(currentPx, x, y, color); // just concatenate values
currentPx++;
}
}
return img;
}
This is probably your problem
exportStr += formatStringExport(currentPx, x, y, color);
Use a StringBuilder instead:
StringBuilder sb = new StringBuilder(imgW * imgH * <String size per pixel>);
....
sb.append(formatStringExport(currentPx, x, y, color));
Check the StringBuilder documentation for details. Additionally, you could try to reduce the number of objects being created. So for instance replace:
c = new Color(image.getRGB(x, y));
String color = matrix[x][y]==1 ? "W" : "B";
by ...
if(image.getRGB(x, y).equals(...))
sb.append(formatStringExport(currentPx, x, y, (matrix[x][y]==1 ? "W" : "B")));
Good luck! :)
I have a processing game i am designing that is supposed to display a game over screen when the game is lost. Unfortuinately, the game over screen never comes up. Here is the function i have it narrowed down to:
if (lives < 1) {
background(0);
textSize(100);
text("You Lost",10,10);
delay(1000);
lives = 10;
x = (int)random(width);
y = (int)random(height / 2);
velocity = new PVector(1,random(-1.4,-0.6));
score = 0;
}
When the amount of lives goes to zero, it pauses for a second and then restarts the game.
I have tried everything i can think of, but still no luck.
So my best guess, having gotten to know the processing language 2 minutes ago is the following:
Because you set the background to black (0) the text can't be seen because it is black as well, maybe try changing the text's color to something else with the fill() method to see if that's causing the issue.
if (lives < 1) {
background(0);
textSize(100);
fill(255, 255, 255); // White text
text("You Lost",10,10);
delay(1000);
lives = 10;
...
Figured it out:
i added a piece of code at the start of my loop:
if (dflag) {
delay(2000);
dflag = false;
}
and then, i put the regular update code in an else statement after it check weather you die:
if (lives < 1) {
// for(int df = 0; df < 1000; df++) {
background(0);
textSize(100);
text("You Lost",100,100);
dflag = true;
//redraw();
lives = 10;
x = (int)random(width);
y = (int)random(height / 2);
velocity = new PVector(1,random(-1.4,-0.6));
score = 0;
} else {
textSize(13);
background(0);
stroke(255);
text(score,10,10);
String l = "";
for (int q = 0; q < lives; q++) { l += "%"; }
text(l,50,10);
strokeWeight(balld);
point(x,y);
strokeWeight(8);
line(paddlex,height - 30,paddlex + paddlew,height-30);
}