Stop player movement when releasing a key - java

I am making a game in Java, and I am working on player movement, but whenever a press a directional key, the player moves but doesn't stop when I release the key.
I can't seem to see what I am doing wrong. May someone point me in the right direction?
// Input class implements KeyListener
public static final int LEFT = 0;
public static final int RIGHT = 1;
public static final int UP = 2;
public static final int DOWN = 3;
public boolean[] keys = new boolean[64];
public void getKeys(KeyEvent e, boolean move) {
switch(e.getKeyCode()) {
case KeyEvent.VK_LEFT:
keys[LEFT] = move;
break;
case KeyEvent.VK_RIGHT:
keys[RIGHT] = move;
break;
case KeyEvent.VK_UP:
keys[UP] = move;
break;
case KeyEvent.VK_DOWN:
keys[DOWN] = move;
break;
case KeyEvent.VK_BACK_QUOTE:
keys[BQUOTE] = move;
break;
}
keys[OTHER] = move;
}
#Override
public void keyPressed(KeyEvent e) {
getKeys(e, true);
}
#Override
public void keyReleased(KeyEvent e) {
getKeys(e, false);
}
#Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
// Player Class
public void tick() {
if (Engine.key_input.keys[KInput.LEFT]) {
dx = -speed;
animDir = SpriteLoader.L;
sprite = SpriteLoader.anim(SpriteLoader.ANIM_PLAYER, animDir);
}
if (Engine.key_input.keys[KInput.RIGHT]){
dx = speed;
animDir = SpriteLoader.R;
sprite = SpriteLoader.anim(SpriteLoader.ANIM_PLAYER, animDir);
}
if (Engine.key_input.keys[KInput.UP]){
dy = -speed;
animDir = SpriteLoader.U;
sprite = SpriteLoader.anim(SpriteLoader.ANIM_PLAYER, animDir);
}
if (Engine.key_input.keys[KInput.DOWN]){
dy = speed;
animDir = SpriteLoader.D;
sprite = SpriteLoader.anim(SpriteLoader.ANIM_PLAYER, animDir);
}
}
// Engine Class
public class Engine {
public Input key_input;
public Engine() {
key_input = new Input();
}
// add keyListener(Input) to component
}

This is not enough info from you. But this is suspicious:
public boolean[] keys = new boolean[64]
Where is the code that says, "I've used this keys value, now I want to ignore it next time"?

In your tick method, you only ever check for a "pressed" state, you never reset the movement delta
For example...
public void tick() {
if (Engine.key_input.keys[KInput.LEFT]) {
dx = -speed;
} else {
dx = 0;
}
I would make determinations of animDir and sprite based on the dx and dy variables after you've updated the deltas.
I would also check the left and right, and up and down in a single if block...
For example
if (Engine.key_input.keys[KInput.LEFT]) {
dx = -speed;
} else if (Engine.key_input.keys[KInput.RIGHT]) {
dx = speed;
} else{
dx = 0;
}
This ensures that you don't accidentally reset the movement

In your handler, you are only ever setting the keys[] = true, but you are not ever returning them back to false.
You need to add a keyReleased() method that will un-move all keys that have been released.

Related

Tracking multiple touch movement

I have a custom view which detects user touch and draws a circle when screen is touched. Different circles for different fingers.And circle is cleared when user removes the finger. Now i have to move the circle while moving the fingers.
i can only move 1 circle while moving fingers. IF i have more than 1 finger on screen, how can i move all the circle in the finger direction?
public boolean onTouchEvent(MotionEvent event) {
int pointerIndex = event.getActionIndex();
int pointerId = event.getPointerId(pointerIndex);
int maskedAction = event.getActionMasked();
switch (maskedAction){
case MotionEvent.ACTION_DOWN: {
Pointers pointer = new Pointers();
pointer.setX(event.getX(pointerIndex));
pointer.setY(event.getY(pointerIndex));
pointer.setPaint(getPaint());
pointersList.put(pointerId,pointer);
break;
}
case MotionEvent.ACTION_POINTER_DOWN: {
Pointers pointer = new Pointers();
pointer.setX(event.getX(pointerIndex));
pointer.setY(event.getY(pointerIndex));
pointer.setPaint(getPaint());
pointersList.put(pointerId,pointer);
break;
}
case MotionEvent.ACTION_MOVE:{
int size = event.getPointerCount();
Pointers point = pointersList.get(pointerId);
if (point != null) {
point.setX(event.getX(pointerIndex));
point.setY(event.getY(pointerIndex));
}
}
break;
case MotionEvent.ACTION_UP:{
pointersList.remove(pointerId);
}
case MotionEvent.ACTION_POINTER_UP: {
pointersList.remove(pointerId);
break;
}
case MotionEvent.ACTION_CANCEL:{
pointersList.remove(pointerId);
break;
}
}
invalidate();
return true;
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
int size = pointersList.size();
for(int i=0; i<size;i++){
Pointers point = pointersList.get(i);
if(point != null){
canvas.drawCircle(point.getX(),point.getY(),SIZE,point.getPaint());
}
}
}
Pointer Class
public class Pointers {
private float x,y;
private Paint paint;
public float getX() {
return x;
}
public void setX(float x) {
this.x = x;
}
public float getY() {
return y;
}
public void setY(float y) {
this.y = y;
}
public Paint getPaint() {
return paint;
}
public void setPaint(Paint paint) {
this.paint = paint;
}
}
I think the problem is in the draw method.
From what I can tell pointerList is a Map that uses the pointer ID as the key.
In the draw method you're trying to get all the pointer that have the key in the range 0 to total pointers - 1 which is incorrect. You're trying to get the pointers by index but they were stored by ID.
Try this:
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
for(Map.Entry<Integer, Pointers> entry : pointersList.entrySet()){
Pointers point = entry.getValue();
canvas.drawCircle(point.getX(),point.getY(),SIZE,point.getPaint());
}
}

How do I keep an object moving using arrow keys?

I am making a snake game, and I want my snake to be moving continuously once a key is pressed. So, I press the down key, and it keeps moving even if the key is released. Right now, it just moves while the key is being held down.
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_DOWN) {
mySegment[0].moveSouth();
repaint();
}
else if (e.getKeyCode() == KeyEvent.VK_UP) {
mySegment[0].moveNorth();
repaint();
}
else if(e.getKeyCode() == KeyEvent.VK_LEFT){
mySegment[0].moveWest();
repaint();
}
else if (e.getKeyCode() == KeyEvent.VK_RIGHT){
mySegment[0].moveEast();
repaint();
}
for (int a = 0; a < 10; a++) {
if (myFruit[a].distance (mySegment[0].getX(), mySegment[0].getY())
<= 20) {
myFruit[a].hide();
}
}
The "mySegment [0]" is the snake, and the "moveSouth" or whatever direction just moves it 5 pixels in that directin
Use a "game loop" to drive the animation. Since this looks to be possibly a Swing or AWT GUI, then your best bet is to use a Swing Timer -- please check out the tutorial. The gist is that within the Timer's ActionListener you increment the position of the snake, changing its direction depending on the state of your key press.
I would use an enum to indicate Direction {UP, DOWN, LEFT, RIGHT}:
public enum Direction {
UP,
DOWN,
LEFT,
RIGHT
}
AND then a Map<Direction, Boolean> to indicate which direction to head:
private Map<Direction, Boolean> dirMap = new EnumMap<>(Direction.class);
Elsewhere you would initialize the Map to hold false in all values:
// initialize the map to all false
for (Direction dir : Direction.values()) {
dirMap.put(dir, false);
}
Then change the state of the items in the Map within your code for listening to key presses and releases -- call map.put(Direction.UP, true) for instance when the up key is pressed, and likewise call map.put(Direction.UP, false) when it has been released, same for the other keys. Note that if yours is a Swing application, I'd use Key Bindings and not a KeyListener to do this. In the listener, I'd then call repaint() on the GUI.
Within the Swing Timer, iterate through the Map, setting the direction based on the state of the Map.
class fields:
private int spriteX = 0; // location of sprite
private int spriteY = 0;
private int directionX = 0; // direction sprite is heading
private int directionY = 0;
Within the ActionListener
// within the Swing Timer's ActionListener
if (dirMap.get(Direction.UP)) {
directionY -= 1;
}
if (dirMap.get(Direction.DOWN)) {
directionY += 1;
}
if (dirMap.get(Direction.RIGHT)) {
directionX += 1;
}
if (dirMap.get(Direction.LEFT)) {
directionY -= 1;
}
// here multiply directionX and directionY by some scale factor and use to place new snake head
// then call repaint();
For example (not a snake but a ball -- I'll leave the Snake to you)
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.EnumMap;
import java.util.Map;
import java.util.Map.Entry;
import javax.swing.*;
#SuppressWarnings("serial")
public class DirTest extends JPanel {
private static final int PREF_W = 800;
private static final int PREF_H = PREF_W;
private static final int TIMER_DELAY = 40;
private static final Color SPRITE_COLOR = Color.RED;
private static final int SPRITE_W = 20;
private static final Color BG = Color.BLACK;
public static final int SCALE = 1;
private Map<Direction, Boolean> dirMap = new EnumMap<>(Direction.class);
private int spriteX = 0;
private int spriteY = 0;
private int directionX = 0;
private int directionY = 0;
private Timer gameLoopTimer = new Timer(TIMER_DELAY, new TimerListener());
public DirTest() {
setKeyBindings();
setBackground(BG);
// initialize map to all 0;
for (Direction dir : Direction.values()) {
dirMap.put(dir, false);
}
gameLoopTimer.start();
}
private void setKeyBindings() {
int condition = WHEN_IN_FOCUSED_WINDOW; // bind to keys if component in active window
InputMap inputMap = getInputMap(condition);
ActionMap actionMap = getActionMap();
setKeyBinding(inputMap, actionMap, KeyEvent.VK_UP, Direction.UP);
setKeyBinding(inputMap, actionMap, KeyEvent.VK_DOWN, Direction.DOWN);
setKeyBinding(inputMap, actionMap, KeyEvent.VK_LEFT, Direction.LEFT);
setKeyBinding(inputMap, actionMap, KeyEvent.VK_RIGHT, Direction.RIGHT);
}
private void setKeyBinding(InputMap inputMap, ActionMap actionMap, int keyCode, Direction dir) {
KeyStroke press = KeyStroke.getKeyStroke(keyCode, 0, false);
KeyStroke released = KeyStroke.getKeyStroke(keyCode, 0, true);
Action pressAction = new PressedAction(dir, true);
Action releasedAction = new PressedAction(dir, false);
inputMap.put(press, press.toString());
inputMap.put(released, released.toString());
actionMap.put(press.toString(), pressAction);
actionMap.put(released.toString(), releasedAction);
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.setColor(SPRITE_COLOR);
g2.fillOval(spriteX, spriteY, SPRITE_W, SPRITE_W);
}
#Override
public Dimension getPreferredSize() {
if (isPreferredSizeSet()) {
return super.getPreferredSize();
}
return new Dimension(PREF_W, PREF_H);
}
private class PressedAction extends AbstractAction {
private boolean pressed;
private Direction dir;
public PressedAction(Direction dir, boolean pressed) {
this.dir = dir;
this.pressed = pressed;
}
#Override
public void actionPerformed(ActionEvent e) {
dirMap.put(dir, pressed);
}
}
private class TimerListener implements ActionListener {
#Override
public void actionPerformed(ActionEvent e) {
for (Entry<Direction, Boolean> entry : dirMap.entrySet()) {
if (entry.getValue()) {
directionX += entry.getKey().getX();
directionY += entry.getKey().getY();
}
}
spriteX += SCALE * directionX;
spriteY += SCALE * directionY;
repaint();
}
}
private static void createAndShowGui() {
JFrame frame = new JFrame("DirTest");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new DirTest());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> createAndShowGui());
}
}
enum Direction {
UP(0, -1),
DOWN(0, 1),
LEFT(-1, 0),
RIGHT(1, 0);
private int x;
private int y;
private Direction(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
}
If you want to keep the snake moving you need some kind of game-loop (as mentioned before). The easiest way is having a single field containing the current direction, and set it based on the input. So here are a few methods/classes you could use to set the correct direction/position
The Direction Enum
public enum Direction
{
NORTH, EAST, SOUTH, WEST;
public Direction oposite()
{
switch(this)
{
case NORTH: return SOUTH;
case SOUTH: return NORTH;
case EAST: return WEST;
case WEST: return EAST;
}
}
}
The method to set the current direction. (This assumes there is a field named 'currentDirection' which contains an enum (Direction) representing the current direction)
public void setDirection(Direction newDirection)
{
if(currentDirection != newDirection.oposite())
currentDirection = newDirection;
}
This is somewhere in your game loop (either an timer, or a while loop including a 'sleep' call to prevent CPU hugging)
switch(currentDirection)
{
case NORTH: mySegment[0].moveNorth(); break;
case EAST: mySegment[0].moveEast(); break;
case SOUTH: mySegment[0].moveSouth(); break;
case WEST: mySegment[0].moveWest(); break;
}
repaint();
And of course instead of calling 'mySegment[0].moveNorth();' or someting equalivant in the actionHandlers for the keyEvents, you should only call 'setDirection();' to make the snake move.
I hope this helped you out.

Java slime volleyball image flickering

I have started making slime volleyball. I have run into this problem where the image for the slime character flickers insanely. the image is disappeared just about the same amount of time that it is visible.
I tried removing the super.paint(g); in the paint method and that fixed the problem of flickering, but it created a new problem that wouldn't remove the image from previous locations.
Source Code:
package slimevolleyball.main;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
public class Main extends JFrame {
private static final long serialVersionUID = 1L;
public static int x = 275;
public static int y = 300;
public static boolean right = false;
public static boolean left = false;
public static boolean jump = false;
public static int startjump = 0;
public static int low = 300;
public static double gravity = 0;
public static double time = 0;
public static int startVelocity = 0;
public static double velocity = 0;
public static BufferedImage slime1;
public static BufferedImage slime2;
public static BufferedImage background;
static Rectangle FrameSize;
static JFrame frame = new JFrame();
public Main() {
addKeyListener(new KeyListener() {
public void keyTyped(KeyEvent e) {
}
public void keyReleased(KeyEvent e) {
int keyCode = e.getKeyCode();
switch (keyCode) {
case KeyEvent.VK_UP:
break;
case KeyEvent.VK_DOWN:
break;
case KeyEvent.VK_LEFT:
left = false;
break;
case KeyEvent.VK_RIGHT:
right = false;
}
}
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
switch (keyCode) {
case KeyEvent.VK_UP:
if (jump == false) {
jump = true;
startjump = 1;
}
break;
case KeyEvent.VK_DOWN:
break;
case KeyEvent.VK_LEFT:
left = true;
right = false;
break;
case KeyEvent.VK_RIGHT:
right = true;
left = false;
break;
}
}
});
setFocusable(true);
setTitle("Slime VolleyBall");
setSize(600, 400);
setResizable(true);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void paint(Graphics g) {
super.paint(g);
g.drawImage(slime1, x, y, null);
}
public static void main(String[] args) throws InterruptedException, IOException {
Main game = new Main();
slime1 = ImageIO.read(new File("red.png"));
while (true) {
MoveSlime1();
Update();
game.repaint();
Thread.sleep(2);
}
}
private static void Update() {
FrameSize = frame.getBounds();
}
private static void MoveSlime1() {
if (right == true && x <= FrameSize.getWidth() / 2) {
x += 1;
} else if (left == true && x >= 0) {
x -= 1;
}
if (jump == true) {
if (startjump == 1) {
low = 300;
gravity = -9.8;
time = 0;
startVelocity = 3;
velocity = 0;
startjump = 0;
} else {
velocity = startVelocity + (gravity * time);
y -= velocity;
time += 0.005;
if (time > 0.01 && y >= low) {
jump = false;
}
}
}
}
}
JFrame is not double buffered, hence the flicker, which is just one of a list of reasons why you should not be extending from JFrame and overriding it's paint method.
Instead, move all your logic over to a class which extends JPanel and then override it's paintComponent method, this way, you'll get double buffering for free.
Also, you should avoid using KeyListeners and favor the Key Bindings API which solves the focus related issues of KeyListener
Also, static is not your friend, you should learn to live without it

Failing to try to change an image when keypressed

I'm trying to make it so when I press down the right key a new picture pops up making it look like my character is walking, not quite sure how to do that though... Here's my code:
import java.awt.*;
public class Dude {
int x, dx, y;
Image still;
public Dude() {
ImageIcon i = new ImageIcon("Ken3.png");
ImageIcon ii = new ImageIcon("KenTurn1.png");
still = i.getImage();
x = 50;
y = 785;
}
public void move() {
x = x + dx;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public Image getImage() {
return still;
}
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_A)
dx = -2;
if (key == KeyEvent.VK_D)
dx = 2;
if (key == KeyEvent.VK_SPACE)
dx = 5;
}
public void keyReleased(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_A)
dx = 0;
if (key == KeyEvent.VK_D)
dx = 0;
Any help would be awesome thanks!
something like this, i commented it. Good luck
public class Dude {
private int walkingIndex;
private Image walkingFrames;
private Image still;
private String state;
public Dude() {
// say were starting off standing
state = "standing";
// init our still frame
still = (new ImageIcon("Ken3.png")).getImage();
// set our walking frame to 0 to start
walkingIndex = 0;
walkingFrames = new Image[3]; // or however many images you have in your walking animation
// go through each frame and initialize the image to KenWalking{index}.png
for(int i=0;i<walkingFrames.length;i++) {
walkingFrames[i] = (new ImageIcon("KenWalking"+i+".png")).getImage();
}
}
public void move() {
x+=dx;
// add 1 to the walking index and make sure its not greater than our number of walking frames, % means get the remainder of
// so 12 % 10 = 2, and 8 % 7 = 1
walkingIndex = (walkingIndex+1)%walkingFrames.length;
}
public Image getImage() {
// if our state is walking then give out a walking frame
if(state.equals("walking")) {
return walkingFrames[walkingIndex];
} else {
// otherwise we can assume were standing, so show that image instead
return still;
}
}
public void keyPressed(KeyEvent e) {
... same stuff
state = "walking";
}
public void keyReleased(KeyEvent e) {
... same stuff
state = "standing";
}
}
If i understand the question right, you are looking for a keyListener. In that case, you can do two things. Add keybindings or add an KeyListener.
Please have in mind that i haven't tested the code on your template, it work well for me in an AWT frame. Hope it helps
First implement the EventListener to Dude
public class Dude implements KeyListener{ ...
For the KeyListener to work you must have a focusable component..
component.addKeyListener(this);
component.setFocusable(true);
component.setFocusTraversalKeysEnabled(false);
The method below will be implemented with the KeyListener
#Override
public void keyPressed(KeyEvent e) {
int x = e.getKeyCode();
switch(x){
case KeyEvent.VK_UP:
// Do Your stuff
break;
case KeyEvent.VK_DOWN:
// Do Your stuff
break;
case KeyEvent.VK_RIGHT:
// Do Your stuff
break;
case KeyEvent.VK_LEFT:
// Do Your stuff
break;
}
}

My boolean method java

I've done quite a bit of searching for an answer to this, and some editing and I cant get it to work. I think there's something wrong with my boolean method, getColor(). Im trying to use it in a loop from a different class.
herse my getColor method, from the AvatarPanel class
public boolean setColor(boolean good)
{
if (good == true)
what--;
avatars.get(0).setBackground(colors.get(what));
repaint();
if (good == false)
what++;
avatars.get(0).setBackground(colors.get(what));
repaint();
return good;
}
And here's the attackPerformed method from the Background class that Im trying to use getColor in. Im not sure if I wrote one of the methods wrong, or if attackPerformed needs a listener. The game is still running normally and working fine. I just wanted some help making sure these were at least written correctly.
I decided it might be a good idea to show the background constructor because I think its where I need to call attackPerformed. I can't figure out where it is that I need to call the attackPerformed, though I'm pretty sure it'll be in the background constructor. The attackPerformed method is shown below, constructor is at top.
public class Background extends JPanel implements Constants, ActionListener
{
private static final long serialVersionUID = 1L;
private final Color BACK_COLOR = Color.GRAY;
private ArrayList<Sprite> sprites;
private Avatar avatar;
/** Constructor */
public Background()
{
sprites = new ArrayList<Sprite>();
reset(0);
setBackground(BACK_COLOR);
KeyboardFocusManager manager =KeyboardFocusManager.getCurrentKeyboardFocusManager();
manager.addKeyEventDispatcher(new KeyEventDispatcher()
{
public boolean dispatchKeyEvent(KeyEvent event)
{
int x = 0, y = 0;
double angle = 0;
Point p = avatar.getPosition();
switch (event.getKeyCode())
{
case KeyEvent.VK_DOWN:
y=6;
angle=270;
break;
case KeyEvent.VK_UP:
y = -6;
angle = 90;
break;
case KeyEvent.VK_LEFT:
x = -6;
break;
case KeyEvent.VK_RIGHT:
x = 6;
angle = 180;
break;
}
avatar.setParameters();
p.x += x;
p.y += y;
avatar.setPosition(p);
avatar.setAngle(angle);
if (!getBounds().contains(avatar.getBounds()))
{
avatar.restore();
Toolkit.getDefaultToolkit().beep();
return true;
}
repaint();
return true;
}
});
Timer timer = new Timer(15, this);
timer.start();
} //end of constructor
public void attackPerformed()
{
for(Sprite s : sprites)
{
AvatarPanel panel = new AvatarPanel();
Rectangle r = new Rectangle(sprites.get(0).getBounds());
Rectangle p = new Rectangle(sprites.get(1).getBounds());
if(r.intersects(p))
{
panel.setColor(false);
}
}
}
public void actionPerformed(ActionEvent e)
{
for (Sprite s : sprites)
{
if (s instanceof Move)
{
s.setParameters();
for (int i=0; i<3; i++)
{
((Move)s).nextPosition();
if (!getBounds().contains(s.getBounds()))
{
((Move) s).moveFailed();
}
else break;
}
}
}
repaint();
}
public #Override void paintComponent(Graphics g)
{
super.paintComponent(g); /* Let the parent class do its painting */
for (Sprite sprite: sprites)
{
sprite.draw(g);
}
}
public void reset(int level)
{
Sprite sprite = null;
int x, y, angle;
double scale, min, max;
sprites.clear();
int[] figures = FIGURES[level];
for (int f=0; f<figures.length; f++)
{
for (int i=0; i<figures[f]; i++)
{
x = (int)(Math.random()*600 + 100);
y = (int)(Math.random()*400 + 100);
switch (f)
{
case 0:
sprite = avatar = new Avatar(x,y);
break;
case 1:
sprite = new Predator(x,y);
break;
case 2:
sprite = new Mine(x,y);
break;
case 3:
sprite = new Shield(x,y);
break;
}
angle = (int)(Math.random()*360);
sprite.setAngle(angle);
min = sprite.getMinScaleFactor();
max = sprite.getMaxScaleFactor();
scale = Math.random() * (max - min) + min;
sprite.setScale(scale);
sprites.add(sprite);
repaint();
}
}
}
}
Looks like your if and else case are very similar.
You can simplify it to this.
public boolean setColor(boolean good)
{
what+=good?-1:+1;
avatars.get(0).setBackground(colors.get(what));
repaint();
    return good;
}
I would strongly recommend to use brackets, as the method repaint() would execute outside of if statement. Since, boolean can only have two possible values you can use 'else' instead of second if...
public boolean setColor(boolean good)
{
if (good == true)
{
what--;
avatars.get(0).setBackground(colors.get(what));
repaint();
}
else
{
what++;
avatars.get(0).setBackground(colors.get(what));
repaint();
}

Categories

Resources