How to getTileProperty while using multi-layer map? - java

I'm new to Slick2D, and I'm using Tiled to make .tmx map.
for (int xAxis = 0; xAxis < map.getWidth(); xAxis++) {
for (int yAxis = 0; yAxis < map.getHeight(); yAxis++) {
int tileID = map.getTileId(xAxis, yAxis, 0);
String value = map.getTileProperty(tileID, "blocked", "0");
int valueInInt = Integer.parseInt(value);
if (valueInInt == 1) {
blocked[xAxis][yAxis] = true;
}
}
}
This works fine when the blocks are on the same layer with others, however, if I put the blocks on a different Tile layer, I can't get the right TileProperty anymore.
Why did it happen? What can I do with that or any ideals?
Thanks a lot.

The third parameter to TiledMap.getTileId() is the layerIndex. You would have to use that to select the layer where you're searching for tiles with the "blocked" property set to "1".

Related

Trying to make a photo filter's effects cumulative on the photo-java swing

Basically I have made an interface that displays a picture and it has multiple JSliders. Each one has a different function such as blur, brighten, and saturate. I have implemented the sliders in such a way that I override the stateChanged method when adding a new slider. This works fine when I do the sliders individually, however it changes back to the original picture once I use a different slider. I want to make it so that it the picture accumulates the filters on the photo. Any suggestions? Here is an example of one of my sliders.
brightSlider.addChangeListener(new ChangeListener() {
#Override
public void stateChanged(ChangeEvent e) {
JSlider source = (JSlider) e.getSource();
double scaleValue = source.getValue() / 100.0;
Picture newPic = new PictureImpl(picture.getWidth(), picture.getHeight());
//Picture newPic = picture;
Pixel zeroPixel = new ColorPixel(0, 0, 0);
Pixel p;
for (int i = 0; i < picture.getWidth(); i++) {
for (int j = 0; j < picture.getHeight(); j++) {
newPic.setPixel(i, j, zeroPixel);
}
}
for (int i = 0; i < picture.getWidth(); i++) {
for (int j = 0; j < picture.getHeight(); j++) {
if (scaleValue > 0) {
p = picture.getPixel(i, j).lighten(scaleValue);
newPic.setPixel(i, j, p);
} else if (scaleValue < 0) {
p = picture.getPixel(i, j).darken(scaleValue);
newPic.setPixel(i, j, p);
}
}
}
setPic(newPic);
picture_view.setPicture(newPic.createObservable());
}
});
As shown in Image processing with Java 2D, you can create a Map<String, BufferedImageOp> that holds concrete instances of the BufferedImageOp interface.
Map<String, BufferedImageOp> ops = new TreeMap<String, BufferedImageOp>();
ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
ops.put("Gray", new ColorConvertOp(cs, null));
…
Add the Map key set to a JComboBox.
final JComboBox opBox = new JComboBox();
for (String key : ops.keySet()) {
opBox.addItem(key);
}
In the combo's handler, invoke the image operation's filter() method on your target BufferedImage.
String key = (String) opBox.getSelectedItem();
BufferedImageOp op = ops.get(key);
bufferedImage = op.filter(bufferedImage, null);
The image below illustrates "Threshold 64" followed by "Invert". ImageDicer is a complete example.

Android Game Logic - Obsticles

I have been working on android apps for a long time but now I have decided to create a game aside with my pre-calculus final. I have completed the whole code and it works perfectly except one tiny issue. First of the game is based on flying pig(my classmate's face) avoiding top and bottom osticle. I developed an algorithm so that the obsticles are evenly spaced and based on random selection placed either as the top or bottom of the screen but never both at the same time!. The algorithm that needs improvement is in the 3rd code segment!
This is the screenshot of the problem: screenshot here
(My account is new so stackoverflow wont let me to share photos directly)
This is the class that calls updates for all dynamic objects (ship = pig, bacon = bonus item, BM = BarrierManager class's update() which updates the obsticles)
//this will update the resources
void Update(float dt) {
ship.update(dt);
//bumbing
if (!ship.death) {
background.update(dt);
**BM.update(dt);**
for (int i = 0; i % 5 == 0; i++) {
bacon.update(dt, BM.position);
}
}
ArrayList<Point> bacon_point = new ArrayList<Point>(bacon.getArray());
if (ship.bump(bacon_point.get(0), bacon_point.get(1), bacon_point.get(2), bacon_point.get(3))) {
bacon.setX(-200);
bacon.setY(-200);
Message msg = BM.game_panel.game.handler.obtainMessage();
msg.what = 0;
BM.game_panel.game.handler.sendMessage(msg);
}
for (int i = 0; i < BM.TopWalls.size(); i++) {
ArrayList<Point> temp = new ArrayList<Point>(BM.TopWalls.get(i).getArray());
//after we have accest the TopWalls arraylist we can call bump that check TopWalls object's points position with the pig's points
ArrayList<Point> temp2 = new ArrayList<Point>(BM.BottomWalls.get(i).getArray());
//after we have accest the TopWalls arraylist we can call bump that check BottomWalls object's points position with the pig's points
if ((ship.bump(temp.get(0), temp.get(1), temp.get(2), temp.get(3))) || (ship.bump(temp2.get(0), temp2.get(1), temp2.get(2), temp2.get(3))) || ship.death) {
ship.death = true;
game.onStop();
while(f==0) {
MediaPlayer mp = MediaPlayer.create(game, R.raw.explosion_fart);
mp.start();
f++;
}
f++;
Message msg = BM.game_panel.game.handler.obtainMessage();
msg.what = 1;
BM.game_panel.game.handler.sendMessage(msg);
i = BM.TopWalls.size()-1;
if(f == 8){
thread.setRunning(false);
}
}
}
}
In the BarrierManager I have created this update method which takes float dt = MainTheards general time for the game.
TopWalls is ArrayList this ArrayList is composed of top obsticles. Bottom walls is the same but BottomWalls.
//zreb decides if it should create TopWalls or BottomWalls object. This parameter is later transfered to the Barier.update method where I work with it
public void update(float dt){
for (int i=0;i<Num+1; i++) {
int zreb = new Random().nextInt(2);
//{if} to make the spacing bigger
if (i % 5 == 0){
**TopWalls.get(i).update(dt, true, zreb);
BottomWalls.get(i).update(dt, false, zreb);**
if(zreb == 0){
position.set(TopWalls.get(i).getX(), TopWalls.get(i).getY());
}
else{
position.set(BottomWalls.get(i).getX(),BottomWalls.get(i).getY());
}
}
}
}
Now this algoritm in the Barrier.class is where the magic went wrong. This update method takes the float dt mentioned earlier, a boolean variable for determining if the obsticle we work with at that instance is the Top or Bottom, and the zreb random int that determines if the top or bottom obsticle is going to be shown.
//problem! needs to be discussed
public void update(float dt, boolean b, int zreb) {
//checking if the barrier is still there
if (x<-bitmap.getWidth()){
//'b'is passed from the Barriermanager - 'update' method, determining if we have to use monkey-true or farmer-false
int raz = 200;
int vyska = BM.dpos;
//'zreb' helps me to randomly determine if monkey or ballard is going to appear
//here it determines if we are going to have Top or Bottom obsticle in the game
if(zreb == 1) {
vyska = BM.dpos - raz;
}
else {
vyska = BM.dpos + raz;
}
//koniec tohto trienedia
if (b)
{
//setting the y value for the top wall
y = vyska - BM.dl/2 - bitmap.getHeight()/2;
}
else{
//setting the y value for bottom wall
y = vyska + BM.dl/2 + bitmap.getHeight()/2;
}
//setting x-value
x = (int) (x +bitmap.getWidth()*(BM.TopWalls.size()-1));
}
x = (int) (x - BM.game_panel.ShipSpeed*dt);
}
Do you have any idea why this "one-or-the-other" condition is not working properly?
This would help me lot because this error made me deactivate the app from the store.

TableView and List Data

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;
});

Creating Boundaries to prevent player from moving into in java

In the Game I'm going to have a set path of JButtons. I also have an Array which holds all and only the values of the JButtons I want the player to be able to move onto.
JButton[] x = new JButton[71];
x[0] = y[0];
x[1] = y[15];
x[2] = y[16];
x[3] = y[31];
x[4] = y[32];
x[5] = y[47];
x[6] = y[62];
x[7] = y[93];
x[8] = y[63];
x[9] = y[78];
x[10] = y[108];
x[11] = y[168];
x[12] = y[183];
x[13] = y[184];
x[14] = y[153];
x[15] = y[123];
x[16] = y[138];
x[17] = y[197];
x[18] = y[195];
x[19] = y[185];
x[20] = y[186];
x[21] = y[171];
x[22] = y[172];
x[23] = y[173];
I also have another array which holds every single value of the JButtons in my Map.
y = new JButton[225];
for (int j = 0; j < 225; j++) {
y[j] = new JButton();
y[j].addActionListener(this);
panel.add(y[j]);
}
Using this code, how do I prevent the player from moving off the desired path/JButtons and stay on that singular path?
Create a point that follows the button, if the point strikes a certain point on the grid (in this case, your bounds) than flip the x/y-dir or set the speed to 0.
Point point = new Point(xloc, yLoc);
if (point.getX() >= bounds) {
flipSpeed/stopSpeedHere
}
Also you can probably use a forLoop, and just iterate through all that code instead of just typing it out.

Image histogram generated by JFreeChart

I want to display histogram of image color channels.
At first my reading of pixels looks like:
for(int i=0; i<width; i++)
for(int j=0; j<height; j++) {
data=writeableRaster.getDataElements(i, j, null);
red=colorModel.getRed(data);
green=colorModel.getGreen(data);
blue=colorModel.getBlue(data);
rgb=(red+green+blue)/3;
++redL[red];
++greenL[green];
++blueL[blue];
++rgbL[rgb];
}
}
I also have additional method for creating chart with given channel colors table:
int number = channelHistogram.length;
HistogramDataset dataset = new HistogramDataset();
dataset.setType(HistogramType.RELATIVE_FREQUENCY);
dataset.addSeries("Hist",channelHistogram,number);
String plotTitle = "Hist";
String xaxis = "number";
String yaxis = "value";
PlotOrientation orientation = PlotOrientation.VERTICAL;
boolean show = false;
boolean toolTips = false;
boolean urls = false;
JFreeChart chart = ChartFactory.createHistogram( plotTitle, xaxis, yaxis,
dataset, orientation, show, toolTips, urls);
But chart is wrong displayed. It means at Y axis there are "low" values (from ~ 0 - 0.09) and at X axis there aren't values from scope 0 - 255.
Any help?
dataset.setType(HistogramType.RELATIVE_FREQUENCY);
Can you try setting different options here and see if it helps? Also if you can show what channelHistogram field contains that may be helpful to debug.

Categories