Reputation: 1
someone can explain to me why it keeps giving me this error
error: MyPanel is not abstract and does not override abstract method actionPerformed(ActionEvent) in ActionListener public class MyPanel extends JPanel implements ActionListener {
i think i did everything right i can't figure out what i did wrong, this code was for a test to make an image move horizontally
this is my code
Main.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.event.ActionListener;
public class Main {
public static void main(String [] args) {
new MyFrame();
}
}
MyFrame.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.event.ActionListener;
public class MyFrame extends JFrame {
MyPanel panel;
MyFrame(){
panel = new MyPanel();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(1024,720);
this.add(panel);
this.pack();
this.setLocationRelativeTo(null);
this.setVisible(true);
}
}
MyPanel.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.event.ActionEvent;
public class MyPanel extends JPanel implements ActionListener {
final int PANEL_WIDTH = 1024;
final int PANEL_HEIGHT = 720;
Image pilota;
Image backgroundImage;
Timer timer;
int xVelocity = 1;
int yVelocity = 1;
int x = 0;
int y = 0;
MyPanel() {
this.setPreferredSize(new Dimension(PANEL_WIDTH,PANEL_HEIGHT));
this.setBackground(Color.black);
pilota = new ImageIcon("pilota1.png").getImage();
timer = new Timer(10,null);
timer.start();
}
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2D = (Graphics2D) g;
g2D.drawImage(pilota, x, y, null);
}
public void actionPerfomed(ActionEvent e) {
x = x + xVelocity;
repaint();
}
}
Upvotes: 0
Views: 800
Reputation: 168825
public void actionPerfomed(ActionEvent e) {
Should be (with two letter 'r's):
public void actionPerformed(ActionEvent e) {
But always add the override notation, so:
@Override
public void actionPerformed(ActionEvent e) {
Sidebar re:
public void paint(Graphics g) {
super.paint(g);
To custom paint any JComponent
the correct method is:
public void paintComponent(Graphics g) {
super.paintComponent(g);
Of course, also add the override notation.
Upvotes: 1