Hallo allerseits,
Mit dem Visual-Editor von Netbeans habe ich eine _einfache_ grafische Oberfläche erzeugt.
Dort soll sich von links nach rechts ein Ball bewegen.
Meine Frage:
Warum wird die Methode paintComponent nicht aufgerufen?
Was muß ich machen, damit diese aufgerufen wird?
Wer kann mir helfen? ich komme hier nicht weiter !
mfg
Ernst
Mit dem Visual-Editor von Netbeans habe ich eine _einfache_ grafische Oberfläche erzeugt.
Dort soll sich von links nach rechts ein Ball bewegen.
Meine Frage:
Warum wird die Methode paintComponent nicht aufgerufen?
Was muß ich machen, damit diese aufgerufen wird?
Wer kann mir helfen? ich komme hier nicht weiter !
mfg
Ernst
Java:
package packT1;
public class MyJFrame extends javax.swing.JFrame {
public MyJFrame() {
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jPanel1.setBackground(new java.awt.Color(255, 255, 102));
jPanel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
jPanel1.setLayout(new java.awt.BorderLayout());
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(43, 43, 43)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 308, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(49, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(22, 22, 22)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 207, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(71, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
// new MyJFrame().setVisible(true);
MyJFrame myJFrame = new MyJFrame();
myJFrame.setSize(700, 700);
MyJPanel myJPAnel = new MyJPanel();
myJFrame.getContentPane().add(myJPAnel);
myJFrame.setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JPanel jPanel1;
// End of variables declaration
}
Java:
package packT1;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;
public class MyJPanel extends javax.swing.JPanel {
private Timer t;
private Ball ball;
public MyJPanel() {
initComponents();
ball = new Ball(this);
t = new Timer(20,
new ActionListener() {
public void actionPerformed(ActionEvent ae) {
System.out.println("Hallo1");
myupdate();
repaint();
}
});
t.start();
}
public void setTimer(int millis) {
t.setDelay(millis);
}
public int getTimerTime() {
return t.getDelay();
}
public void myupdate() {
ball.berechnePos();
}
// ??????????????????????????????????????????????????????????????
// ???? Warum wird dieses paintComponent nicht aufgerufen ?????
// ??????????????????????????????????????????????????????????????
public void paintComponent(Graphics g) {
System.out.println("Wo befindet sich Hallo2");
/*
super.paintComponent(g);
Point pos;
pos = ball.getPos();
g.drawImage(ball.getImage(), pos.x, pos.y, this);
System.out.println("Wo befindet sich Hallo2");
*/
}
/** 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.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
setLayout(new java.awt.BorderLayout());
}// </editor-fold>
// Variables declaration - do not modify
// End of variables declaration
}
Java:
package packT1;
import java.awt.Point;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class Ball {
// x-Koordinate der Kugel
private int x;
// y-Koordinate der Kugel
private int y;
// Das Bild der Kugel befindet sich als Datei auf Festplatte
private BufferedImage bild;
private MyJPanel myJPanel;
public Ball(MyJPanel pspielJPanel){
try {
myJPanel = pspielJPanel;
bild = ImageIO.read(new File("meinebilder/ball.png"));
} catch (IOException ex) {
System.out.println("Datei existiert nicht:" +ex.toString());
ex.printStackTrace();
System.exit(0);
}
}
public BufferedImage getImage() {
return bild;
}
public void berechnePos() {
if( x < myJPanel.getWidth()){
x+=3;
}
else{
x=0;
}
System.out.println("myJPanel.getWidth()="+myJPanel.getWidth());
}
public Point getPos() {
return new Point(x, y);
}
}