Unable To Delete Multi Selected Objects - java

I'm trying to select mutiple objects by pressing SHIFT key, then by right click and select the delete option, it shall be able to delete all the selected objects.
However, it didn't work.
My code are as follow.
MouseClick
clickShape = null;
int x = clickEvent.getX(); // x-coordinate of point where mouse was
// clicked
int y = clickEvent.getY(); // y-coordinate of point
// when press down shift key
if (clickEvent.isShiftDown()) {
for (int i = 0; i < shapes.size(); i++) {
Shape s = (Shape) shapes.get(i);
if (s.containsPoint(x, y)) {
s.setColor(Color.RED);
multiShape.add(s);
}
}
DeleteSelection
else if (command.equals("Delete Selection")) {
for (Shape s : multiShape)// look for multishape size
shapes.remove(multiShape);
}// remove selectedmultiple object.

it should be
shapes.remove(s);
not
shapes.remove(multiShape);
you should pass shape s to remove method to remove shape s from shapes ArrayList
java.util.ArrayList.remove(Object) // this is the method you are using .

Related

Changing colors of a list of shapes java

I'm stuck on this problem:
When I click inside a shape (there's a list of rectangles and circles) it changes its color. But when I click outside, it doesn't change back.
public void mouseClicked(MouseEvent me) {
Color colorAux;
for (int i = 0; i < images.size(); i++) {
colorAux = images.get(i).getColor();
if (images.get(i).getShape() == "Rectangle") {
if ((images.get(i).getLocation().getX() < me.getX() && images.get(i).getLocation().getY() < me.getY() && images.get(i).getX() + images.get(i).getWidth() > me.getX() && images.get(i).getLocation().getY() + images.get(i).getHeight() > me.getY())) {
images.get(i).setColor(Color.BLUE);
repaint();
JOptionPane.showMessageDialog(null, colorAux); //Debug
} else if (!(images.get(i).getLocation().getX() < me.getX() && images.get(i).getLocation().getY() < me.getY() && images.get(i).getX() + images.get(i).getWidth() > me.getX() && images.get(i).getLocation().getY() + images.get(i).getHeight() > me.getY()) && (images.get(i).getColor() == Color.BLUE)) {
images.get(i).setColor(colorAux);
repaint();
}
}
}
Should I be using an array of colors? Don't know how can I solve this. To clarify what I am trying to archive, here is an example:
If the list contains a purple rectangle, I want it to change to blue when clicking inside it (which works). Then, when I click outside the rectangle, I want it to change back to purple (which does not work).
I've tried the Leon's advice, but it did not work. Where am i doing wrong?
Being more specific, when i draw only 1 shape it works! But for example, i draw a blue rectangle, a purple circle and a red rectangle, and click inside some of the shapes, like the red rectangle, every shape changes its color to BLUE. And when i click outside again, it changes every shape's color to the default color (black).
public void mouseClicked(MouseEvent me) {
List<Color> colors = new ArrayList<Color>();
for (int j = 0; j < images.size(); j++) {
colors.add(images.get(j).getColor());
}
for (int i = 0; i < images.size(); i++) {
if ((images.get(i).getLocation().getX() < me.getX() && images.get(i).getLocation().getY() < me.getY() && images.get(i).getX() + images.get(i).getWidth() > me.getX() && images.get(i).getLocation().getY() + images.get(i).getHeight() > me.getY())) {
images.get(i).setColor(Color.BLUE);
repaint();
} else {
images.get(i).setColor(colors.get(i));
repaint();
}
}
}
Your idea of using a list of colors in the right idea (unless all objects have the same color at the start). In this list you store the initial color of all objects and the you can do
// initialColors is the list holding the initial colors
for (int i=0; i<images.size(); i++) {
if (images.get(i).getShape() == "Rectangle") {
if (/*code to check if we are inside the rectangle which you already have*/) {
images.get(i).setColor(Color.BLUE);
repaint();
} else {
images.get(i).setColor(initialColors.get(i));
repaint();
}
} /* maybe add a case for getShape() == "Circle" */
}
You could create and populate the initialColors List at the same time as the images List (because in that moment, you know which color each shape has).
About why your approach is not working: Let's say we have clicked inside a rectangle and its color is changed to blue. When we now use colorAux = images.get(i).getColor() to get the color, we get blue, because we changed the rectangles color. When we then reach images.get(i).setColor(colorAux), we set the color of the blue rectangle to blue, meaning nothing happens.
Also, you do not need the else if and can use else instead, as the first if check whether the click happend inside the rectangle. Meaning we execute the else branch when the click was not inside the rectangle and we can simply reset the color in there.
Edit: Now the change you added to the question does not work, as we still have the same problem: We get the color in the mouseClicked event, not when the shapes are initially colored. This means we fill the list of colors with the current colors (which might have been changed to blue), not the initial ones. You should move the loop you added wherever you initially color the shapes (probably where you fill the images list).
Here's what I think is happening :
User clicks the mouse inside the shape - and the shape's color is set to blue.
User clicks the mouse outside of the shape, and we set the color to colorAux, which was set by the images color, which is blue!
You need to save the original color somewhere.

Java - Selecting and moving an object

I want to basically select an object on the frame and then drag it and the object moves with the mouse. For some reason I can't seem to get it to work.
Here is my code pressed method :
public void mousePressed(MouseEvent event)
{
//Get x and y
int x = event.getX();
int y = event.getY();
//set selected to null
selected = null;
//if a fruit contains x, y then selected is assigned that fruit
for( Fruit m : fruits)
{
if(m.contains(x,y))
{
selected.setXY(x,y);
}
}
This is my drag method:
public void mouseDragged(MouseEvent event)
{
//if selected not equal null
if(selected != null){
//get x and y
int x = event.getX();
int y = event.getY();
//make select follow the mouse and repaint
selected.setXY(x,y);
repaint();
}
}
If you need more of my code, please let me know and I will edit the question and add it.
Any help is appreciated
Since you set selected to null it cannot work.
What happens if you change mousePressed to (note that it will only work if there is only one fruit at (x,y):
public void mousePressed(MouseEvent event)
{
//Get x and y
int x = event.getX();
int y = event.getY();
//set selected to null
selected = null;
//if a fruit contains x, y then selected is assigned that fruit
for( Fruit m : fruits)
{
if(m.contains(x,y))
{
selected = m;
// position is set in mouseDrag
}
}
In mouseReleased you then set selected back to null (optional).

Trouble flipping images in Java

So I have to use java.awt.color to flip some imported images.
Here is my primary method to achieve flipping an image over the vertical axis:
public void execute (Pixmap target)
{
Dimension bounds = target.getSize();
// TODO: mirror target image along vertical middle line by swapping
// each color on right with one on left
for (int x = 0; x < bounds.width; x++) {
for (int y = 0; y < bounds.height; y++) {
// new x position
int newX = bounds.width - x - 1;
// flip along vertical
Color mirrorVert = target.getColor(newX, y);
target.setColor(x, y, new Color (mirrorVert.getRed(),
mirrorVert.getGreen(),
mirrorVert.getBlue()));
}
}
}
However, when this executes, instead of the image, flipping, I get something like this:
Original:
"Flipped":
Thank y'all for the help
// flip along vertical
Color mirrorVert = target.getColor(newX, y);
target.setColor(x, y, new Color (mirrorVert.getRed(),
mirrorVert.getGreen(),
mirrorVert.getBlue()));
target.setColor(newX, y , new Color(255,255,255));
}
This should work in theory. Basically the idea is to set the Colors on the right side to white, while copying it over to the left side.
Actually this will not work... So what you could do is to save the new colors in an array. Then you can make everything white and put the swapped colors back.
for (int i=0;i<image.getWidth();i++)
for (int j=0;j<image.getHeight();j++)
{
int tmp = image.getRGB(i, j);
image.setRGB(i, j, image.getRGB(i, image.getHeight()-j-1));
image.setRGB(i, image.getHeight()-j-1, tmp);
}
That one looks promising.

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.

Color Grid: figure out which box was clicked

My question concerns the Color Grid teechart. I'm trying to write code so that when the user touches a box on the grid, only that box color will change. I find the using chart.getHeight() and chart.getWidth() give the whole area of the chart and not just the color grid dimensions. So right now, I'm estimating the length and width of the grid in pixels to estimate the box that the user touched. Is there any way that I can figure out the exact amount of pixels of just the color grid length and height? Additionally, I noticed a "clicked" method in the Color Grid api. Is there anything already built-in that would allow me to find which box the user touched/clicked? Thanks!
I've made a simple example that uses the seriesClicked event. However, it seems to give a correct index but the cell colored seems to be in the opposite row:
oldIndex = -1;
tChart1.getAspect().setView3D(false);
tChart1.getLegend().setVisible(false);
ColorGrid col1 = new ColorGrid(tChart1.getChart());
col1.setColorEach(true);
for (int x=0; x<10; x++)
for (int z=0; z<10; z++)
col1.add(x, 1, z, Color.random());
col1.addSeriesMouseListener(new SeriesMouseAdapter() {
#Override
public void seriesClicked(SeriesMouseEvent e) {
ColorGrid myGrid = (ColorGrid)tChart1.getSeries(0);
int valueIndex = myGrid.clicked(e.getPoint().x, e.getPoint().y);
if (valueIndex > -1) {
if (oldIndex > -1) {
myGrid.getColors().setColor(oldIndex, oldColor);
}
oldIndex = valueIndex;
oldColor = myGrid.getValueColor(valueIndex);
myGrid.getColors().setColor(valueIndex, Color.red);
tChart1.getHeader().setText(String.valueOf(valueIndex));
tChart1.getSeries(0).repaint();
}
}
});
I'll add to the defect list the need to revise the clicked function for the ColorGrid (TJ71016603)
In the meanwhile, a workaround could be to modify the given valueIndex as follows:
if (valueIndex > -1) {
valueIndex = valueIndex / myGrid.getNumZValues() * myGrid.getNumZValues() + (myGrid.getNumZValues()-1 - (valueIndex % myGrid.getNumZValues()));
if (oldIndex > -1) {
//...
}

Categories