I am stuck to with this java code and I did comment on bottom where I can\'t fig
ID: 3912423 • Letter: I
Question
I am stuck to with this java code and I did comment on bottom where I can't figure out
package jframe;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
/**
*
* @author GedeonZNzombi
*/
public class Main extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel jContentPane = null;
private PanelPelota panel = null;
private PanelPelota getPanel() {
if (panel == null) {
panel = new PanelPelota();
return panel;
}
/**
* This is the default constructor
*/
public Main() { // I am having the problem here
super();
initialize();
// Listeners for the keyboard
this.addKeyListener(new KeyAdapter() {
//Method for the key pressed
public void keyPressed(KeyEvent evt) {
formKeyPressed(evt);
}
// Method for the key released
public void keyReleased(KeyEvent evt) {
formKeyReleased(evt);
}
});
}
// Here i'm stating the method that will send the key pressed to the game class
private void formKeyPressed(KeyEvent evt)
{
panel.keyPressed(evt);
}
// Here i'm stating the method that will send the key released to the game class
private void formKeyReleased(KeyEvent evt)
{
panel.keyReleased(evt);
}
/**
* This method initializes this
*
* @return void
*/
private void initialize() {
this.setResizable(false);
this.setBounds(new Rectangle(312, 184, 250, 250)); // Position on the desktop
this.setMinimumSize(new Dimension(250, 250));
this.setMaximumSize(new Dimension(250, 250));
this.setContentPane(getJContentPane());
this.setTitle("Pong");
}
/**
* This method initializes jContentPane
*
* @return javax.swing.JPanel
*/
private JPanel getJContentPane() {
if (jContentPane == null) {
jContentPane = new JPanel();
jContentPane.setLayout(new BorderLayout());
jContentPane.add(getPanel(), BorderLayout.CENTER);
}
return jContentPane;
}
public static void main(String[] args) {
// TODO code application logic here
SwingUtilities.invokeLater(new Runnable() {
public void run() {
Main thisClass = new Main();
thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
thisClass.setVisible(true);
}
});
}
}
Explanation / Answer
The code is working fine. I added the debug statement in your keyPressed and released functions and they print just fine.. I do not know what is expected of those functions, but i can surely tell that they get called. Let me know if more discussion is needed. Thanks!
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.