Super works fine, but your interpretation of what it does is incorrect. Your problem is that you are trying to use inheritance to solve a problem that is not resolved with inheritance. You should call repaint() actual rendered and used instance of Ball, and not an instance of some completely different Key class that is inadequately distributed from Ball. First, make Key not continue Ball, go to the true link to Ball in Key and the solution will fall from that.
Maybe 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);
Note that I would use Key Bindings for this and not KeyListener, since then I would not have to futz with keyboard focus.
For instance:
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(px, py); } 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; } }
source share