I'm creating a Frogger-like game and my sprite(frog)(image), if you will, is created but does not move on my keypress, and I believe the error is occurring somewhere in the draw() method for the actual frog.java class. This is my own project and I'm trying to incorporate polymorphism and inheritance. Is there a way to move my sprite using the draw and move methods that I have, else what would be the correct way to do so. This is being done in Netbeans unfortunately.
import java.awt.*;
import java.awt.event.*;
public class FroggerForm extends Frame implements ActionListener
{
private Frog frog;
private javax.swing.Timer moveTimer = new javax.swing.Timer(500, this);
/**
Creates new form FroggerForm
*/
public FroggerForm()
{
initComponents();
this.setSize(400, 400);
frog = new Frog(froggerPanel);
moveTimer.start();
}
/**
This method is called from within the constructor to initialize the form.
WARNING: Do NOT modify this code. The content of this method is always
regenerated by the Form Editor.
*/
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents()
{
froggerPanel = new java.awt.Panel();
setMinimumSize(new java.awt.Dimension(500, 500));
addWindowListener(new java.awt.event.WindowAdapter()
{
public void windowClosing(java.awt.event.WindowEvent evt)
{
exitForm(evt);
}
});
setLayout(null);
froggerPanel.setBackground(new java.awt.Color(190, 190, 190));
froggerPanel.setPreferredSize(new java.awt.Dimension(400, 400));
froggerPanel.addKeyListener(new java.awt.event.KeyAdapter()
{
public void keyPressed(java.awt.event.KeyEvent evt)
{
keyAdapter(evt);
}
});
add(froggerPanel);
froggerPanel.setBounds(100, 0, 400, 400);
pack();
}// </editor-fold>
/**
Exit the Application
*/
private void exitForm(java.awt.event.WindowEvent evt) {
System.exit(0);
}
private void keyAdapter(java.awt.event.KeyEvent evt)
{
frog.hide();
if(evt.getSource() == KeyEvent.VK_UP)
frog.move(0, -10);
else if(evt.getSource() == KeyEvent.VK_DOWN)
frog.move(0, 10);
else if(evt.getSource() == KeyEvent.VK_RIGHT)
frog.move(10, 0);
else if(evt.getSource() == KeyEvent.VK_LEFT)
frog.move(-10 , 0);
}
/**
#param args the command line arguments
*/
public static void main(String args[])
{
java.awt.EventQueue.invokeLater(new Runnable()
{
public void run()
{
new FroggerForm().setVisible(true);
}
});
}
// Variables declaration - do not modify
private java.awt.Panel froggerPanel;
// End of variables declaration
#Override
public void actionPerformed(ActionEvent ae)
{
frog.draw();
frog.move();
}
}
public abstract class PFigure implements Comparable
{
protected int x, y; // Current position of the figure
protected int width, height; // Drawn (displayed) this size
protected int priority; // Can use to determine "winner"
protected Panel panel; // Panel the figure lives on
public PFigure ( int startX, int startY, int _width, int _height,
int pr, Panel p )
{
x = startX;
y = startY;
width = _width;
height = _height;
priority = pr;
panel = p;
}
// Can use this in "battles", which figures is "greater"
public int compareTo(Object o)
{
if( o instanceof PFigure )
return priority - ((PFigure)o).priority;
return Integer.MAX_VALUE;
}
// Has "this" figure collided with p?
public boolean collidedWith ( PFigure p )
{
if ( p == null )
return false;
return ( x + width ) >= p.x && ( p.x + p.width ) >= x &&
( y + height ) >= p.y && ( p.y + p.height ) >= y;
}
// Can be used for moving by keyboard or mouse
public void move ( int deltaX, int deltaY )
{
x = x + deltaX;
y = y + deltaY;
}
public void hide()
{
Graphics g = panel.getGraphics();
Color oldColor = g.getColor();
g.setColor(panel.getBackground() );
g.fillRect(x, y, width, height);
g.setColor(oldColor);
}
// Can be automatic move, for example, called based on timer
public void move()
{
}
// Draw the figure.
// Each derived class will write their own drawing method.
// The first line should be:
// Graphics g = panel.getGraphics();
abstract public void draw();
}
import java.awt.*;
import java.io.File;
import javax.imageio.ImageIO;
public class Frog extends PFigure
{
private Image frogImg;
public Frog(Panel p)
{
super(200, 350, 30, 30, 0, p);
try
{
File file = new File("frogger.png");
frogImg = ImageIO.read(file);
}
catch ( Exception e )
{
System.out.println("Crashing: " + e);
}
}
#Override
public void draw()
{
if(frogImg != null)
{
Graphics g = panel.getGraphics();
g.drawImage(frogImg, x, y, width, height, null);
}
}
#Override
public void move(int deltaX, int deltaY)
{
if ( x <= 0)
x = 1;
else if(x >= panel.getSize().width)
x = panel.getSize().width - 1;
else if(y >= panel.getSize().height)
y = panel.getSize().height - 1;
x += deltaX;
y += deltaY;
}
}
I believe your first error is in the use of KeyListener.
KeyListener will only respond to key strokes when the component it is registered to has focus AND is focusable.
Use Key Bindings instead, they allow you to define the focus level where key events will be generated
Updated
As an example...
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.imageio.ImageIO;
import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class WizardFireball {
public static void main(String[] args) {
new WizardFireball();
}
public WizardFireball() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new GamePane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class GamePane extends JPanel {
private GameModel model;
public GamePane() {
try {
model = new DefaultModel();
ActionMap am = getActionMap();
InputMap im = getInputMap(WHEN_IN_FOCUSED_WINDOW);
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0, false), "up-pressed");
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0, false), "down-pressed");
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0, false), "left-pressed");
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0, false), "right-pressed");
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0, true), "up-released");
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0, true), "down-released");
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0, true), "left-released");
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0, true), "right-released");
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0), "fire");
am.put("up-pressed", new KeyPressedAction(getModel(), Key.UP));
am.put("down-pressed", new KeyPressedAction(getModel(), Key.DOWN));
am.put("left-pressed", new KeyPressedAction(getModel(), Key.LEFT));
am.put("right-pressed", new KeyPressedAction(getModel(), Key.RIGHT));
am.put("up-released", new KeyReleasedAction(getModel(), Key.UP));
am.put("down-released", new KeyReleasedAction(getModel(), Key.DOWN));
am.put("left-released", new KeyReleasedAction(getModel(), Key.LEFT));
am.put("right-released", new KeyReleasedAction(getModel(), Key.RIGHT));
am.put("fire", new FireBallAction(getModel()));
} catch (IOException ex) {
ex.printStackTrace();
}
Timer timer = new Timer(40, new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
for (Asset asset : getModel().getAssets()) {
asset.update(GamePane.this);
}
repaint();
}
});
timer.start();
}
public GameModel getModel() {
return model;
}
#Override
public Dimension getPreferredSize() {
return new Dimension(400, 200);
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
for (Asset asset : getModel().getAssets()) {
asset.paint(this, g2d);
}
g2d.dispose();
}
}
public enum Key {
LEFT,
RIGHT,
UP,
DOWN
}
public interface GameModel {
public void keyPressed(Key key);
public void keyReleased(Key key);
public boolean isKeyPressed(Key key);
public void addFireball();
public void remove(Asset asset);
public Wizard getWizard();
public Iterable<Asset> getAssets();
}
public class DefaultModel implements GameModel {
private Set<Key> keys;
private Wizard wizard;
private List<Asset> assets;
public DefaultModel() throws IOException {
keys = new HashSet<>(25);
assets = new ArrayList<>(25);
wizard = new Wizard();
}
#Override
public void keyPressed(Key key) {
keys.add(key);
}
#Override
public void keyReleased(Key key) {
keys.remove(key);
}
#Override
public boolean isKeyPressed(Key key) {
return keys.contains(key);
}
#Override
public Wizard getWizard() {
return wizard;
}
#Override
public void addFireball() {
try {
assets.add(new Fireball(getWizard()));
} catch (IOException ex) {
ex.printStackTrace();
}
}
#Override
public void remove(Asset asset) {
assets.remove(asset);
}
#Override
public Iterable<Asset> getAssets() {
List<Asset> proxy = new ArrayList<>(assets);
proxy.add(wizard);
return proxy;
}
}
public abstract class AbstractGameAction extends AbstractAction {
private GameModel model;
public AbstractGameAction(GameModel model) {
this.model = model;
}
public GameModel getModel() {
return model;
}
}
public abstract class AbstractKeyAction extends AbstractGameAction {
private Key key;
public AbstractKeyAction(GameModel model, Key key) {
super(model);
this.key = key;
}
public Key getKey() {
return key;
}
}
public class KeyPressedAction extends AbstractKeyAction {
public KeyPressedAction(GameModel model, Key key) {
super(model, key);
}
#Override
public void actionPerformed(ActionEvent e) {
getModel().keyPressed(getKey());
}
}
public class KeyReleasedAction extends AbstractKeyAction {
public KeyReleasedAction(GameModel model, Key key) {
super(model, key);
}
#Override
public void actionPerformed(ActionEvent e) {
getModel().keyReleased(getKey());
}
}
public class FireBallAction extends AbstractGameAction {
public FireBallAction(GameModel model) {
super(model);
}
#Override
public void actionPerformed(ActionEvent e) {
getModel().addFireball();
}
}
public interface Asset {
public void paint(GamePane surface, Graphics2D g2d);
public void update(GamePane surface);
public Rectangle getBounds();
}
public class Fireball implements Asset {
private final BufferedImage fireball;
private final Rectangle bounds;
public Fireball(Wizard wizard) throws IOException {
fireball = ImageIO.read(getClass().getResource("/Fireball.png"));
bounds = new Rectangle();
bounds.x = wizard.getBounds().x + wizard.getBounds().width;
bounds.y = wizard.getBounds().y + (wizard.getBounds().height - fireball.getHeight());
bounds.setSize(fireball.getWidth(), fireball.getHeight());
}
#Override
public Rectangle getBounds() {
return bounds;
}
#Override
public void paint(GamePane surface, Graphics2D g2d) {
Rectangle bounds = getBounds();
g2d.drawImage(fireball, bounds.x, bounds.y, surface);
}
#Override
public void update(GamePane surface) {
Rectangle bounds = getBounds();
bounds.x += 8;
if (bounds.x > surface.getWidth()) {
surface.getModel().remove(this);
}
}
}
public class Wizard implements Asset {
private final BufferedImage wizard;
private final Rectangle bounds;
private boolean initialised = false;
public Wizard() throws IOException {
wizard = ImageIO.read(getClass().getResource("/Wizard.png"));
bounds = new Rectangle(wizard.getWidth(), wizard.getHeight());
}
#Override
public Rectangle getBounds() {
return bounds;
}
#Override
public void paint(GamePane surface, Graphics2D g2d) {
Rectangle bounds = getBounds();
Point point = bounds.getLocation();
if (!initialised) {
point = new Point(5, (surface.getHeight() - bounds.height) / 2);
bounds.setLocation(point);
initialised = true;
}
g2d.drawImage(wizard, point.x, point.y, surface);
}
#Override
public void update(GamePane surface) {
Rectangle bounds = getBounds();
Point point = bounds.getLocation();
if (initialised) {
GameModel model = surface.getModel();
if (model.isKeyPressed(Key.UP)) {
point.y -= 4;
} else if (model.isKeyPressed(Key.DOWN)) {
point.y += 4;
}
if (point.y < 0) {
point.y = 0;
} else if (point.y + bounds.height > surface.getHeight()) {
point.y = surface.getHeight() - bounds.height;
}
bounds.setLocation(point);
}
}
}
}
Related
I have this code, that the oval shape should automatically move to the right/left/up/down (it depends on decision from the user) while implementing the runnable class. However it does not move. I would really appreciate any advice. Thank you in advance.
public class AnimationPanel extends JPanel implements Runnable{
public int getSpeed() {
return speed;
}
public void setSpeed(int speed) {
this.speed = speed;
}
public boolean isLeftToRight() {
return isLeftToRight;
}
public void setLeftToRight(boolean isLeftToRight) {
this.isLeftToRight = isLeftToRight;
}
private int x;
private int y;
private boolean isPaused = false;
private int width;
private int hwight;
private int speed=10;
private Thread animThread;
private boolean isLeftToRight = true;
public AnimationPanel() {
setBackground(Color.WHITE);
setDoubleBuffered(true);
x = 0;
y = 0;
animThread = new Thread(this);
animThread.start();
}
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.BLUE);
g2.fillOval(x, y,20,20);
}
public void movement() {
hwight = this.getHeight();
width = this.getWidth();
if(isLeftToRight) {
if(x<width) {
x=+10;
} else if(x>=0){
x=-10;
}
} else {
if(y<hwight) {
y=+10;
} else if(y>=0){
y=-10;
}
}
}
#Override
public void run() {
while(true) {
if (!isPaused)
{
movement();
repaint();
}
try {
Thread.sleep(speed);
} catch(InterruptedException e) {
JOptionPane.showMessageDialog(null, "Interrupted");
}
}
}
}
public class AppFrame extends JFrame implements ActionListener{
private AnimationPanel anim;
private JButton slowerButton;
private JButton fasterButton;
private JButton upDownButton;
private JButton leftRightButton;
private JPanel buttonsPanel;
private int height = 500;
private int width = 500;
public AppFrame() {
this.setTitle("Circle");
this.setSize(height,width);
this.setResizable(false);
initGui();
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
private void initGui() {
buttonsPanel = new JPanel();
this.setLayout(new BorderLayout());
buttonsPanel.setLayout(new FlowLayout());
buttonsPanel.add(slowerButton = new JButton("Slower"));
buttonsPanel.add(fasterButton = new JButton("Faste"));
buttonsPanel.add(upDownButton = new JButton("up and down"));
buttonsPanel.add(leftRightButton = new JButton("left to right"));
slowerButton.addActionListener(this);
fasterButton.addActionListener(this);
upDownButton.addActionListener(this);
leftRightButton.addActionListener(this);
anim = new AnimationPanel();
this.add(buttonsPanel, BorderLayout.PAGE_START);
this.add(anim);
}
#Override
public void actionPerformed(ActionEvent e) {
Object zrodlo = e.getSource();
if(slowerButton == zrodlo) {
anim.setSpeed(anim.getSpeed()*2);
} else if(fasterButton == zrodlo) {
anim.setSpeed(anim.getSpeed()/2);
} else if(upDownButton == zrodlo) {
anim.setLeftToRight(false);
} else if(leftRightButton == zrodlo) {
anim.setLeftToRight(true);
}
}
}
public class Runner {
public static void main(String[] args) {
AppFrame app =new AppFrame();
app.setVisible(true);
}
}
What's going wrong...
if (x < width) {
x = +10;
} else if (x >= 0) {
x = -10;
}
So, the above code is simply assigning either -10 or +10 to the x variable, it never increments/decrements the value. Use += and -= instead
if (x < width) {
x += 10;
} else if (x >= 0) {
x -= 10;
}
This will solve the immediate issue, but create a new one. It would be better to have a simple delta which was either positive or negative and is then simply applied to the variable
x += delta; // +/- speed
if (x + 20 >= width) {
x = width - 20;
delta *= -1
} ...
Additional fixes
Swing is not thread, you should not be using threads to change the state of the UI. Start by having a look at Concurrency in Swing and How to Use Swing Timers for more details.
You should also have a look at Painting in AWT and Swing and Performing Custom Painting as you should be preferring paintComponent over paint
The following is a rewrite of your example using a Swing Timer
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JToggleButton;
import javax.swing.Timer;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
JFrame frame = new JFrame();
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
protected class TestPane extends JPanel {
private AnimationPanel animationPane;
public TestPane() {
setLayout(new BorderLayout());
animationPane = new AnimationPanel();
add(animationPane);
JToggleButton pauseButton = new JToggleButton("Pause");
pauseButton.setSelected(true);
pauseButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
animationPane.setPaused(pauseButton.isSelected());
}
});
JButton fasterButton = new JButton("Faster");
fasterButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
animationPane.setSpeed(animationPane.getSpeed() * 2);
}
});
JButton slowerButton = new JButton("Slower");
slowerButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
animationPane.setSpeed(animationPane.getSpeed() / 2);
}
});
JToggleButton horizontalButton = new JToggleButton("Horizontal");
JToggleButton verticalButton = new JToggleButton("Vertical");
horizontalButton.setSelected(true);
ButtonGroup bg = new ButtonGroup();
bg.add(horizontalButton);
bg.add(verticalButton);
horizontalButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
animationPane.setLeftToRight(horizontalButton.isSelected());
}
});
verticalButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
animationPane.setLeftToRight(!verticalButton.isSelected());
}
});
JPanel actionPane = new JPanel();
actionPane.add(pauseButton);
actionPane.add(slowerButton);
actionPane.add(fasterButton);
actionPane.add(horizontalButton);
actionPane.add(verticalButton);
add(actionPane, BorderLayout.SOUTH);
}
}
public class AnimationPanel extends JPanel {
public int getSpeed() {
return speed;
}
public void setSpeed(int speed) {
this.speed = speed;
}
public boolean isLeftToRight() {
return isLeftToRight;
}
public void setLeftToRight(boolean isLeftToRight) {
this.isLeftToRight = isLeftToRight;
}
private boolean paused = true;
private int speed = 10;
private boolean isLeftToRight = true;
private Point origin = new Point(190, 190);
private Timer timer;
public AnimationPanel() {
timer = new Timer(16, new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
movement();
repaint();
}
});
}
#Override
public Dimension getPreferredSize() {
return new Dimension(400, 400);
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
g2d.setColor(Color.BLUE);
g2d.fillOval(origin.x, origin.y, 20, 20);
g2d.dispose();
}
public boolean isPaused() {
return paused;
}
public void setPaused(boolean paused) {
this.paused = paused;
if (paused) {
timer.stop();
} else {
timer.start();
}
}
public void movement() {
int height = this.getHeight();
int width = this.getWidth();
if (isLeftToRight) {
origin.x += speed;
if (origin.x + 20 >= width) {
speed *= -1;
origin.x = width - 20;
} else if (origin.x <= 0) {
speed *= -1;
origin.x = 0;
}
} else {
origin.y += speed;
if (origin.y + 20 >= height) {
speed *= -1;
origin.y = height - 20;
} else if (origin.y <= 0) {
speed *= -1;
origin.y = 0;
}
}
}
}
}
A "different" approach
I don't like delta based animations, I think they are shorted sighted and they generally produce bad results. Where possible, I prefer to make use of time based animations. That is, based on a given time, move the object over a given range.
When done right, this is really flexible. It allows the system to drop frames automatically without the animation "stalling" and generally produces nicer animation which is generally easier to manage and maintained.
This does, however, introduce more complexity, but if you spend the time to generalise the workflows, they can be re-used relatively easily.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import java.time.Duration;
import java.time.Instant;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JToggleButton;
import javax.swing.Timer;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
JFrame frame = new JFrame();
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
protected class TestPane extends JPanel {
private AnimationPanel animationPane;
public TestPane() {
setLayout(new BorderLayout());
animationPane = new AnimationPanel();
add(animationPane);
JToggleButton pauseButton = new JToggleButton("Run");
pauseButton.setSelected(true);
pauseButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
if (pauseButton.isSelected()) {
pauseButton.setText("Run");
} else {
pauseButton.setText("Pause");
}
animationPane.setPaused(pauseButton.isSelected());
}
});
// JButton fasterButton = new JButton("Faster");
// fasterButton.addActionListener(new ActionListener() {
// #Override
// public void actionPerformed(ActionEvent e) {
// animationPane.setSpeed(animationPane.getSpeed() * 2);
// }
// });
// JButton slowerButton = new JButton("Slower");
// slowerButton.addActionListener(new ActionListener() {
// #Override
// public void actionPerformed(ActionEvent e) {
// animationPane.setSpeed(animationPane.getSpeed() / 2);
// }
// });
JToggleButton horizontalButton = new JToggleButton("Horizontal");
JToggleButton verticalButton = new JToggleButton("Vertical");
horizontalButton.setSelected(true);
ButtonGroup bg = new ButtonGroup();
bg.add(horizontalButton);
bg.add(verticalButton);
// horizontalButton.addActionListener(new ActionListener() {
// #Override
// public void actionPerformed(ActionEvent e) {
// animationPane.setLeftToRight(horizontalButton.isSelected());
// }
// });
//
// verticalButton.addActionListener(new ActionListener() {
// #Override
// public void actionPerformed(ActionEvent e) {
// animationPane.setLeftToRight(!verticalButton.isSelected());
// }
// });
JPanel actionPane = new JPanel();
actionPane.add(pauseButton);
// actionPane.add(slowerButton);
// actionPane.add(fasterButton);
// actionPane.add(horizontalButton);
// actionPane.add(verticalButton);
add(actionPane, BorderLayout.SOUTH);
}
}
public class AnimationPanel extends JPanel {
public enum Direction {
VERTICAL, HORIZONTAL
}
private Direction direction = Direction.HORIZONTAL;
private Point2D origin = new Point2D.Double(200, 200);
private Animator animator;
private Range<Double> range;
private Duration duration = Duration.ofSeconds(5);
private Ellipse2D dot = new Ellipse2D.Double(0, 0, 20, 20);
public AnimationPanel() {
animator = new Animator(new Animator.Observer() {
#Override
public void animatorDidTick(Animator animator, double progress) {
double nextValue = range.valueAt(progress);
if (direction == Direction.HORIZONTAL) {
origin.setLocation(nextValue, origin.getY());
}
repaint();
}
#Override
public void animatorDidComplete(Animator animator) {
double targetPoint = range.getTo();
if (direction == Direction.HORIZONTAL) {
range = getDotHorizontalRange();
if (targetPoint != range.getFrom()) {
range.reverse();
}
}
animator.setDuration(duration);
resume();
}
});
}
#Override
public Dimension getPreferredSize() {
return new Dimension(400, 400);
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
g2d.setColor(Color.BLUE);
g2d.drawRect(0, 0, getWidth() - 1, getHeight() - 1);
g2d.draw(new Line2D.Double(0, getHeight() / 2, getWidth(), getHeight() / 2));
g2d.draw(new Line2D.Double(getWidth() / 2, 0, getWidth() / 2, getHeight()));
g2d.translate(origin.getX() - (dot.getWidth() / 2d), origin.getY() - (dot.getHeight() / 2d));
g2d.fill(dot);
g2d.dispose();
}
protected Range<Double> getDotHorizontalRange() {
return new DoubleRange(dot.getWidth() / 2, getWidth() - (dot.getWidth() / 2));
}
protected double getHorizontalRangeDistance() {
return ((DoubleRange)getDotHorizontalRange()).getDistance();
}
public void setPaused(boolean paused) {
if (paused) {
animator.pause();
} else {
if (range == null) {
initialiseRange();
}
animator.resume();
}
}
protected void resume() {
if (range == null) {
// Try and force a restart...
setPaused(false);
}
animator.resume();
}
protected void initialiseRange() {
if (direction == Direction.HORIZONTAL) {
double currentX = origin.getX();
// Assume a positive intial direction
double avaliableRange = Math.abs(getHorizontalRangeDistance());
double distance = avaliableRange - currentX;
int remainingTime = (int)(duration.toMillis() * (distance / avaliableRange));
animator.setDuration(Duration.ofMillis(remainingTime));
range = new DoubleRange((double)currentX, getDotHorizontalRange().getTo());
}
}
}
public abstract class Range<T> {
private T from;
private T to;
public Range(T from, T to) {
this.from = from;
this.to = to;
}
public T getFrom() {
return from;
}
public T getTo() {
return to;
}
#Override
public String toString() {
return "From " + getFrom() + " to " + getTo();
}
public abstract T valueAt(double progress);
public void reverse() {
T nextFrom = to;
to = from;
from = nextFrom;
}
}
public class DoubleRange extends Range<Double> {
public DoubleRange(Double from, Double to) {
super(from, to);
}
public Double getDistance() {
return getTo() - getFrom();
}
#Override
public Double valueAt(double progress) {
double distance = getDistance();
double value = distance * progress;
value += getFrom();
return value;
}
}
public class Animator {
public enum State {
STOP, PAUSE, RUN
}
public interface Observer {
public void animatorDidTick(Animator animator, double progress);
public void animatorDidComplete(Animator animator);
}
private Duration duration = Duration.ofSeconds(5);
// Used to manage pause support. This will be
// added onto the "live" runtime when the
// animator is running
private Duration previousRuntime = Duration.ZERO;
private Instant epoch;
private Observer observer;
private State state = State.STOP;
// This is actually used to manage the "ticks"
private Timer ticker = new Timer(5, new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
if (epoch == null) {
epoch = Instant.now();
}
double progress = getProgressAtCurrentTime();
observer.animatorDidTick(Animator.this, Math.max(0, Math.min(1.0, progress)));
if (progress >= 1.0) {
progress = 1.0;
stop();
observer.animatorDidComplete(Animator.this);
}
}
});
public Animator(Observer observer) {
this.observer = observer;
}
public void setDuration(Duration duration) {
this.duration = duration;
}
public boolean isPaused() {
return state == State.PAUSE;
}
public boolean isRunning() {
return state == State.RUN;
}
public boolean isStopped() {
return state == State.STOP;
}
public void pause() {
ticker.stop();
if (epoch != null) {
Duration runtime = Duration.between(epoch, Instant.now());
previousRuntime = previousRuntime.plus(runtime);
state = State.PAUSE;
}
epoch = null;
}
public void resume() {
state = State.RUN;
ticker.start();
}
protected double getProgressAtCurrentTime() {
Duration runtime = Duration.ZERO;
if (epoch != null) {
// The delta time between when we started and now
runtime = Duration.between(epoch, Instant.now());
}
// Plus any additonal time which was recored
runtime = runtime.plus(previousRuntime);
return runtime.toMillis() / (double) duration.toMillis();
}
// This is for internal reset purposes
protected void stop() {
ticker.stop();
state = State.STOP;
previousRuntime = Duration.ZERO;
epoch = null;
}
}
}
The above example makes use of a concept of "normalised" time. That is, any given animation transitions over a time range of 0-1. This makes it incredibly easy to change the speed. Want to to go faster? Decrease the duration. Slower? Increase the duration. Everything else is done by simply calculating the required properties against a "from" and "to" state and the current "normalised time" value.
For example, look at the animation above. The dot starts at the halfway point, but the time it takes to get to the other side is no different then the time it takes to return ALL the way to far side (the speed doesn't change), this is because, the initial duration is calculated based on a delta of the whole range and the current position (that is, the initial duration is 50% of the desired duration).
The above example also allows you to "pause" the running animation and when it resumes, it will continue as if nothing happened.
Animation, I mean, really good animation, is complicated. Don't believe me, take a look at move dot between 2 points jframe (which I've spent the last few days building out as personal projects to run in Java and MacOS, because apparently I don't have a live) and How can I implement easing functions with a thread which takes a deep dive into just one aspect of the animation theory and don't even get me started on "time line" animation.
I am currently doing a new project and long story short I am trying to implement action listener on a rectangle object so it can be moved up and down.
But here the code completely crashes when making an object of well in this case test2 class in the test3 class (yes I am aware of the need for a uppercase for classes). As I have replicated the problem making a new project and just used the code that is needed.
public class test {
test2 board = new test2();
public void frame() {
JFrame b = new JFrame("test");
b.setSize(905,705);
b.setLocation(300,60);
b.setResizable(false);
b.setVisible(true);
b.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
b.add(board);
}
public static void main(String[] args) {
test start = new test();
start.frame();
}
}
public class test2 extends JPanel {
public int playerScore = 0;
public int opponentScore = 0;
test3 player = new test3();
public void paint(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
board(g);
g.setColor(Color.WHITE);
g2d.fill(player.player);
}
public void board(Graphics g) {
g.setColor(Color.black);
g.fillRect(0, 0, 900, 900);
}
}
public class test3 {
public boolean down = false;
public boolean up = false;
public int playerXpos = 45;
public int playerYpos = 300;
public int playerWidth = 15;
public int playerHeight = 80;
Rectangle player = new Rectangle(playerXpos, playerYpos, playerWidth, playerHeight);
test2 theBoard = new test2();
public void actionPerformed(ActionEvent e) {
if (down) {
down = true;
if (up != true) {
down = true;
} else {
up = true;
down = false;
}
}
if (up) {
up = true;
if (down != true) {
up = true;
} else {
up = false;
down = true;
}
}
}
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_UP) {
for (int r = playerYpos; r >=0; r--) {
if (r == 0) {
playerYpos = playerYpos - 20;
} else {
playerYpos = playerYpos - 1;
}
if (playerYpos < 50) {
playerYpos = 50;
}
}
theBoard.repaint();
}
if (e.getKeyCode() == KeyEvent.VK_DOWN) {
for (int r = playerYpos; r >= 0; r--) {
if (r == 0) {
playerYpos = playerYpos + 20;
} else {
playerYpos = playerYpos - 1;
}
if (playerYpos > 800){
playerYpos = 800;
}
}
theBoard.repaint();
}
}
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
}
Your code doesn't compile.
I took some of your code and created the following GUI.
Here's the GUI after moving the player rectangle.
Oracle has a helpful tutorial, Creating a GUI With JFC/Swing. Skip the Netbeans section.
The first thing I did was start the Swing application with a call to the SwingUtilities invokeLater method. This method ensures that the Swing components are created and executed on the Event Dispatch Thread.
I created a Player class to hold the player rectangle. This way, you can change the player shape in one place.
I created a JFrame. The JFrame methods have to be called in a specific order. This is the order I use for my Swing applications.
I created a drawing JPanel. This JPanel is similar to yours, except I override the paintComponent method. I pass an instance of Player to the drawing JPanel so the JPanel can draw the player rectangle.
I used key bindings instead of a key listener. This makes the keys functional whether the drawing JPanel is in focus or not. Key bindings also made it easy for me to add the WASD keys as well as the arrow keys.
Here's the complete runnable code.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;
public class ExampleGUI implements Runnable {
public static void main(String[] args) {
SwingUtilities.invokeLater(new ExampleGUI());
}
private final DrawingPanel drawingPanel;
private final Player player;
public ExampleGUI() {
this.player = new Player();
this.drawingPanel = new DrawingPanel(player);
}
#Override
public void run() {
JFrame frame = new JFrame("Example GUI");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(drawingPanel, BorderLayout.CENTER);
frame.pack();
frame.setLocationByPlatform(true);
setKeyBindings(drawingPanel);
frame.setVisible(true);
}
private void setKeyBindings(JPanel panel) {
String up = "up";
String down = "down";
String left = "left";
String right = "right";
InputMap inputMap = panel.getInputMap();
ActionMap actionMap = panel.getActionMap();
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0, false), up);
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0, false), down);
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0, false), left);
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0, false), right);
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_W, 0, false), up);
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_S, 0, false), down);
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_A, 0, false), left);
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_D, 0, false), right);
actionMap.put(up, new PlayerAction(this, player, 0, -10));
actionMap.put(down, new PlayerAction(this, player, 0, +10));
actionMap.put(left, new PlayerAction(this, player, -10, 0));
actionMap.put(right, new PlayerAction(this, player, +10, 0));
}
public void repaint() {
drawingPanel.repaint();
}
public class DrawingPanel extends JPanel {
private static final long serialVersionUID = 1L;
private final Player player;
public DrawingPanel(Player player) {
this.player = player;
this.setPreferredSize(new Dimension(600, 600));
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
drawBoard(g2d);
drawPlayer(g2d);
}
private void drawBoard(Graphics2D g2d) {
g2d.setColor(Color.BLACK);
g2d.fillRect(0, 0, getWidth(), getHeight());
}
private void drawPlayer(Graphics2D g2d) {
g2d.setColor(player.getColor());
g2d.fill(player.getPosition());
}
}
public class PlayerAction extends AbstractAction {
private static final long serialVersionUID = 1L;
private final ExampleGUI frame;
private final Player player;
private final Point moveDirection;
public PlayerAction(ExampleGUI frame, Player player, int x, int y) {
this.frame = frame;
this.player = player;
this.moveDirection = new Point(x, y);
}
#Override
public void actionPerformed(ActionEvent event) {
player.move(moveDirection);
frame.repaint();
}
}
public class Player {
private final Color color;
private Rectangle position;
public Player() {
this.color = Color.WHITE;
this.position = new Rectangle(300, 300, 20, 80);
}
public void move(Point point) {
position.x += point.x;
position.y += point.y;
}
public Color getColor() {
return color;
}
public Rectangle getPosition() {
return position;
}
}
}
I have a project that a circle goes with random x and y values and with selected colors but when the user pressed the space bar the color of the circle must be changed. My circle moves both x and y coordinate and I want to change the color of the circle when I press the space button. But it does not work when I pressed it. It goes with its original color. So how can I make this code right?
public class c {
private int x,y,r;
private Color co;
private int Re,G,B;
private Random ran;
public c() {
// TODO Auto-generated constructor stub
ran= new Random();
x=100;
y=50;
r= ran.nextInt(200)+50;
Re=ran.nextInt(255);
G=ran.nextInt(255);
B=ran.nextInt(255);
co= new Color(Re,G,B);
}
public int getRe() {
return Re;
}
public int getG() {
return G;
}
public int getB() {
return B;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public int getR() {
return r;
}
public void setCo(int Re,int G,int B) {
co= new Color(Re,G,B);
}
public Color getCo() {
return co;
}
public Random getRan() {
return ran;
}
public void setX(int x) {
this.x=x;
}
public void setY(int y) {
this.y=y;
}
}
public class Circle extends JFrame implements ActionListener,KeyListener{
private Timer timer;
private int x,y,a=5,b=5;
private Random rand;
c circ = new c();
public Circle() {
setLayout(new BorderLayout());
x=circ.getX();
y=circ.getY();
timer=new Timer(50,this);
timer.start();
addKeyListener(this);
setSize(550,550);
setVisible(true);
}
public void paint(Graphics g) {
super.paint(g);
g.fillOval(x,y,100,100);
g.setColor(circ.getCo());
}
public static void main(String[]args) {
new Circle();
}
#Override
public void actionPerformed(ActionEvent e) {
moveWithTimer();
repaint();
}
public void moveWithTimer() {
x=x+b;
y=y+a;
if(x<0) {
b=5;
}
if(x+50>500) {
b=-5;
}
if(y<0){
a=5;
}
if(y+50>500) {
a=-5;
}
}
#Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
if(e.getKeyCode()==e.VK_SPACE) {
circ.setCo(rand.nextInt(255),rand.nextInt(255),rand.nextInt(255));
}
}
#Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
#Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
}
But it does not work when I pressed it. It goes with its original color. So how can I make this code right?
KeyListener is fickle, better to use the Key Bindings API which overcomes the primary, focus related, issues of KeyListener
As a general rule of thumb, you shouldn't override paint of top level containers like JFrame, they are compound components and it's just a real mess.
Instead, start with a JPanel and override it's paintComponent method. It's generally more flexible. Have a look at Performing Custom Painting for more details.
Your movement code is wrong. You assign the x/y values of the circle class to some other variables, the problem here is, changing the values of these variables will have no affect on the variables in you circle class, instead, you need assign them back...
public void moveWithTimer() {
int x = circ.getX();
int y = circ.getY();
x = x + b;
y = y + a;
if (x < 0) {
b = 5;
}
if (x + 50 > 500) {
b = -5;
}
if (y < 0) {
a = 5;
}
if (y + 50 > 500) {
a = -5;
}
circ.setX(x);
circ.setY(y);
}
Your "circle" class could also use a couple of additional methods. One to randomise the color (it already has a Random object, might as well use it) and one to paint the object.
public class Circle {
//...
public void paint(Graphics2D g2d) {
g2d.setColor(co);
g2d.fillOval(x, y, r * 2, r * 2);
}
//...
public void randomColor() {
setCo(ran.nextInt(255), ran.nextInt(255), ran.nextInt(255));
}
//...
}
If it was me, I'd be tempted to add a move method as well, but that's me ;)
As a runnable example...
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.Random;
import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private Timer timer;
private int a = 5, b = 5;
private Random rand;
private Circle circ = new Circle();
public TestPane() {
timer = new Timer(50, new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
moveWithTimer();
repaint();
}
});
timer.start();
InputMap im = getInputMap(WHEN_IN_FOCUSED_WINDOW);
ActionMap am = getActionMap();
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0), "spaced");
am.put("spaced", new AbstractAction() {
#Override
public void actionPerformed(ActionEvent e) {
circ.randomColor();
repaint();
}
});
}
public void moveWithTimer() {
int x = circ.getX();
int y = circ.getY();
x = x + b;
y = y + a;
if (x < 0) {
b = 5;
}
if (x + 50 > 500) {
b = -5;
}
if (y < 0) {
a = 5;
}
if (y + 50 > 500) {
a = -5;
}
circ.setX(x);
circ.setY(y);
}
#Override
public Dimension getPreferredSize() {
return new Dimension(500, 500);
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
circ.paint(g2d);
g2d.dispose();
}
}
public class Circle {
private int x, y, r;
private Color co;
private int Re, G, B;
private Random ran;
public Circle() {
// TODO Auto-generated constructor stub
ran = new Random();
x = 100;
y = 50;
r = ran.nextInt(50) + 50;
Re = ran.nextInt(255);
G = ran.nextInt(255);
B = ran.nextInt(255);
co = new Color(Re, G, B);
}
public void paint(Graphics2D g2d) {
g2d.setColor(co);
g2d.fillOval(x, y, r * 2, r * 2);
}
public int getRe() {
return Re;
}
public int getG() {
return G;
}
public int getB() {
return B;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public int getR() {
return r;
}
public void randomColor() {
setCo(ran.nextInt(255), ran.nextInt(255), ran.nextInt(255));
}
public void setCo(int Re, int G, int B) {
co = new Color(Re, G, B);
}
public Color getCo() {
return co;
}
public Random getRan() {
return ran;
}
public void setX(int x) {
this.x = x;
}
public void setY(int y) {
this.y = y;
}
}
}
I suggest you should set the color of the graphics object within paint(g) before painting the circle.
public void paint(Graphics g) {
super.paint(g);
g.setColor(circ.getCo());
g.fillOval(x,y,100,100);
}
In general, you should not override the paint() method of the JFrame. Instead, create a JPanel, add it to your frame and override the paintComponent() method of the panel.
I have a problem with repaint() method in my Java code. I want to call it in another class but I can't, something doesn't work at all. I've searched on forums, but nothing was able to help me out.
My Main class:
public class Main {
public static Main main;
public static JFrame f;
public Main(){
}
public static void main(String[] args) {
main = new Main();
f = new JFrame();
Ball b = new Ball();
f.getContentPane().setBackground(Color.GRAY);
f.add(b);
f.setSize(500, 500);
f.setLocationRelativeTo(null);
f.setTitle("Test");
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.addMouseMotionListener(b);
f.addKeyListener(new Key());
}
}
Ball class where I created 2DGraphics for moving shapes:
public class Ball extends JLabel implements MouseMotionListener{
public Ball(){
}
public static double x = 10;
public static double y = 10;
public static double width = 40;
public static double height = 40;
String nick;
boolean isEllipse = true;
public void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
if(isEllipse){
Ellipse2D e2d = new Ellipse2D.Double(x, y, width, height);
g2d.setColor(Color.RED);
g2d.fill(e2d);
}
else{
Rectangle2D r2d = new Rectangle2D.Double(x, y, width, height);
g2d.setColor(Color.GREEN);
g2d.fill(r2d);
}
}
#Override
public void mouseDragged(MouseEvent e) {
isEllipse = false;
x = e.getX() - 30;
y = e.getY() - 40;
this.repaint();
}
#Override
public void mouseMoved(MouseEvent e) {
x = e.getX() - 30;
y = e.getY() - 40;
isEllipse = true;
this.repaint();
}
}
And Key class where I put KeyListener for move the shapes by key pressing:
public class Key extends Ball implements KeyListener {
public Key() {
}
#SuppressWarnings("static-access")
#Override
public void keyPressed(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_W){
super.x += 10;
super.repaint();
System.out.println("x: " + super.x);
}
}
#Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
#Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
}
But something is wrong with this code: super method doesn't work for Key class. Everything in Ball class is working well. Where is the problem?
Super works fine, but your interpretation of what it does is wrong. Your problem is that you're trying to use inheritance to solve a problem that isn't solved with inheritance. You need to call repaint() on the actual visualized and used Ball instance, not on an instance of some completely different class, Key, that inappropriately extends from Ball. First off, make Key not extend Ball, pass in a true Ball reference into Key and the solution will fall from that.
Perhaps do something like this:
f.addKeyListener(new Key(b));
and
public class Key implements KeyListener {
private Ball ball;
public Key(Ball ball) {
this.ball = ball;
}
public void keyPressed(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_W){
b.incrX(10); // give Ball a public method for this
b.repaint();
// System.out.println("x: " + super.x);
}
}
// .... etc...
Note, myself, I'd use Key Bindings for this, not a KeyListener, since then I wouldn't have to futz with keyboard focus.
For example:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.RenderingHints;
import java.awt.event.*;
import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;
public class MoveBall {
private static final int PREF_W = 500;
private static final int PREF_H = PREF_W;
private static void createAndShowGui() {
BallPanel ballPanel = new BallPanel(PREF_W, PREF_H);
MyMouse myMouse = new MyMouse(ballPanel);
ballPanel.addMouseListener(myMouse);
ballPanel.addMouseMotionListener(myMouse);
new CreateKeyBindings(ballPanel);
JFrame frame = new JFrame("MoveBall");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(ballPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
createAndShowGui();
});
}
}
#SuppressWarnings("serial")
class BallPanel extends JPanel {
private static final Color ELLIPSE_COLOR = Color.RED;
private static final Color SQUARE_COLOR = Color.GREEN;
private static final int BALL_WIDTH = 40;
private int prefW;
private int prefH;
private boolean isEllipse = true;
private int ballX;
private int ballY;
public BallPanel(int prefW, int prefH) {
this.prefW = prefW;
this.prefH = prefH;
}
public boolean isEllipse() {
return isEllipse;
}
public void setEllipse(boolean isEllipse) {
this.isEllipse = isEllipse;
}
public int getBallX() {
return ballX;
}
public void setBallX(int ballX) {
this.ballX = ballX;
}
public void setXY(int x, int y) {
ballX = x;
ballY = y;
repaint();
}
public void setXYCenter(int x, int y) {
ballX = x - BALL_WIDTH / 2;
ballY = y - BALL_WIDTH / 2;
repaint();
}
public void setXYCenter(Point p) {
setXYCenter(p.x, p.y);
}
public int getBallY() {
return ballY;
}
public void setBallY(int ballY) {
this.ballY = ballY;
}
public void incrementBallX(int x) {
ballX += x;
repaint();
}
public void incrementBallY(int y) {
ballY += y;
repaint();
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
if (isEllipse) {
g2.setColor(ELLIPSE_COLOR);
g2.fillOval(ballX, ballY, BALL_WIDTH, BALL_WIDTH);
} else {
g2.setColor(SQUARE_COLOR);
g2.fillOval(ballX, ballY, BALL_WIDTH, BALL_WIDTH);
}
}
#Override
public Dimension getPreferredSize() {
if (isPreferredSizeSet()) {
return super.getPreferredSize();
}
return new Dimension(prefW, prefH);
}
}
class MyMouse extends MouseAdapter {
private BallPanel ballPanel;
public MyMouse(BallPanel ballPanel) {
this.ballPanel = ballPanel;
}
#Override
public void mousePressed(MouseEvent e) {
ballPanel.setXYCenter(e.getPoint());
}
#Override
public void mouseDragged(MouseEvent e) {
ballPanel.setXYCenter(e.getPoint());
}
#Override
public void mouseReleased(MouseEvent e) {
ballPanel.setXYCenter(e.getPoint());
}
}
enum Direction {
UP(KeyEvent.VK_UP), DOWN(KeyEvent.VK_DOWN), LEFT(KeyEvent.VK_LEFT), RIGHT(KeyEvent.VK_RIGHT);
private int key;
private Direction(int key) {
this.key = key;
}
public int getKey() {
return key;
}
}
// Actions for the key binding
#SuppressWarnings("serial")
class MyKeyAction extends AbstractAction {
private static final int STEP_DISTANCE = 5;
private BallPanel ballPanel;
private Direction direction;
public MyKeyAction(BallPanel ballPanel, Direction direction) {
this.ballPanel = ballPanel;
this.direction = direction;
}
#Override
public void actionPerformed(ActionEvent e) {
switch (direction) {
case UP:
ballPanel.incrementBallY(-STEP_DISTANCE);
break;
case DOWN:
ballPanel.incrementBallY(STEP_DISTANCE);
break;
case LEFT:
ballPanel.incrementBallX(-STEP_DISTANCE);
break;
case RIGHT:
ballPanel.incrementBallX(STEP_DISTANCE);
break;
default:
break;
}
}
}
class CreateKeyBindings {
private BallPanel ballPanel;
public CreateKeyBindings(BallPanel ballPanel) {
this.ballPanel = ballPanel;
int condition = JComponent.WHEN_IN_FOCUSED_WINDOW;
InputMap inputMap = ballPanel.getInputMap(condition);
ActionMap actionMap = ballPanel.getActionMap();
for (Direction direction : Direction.values()) {
KeyStroke keyStroke = KeyStroke.getKeyStroke(direction.getKey(), 0);
String keyString = keyStroke.toString();
inputMap.put(keyStroke, keyString);
actionMap.put(keyString, new MyKeyAction(ballPanel, direction));
}
}
}
I am making a game and I ran into a key ghosting problem (where the program only detects one keypress at a time, so the player can't go diagonally). I was watching this tutorial: https://www.youtube.com/watch?v=5UaEUrbpDPE
I followed everything they said and it still only detects one key at a time.
Main:
public class Main extends JApplet implements Runnable, KeyListener {
private static final long serialVersionUID = 1L;
public static int width = 900;
public static int height = 600;
public static int fps = 60;
public static Main instance;
public static Ailoid ailoid = new Ailoid();
public static Player player = new Player();
// Initialize
public void init() {
setSize(width, height);
setBackground(Color.white);
addKeyListener(this);
setFocusable(true);
setFocusTraversalKeysEnabled(false);
requestFocus();
instance = this;
ailoid.setLocation(new Location(100, 100));
AlienManager.registerAlien(ailoid);
player.setLocation(new Location(400, 400));
}
// Paint graphics
public void paint(Graphics g) {
super.paint(g);
paintComponent(g);
}
public void paintComponent(Graphics g) {
for (Alien alien : AlienManager.getAliens()) {
Location loc = alien.getLocation();
g.setColor(Color.GREEN);
g.fillRect(loc.getX(), loc.getY(), 10, 25);
}
g.setColor(Color.BLUE);
Location loc = Main.player.getLocation();
g.fillRect(loc.getX(), loc.getY(), 10, 25);
}
// Thread start
#Override
public void start() {
Thread thread = new Thread(this);
thread.start();
}
// Thread stop
#Override
public void destroy() {
}
// Thread run
#Override
public void run() {
Thread thread = new Thread(this);
while (thread != null) {
Updater.run();
repaint();
try {
// 1000 divided by fps to get frames per second
Thread.sleep(1000 / fps);
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
}
#Override
public void keyPressed(KeyEvent evt) {
if (!KeysDown.get().contains(evt.getKeyCode()))
KeysDown.add(new Integer(evt.getKeyCode()));
KeyPress.run(evt);
}
#Override
public void keyReleased(KeyEvent evt) {
KeysDown.remove(new Integer(evt.getKeyCode()));
}
#Override
public void keyTyped(KeyEvent evt) {
}
}
KeysDown:
public class KeysDown {
private static ArrayList<Integer> keysDown = new ArrayList<Integer>();
public static ArrayList<Integer> get() {
return keysDown;
}
public static void add(Integer key) {
keysDown.add(key);
}
public static void remove(Integer key) {
keysDown.remove(key);
}
}
KeyPress:
public class KeyPress {
public static void run(KeyEvent evt) {
if (KeysDown.get().contains(KeyEvent.VK_RIGHT)) {
Main.player.moveRight();
}
else if (KeysDown.get().contains(KeyEvent.VK_LEFT)) {
Main.player.moveLeft();
}
else if (KeysDown.get().contains(KeyEvent.VK_DOWN)) {
Main.player.moveDown();
}
else if (KeysDown.get().contains(KeyEvent.VK_UP)) {
Main.player.moveUp();
}
}
}
Thank you!
Again as I have mentioned in previous comments:
Don't draw directly on the JApplet or in any top-level window.
If you give your applet a paintComponent method, it won't override any applet methods and won't gain the benefit of double buffering.
Instead draw in the paintComponent method of a JPanel, an thereby gain the benefit of double buffering.
Use Key Bindings not a KeyListener.
Also, I like to use a Swing Timer for simple game loops.
In the code below, I use an enum called Direction to encapsulate the idea of directions on the screen.
I then put the for Directions together with Boolean.FALSE in a Map<Direction, Boolean> called dirMap.
I run a Swing Timer continuously, polling the state of this Map and move a sprite based on the state of Booleans held by the dirMap.
I change the state of the map held values in my Key Binding Actions. So if the down key is pressed, its action will change the Direction.DOWN associated value in the Map to true, and when released, the Direction.DOWN associated value will be changed back to false.
For example:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.EnumMap;
import javax.imageio.ImageIO;
import javax.swing.*;
#SuppressWarnings("serial")
public class AnimateExample extends JPanel {
public static final String DUKE_IMG_PATH = // https://duke.kenai.com/iconSized/duke.gif
"https://duke.kenai.com/iconSized/duke4.gif";
private static final int PREF_W = 800;
private static final int PREF_H = 800;
private static final int TIMER_DELAY = 20;
private static final String KEY_DOWN = "key down";
private static final String KEY_RELEASE = "key release";
public static final int TRANSLATE_SCALE = 3;
private static final String BACKGROUND_STRING = "Use Arrow Keys to Move Image";
private static final Font BG_STRING_FONT = new Font(Font.SANS_SERIF,
Font.BOLD, 32);
private EnumMap<Direction, Boolean> dirMap =
new EnumMap<AnimateExample.Direction, Boolean>(Direction.class);
private BufferedImage image = null;
private int imgX = 0;
private int imgY = 0;
private int bgStringX;
private int bgStringY;
public AnimateExample() {
for (Direction dir : Direction.values()) {
dirMap.put(dir, Boolean.FALSE);
}
try {
URL imgUrl = new URL(DUKE_IMG_PATH);
image = ImageIO.read(imgUrl);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
new Timer(TIMER_DELAY, new TimerListener()).start();
// here we set up our key bindings
int condition = JComponent.WHEN_IN_FOCUSED_WINDOW;
InputMap inputMap = getInputMap(condition);
ActionMap actionMap = getActionMap();
for (final Direction dir : Direction.values()) {
// for the key down key stroke
KeyStroke keyStroke = KeyStroke.getKeyStroke(dir.getKeyCode(), 0,
false);
inputMap.put(keyStroke, dir.name() + KEY_DOWN);
actionMap.put(dir.name() + KEY_DOWN, new AbstractAction() {
#Override
public void actionPerformed(ActionEvent arg0) {
dirMap.put(dir, true);
}
});
// for the key release key stroke
keyStroke = KeyStroke.getKeyStroke(dir.getKeyCode(), 0, true);
inputMap.put(keyStroke, dir.name() + KEY_RELEASE);
actionMap.put(dir.name() + KEY_RELEASE, new AbstractAction() {
#Override
public void actionPerformed(ActionEvent arg0) {
dirMap.put(dir, false);
}
});
}
FontMetrics fontMetrics = getFontMetrics(BG_STRING_FONT);
int w = fontMetrics.stringWidth(BACKGROUND_STRING);
int h = fontMetrics.getHeight();
bgStringX = (PREF_W - w) / 2;
bgStringY = (PREF_H - h) / 2;
}
#Override
public Dimension getPreferredSize() {
return new Dimension(PREF_W, PREF_H);
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g.setFont(BG_STRING_FONT);
g.setColor(Color.LIGHT_GRAY);
g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
g.drawString(BACKGROUND_STRING, bgStringX, bgStringY);
if (image != null) {
g.drawImage(image, imgX, imgY, this);
}
}
private class TimerListener implements ActionListener {
public void actionPerformed(java.awt.event.ActionEvent e) {
for (Direction dir : Direction.values()) {
if (dirMap.get(dir)) {
imgX += dir.getX() * TRANSLATE_SCALE;
imgY += dir.getY() * TRANSLATE_SCALE;
}
}
repaint();
};
}
enum Direction {
Up(KeyEvent.VK_UP, 0, -1), Down(KeyEvent.VK_DOWN, 0, 1), Left(
KeyEvent.VK_LEFT, -1, 0), Right(KeyEvent.VK_RIGHT, 1, 0);
private int keyCode;
private int x;
private int y;
private Direction(int keyCode, int x, int y) {
this.keyCode = keyCode;
this.x = x;
this.y = y;
}
public int getKeyCode() {
return keyCode;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
}
private static void createAndShowGui() {
AnimateExample mainPanel = new AnimateExample();
JFrame frame = new JFrame("Animate Example");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
For more on Key Bindings, please check out the informative tutorial which you can find here.