This site requires JavaScript, please enable it in your browser!
Greenfoot back
NIcolasGomez
NIcolasGomez wrote ...

2014/11/6

Changing color of my ball

NIcolasGomez NIcolasGomez

2014/11/6

#
How can I change the color of my ball? I want it to change when it touches the Paddle?
NIcolasGomez NIcolasGomez

2014/11/6

#
it's a Pong - lookalike
NIcolasGomez NIcolasGomez

2014/11/6

#
it's a Pong - lookalike
Alwin_Gerrits Alwin_Gerrits

2014/11/6

#
I don't think you will be able to only adjust the color of your ball. Seen all images used in Greenfoot have a square form if you change the color of your image it will the entire square with that color (I think). You can try if you want. Try this
setColor(Color.something); 
Obviously change 'something' by the name of the color, like 'blue' or 'black'. Don't put any quotation marks around them though! But as I was saying before it can be that simply changing the color of the image is impossible seen greenfoot sees the image only as a square with some colors. If I were you I would just make a new image using a picture program (if you don't have one try 'gimp'). It will allso be a lot less work when talking about the amount you have to program and the possible bugs you have to fix.
Alwin_Gerrits Alwin_Gerrits

2014/11/6

#
Just a clarification of what should work if you still want to try altering the image codewise:
GreenfootImage ball = new GreenfootImage("ball.png");
ball.setColor(Color.blue);
You may be able to leave the 'new' part seen you're using an already used image, but that could result in bugs or errors
danpost danpost

2014/11/7

#
danpost danpost

2014/11/7

#
Strange -- my post is hidden. Try 'Quote' to view. Oh -- found an unwanted open square bracket that was causing problems. @Alwin_Gerrits, (1) the 'setColor' method does not alter the image, it only sets the drawing color of that image for future drawing operations; (2) there are multiple methods for altering or drawing shapes on an image in the currently set color of the image ('clear', 'drawLine', 'drawRect', 'drawOval', 'drawPolygon', 'fill', 'fillRect', 'fillOval', and 'fillPolygon'); there is also a 'drawImage', method to draw one image onto another; with all but the 'clear' and 'fill' methods, you specify where on the image the operation is to be executed; @NicolasGomez, how you change the color of the image would be determined by how you initially set the image. That is, if you programmatically create the image, then you would do the same to change it; if you set the image to an image in an image file, then you probably have image files for the other colors and you would again, do similarly.
davmac davmac

2014/11/7

#
Alwin_Gerrits wrote...
Seen all images used in Greenfoot have a square form if you change the color of your image it will the entire square with that color (I think).
There is no need to guess at what methods in Greenfoot will do; you just have to check the documentation. In this case: public void setColor(java.awt.Color color) Set the current drawing color. This color will be used for subsequent drawing operations.
NIcolasGomez NIcolasGomez

2014/11/9

#
I didn't make the image in greenfoot i have images on my computer I use "Ball" this is the ball that you see when you play. And Ball2 this is the same ball but with another color. How do I do this?
danpost danpost

2014/11/9

#
You do not need a separate class for each color of the ball. Just change the image of the one Ball actor to change its color. Use the 'setImage' method supplying the String name of the file to change the image to. If you want the ball to alternate between the two colors as it hits the paddle, then hold both images in instance fields and set the image of the ball to one of them in the Ball class constructor. Then, when the ball hits the paddle, use the 'if' condition 'getImage() == image1' to determine which image to set (on 'true' result, set image to 'image2' else set image to 'image1'). 'image1' and 'image2' are the names I gave the fields that hold the references to the two images. If you have more than two images, it would be best to use an array and an int field to track which element in the array is currently used.
You need to login to post a reply.