Hallo Leute,
ich habe ein Problem mit folgendem Code. Ich hab mit NetBeans eine Oberfläche erstellt mit einem JFrame, JPanel und JButton.
Main.java
NewJFrame.java
Das Problem tritt auf, wenn die Prozedur "animation ( NewJFrame frame )" per Button aufgerufen wird.
Dann sieht man nur die Startposition und die Endposition, obwohl sich die y-Koordinate verändert (kann man der Ausgabe sehen).
Wenn die Prozedur jedoch in der Main-Methode aufgerufen wird (zur Zeit auskommentiert), bewegt sich das schwarze Rechteck normal nach unten und wird richtig animiert.
Könnt ihr mir hierbei helfen und sagen wo mein Fehler liegt?
Vielen Dank
Stromberg
ich habe ein Problem mit folgendem Code. Ich hab mit NetBeans eine Oberfläche erstellt mit einem JFrame, JPanel und JButton.
Main.java
Java:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package test;
/**
*
* @author Administrator
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void animation (NewJFrame frame)
{
boolean status = true;
int x, y;
x = frame.getxTest();
y = frame.getyTest();
while (status)
{
try
{
Thread.sleep(50);
}
catch (InterruptedException e)
{}
frame.setxTest(x);
frame.setyTest(y);
y++;
frame.repaint();
System.out.println(y);
if (y >= frame.getPanelHeight())
{
status = false;
}
}
}
public static void main(String[] args) {
// TODO code application logic here
NewJFrame fenster = new NewJFrame();
//animation(fenster);
}
}
NewJFrame.java
Java:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/*
* NewJFrame.java
*
* Created on 10.10.2010, 17:01:04
*/
package test;
import java.awt.*;
public class NewJFrame extends javax.swing.JFrame {
int xTest, yTest;
public NewJFrame() {
initComponents();
setVisible(true);
xTest = 10;
yTest = 1;
}
public void setxTest(int xTest) {
this.xTest = xTest;
}
public void setyTest(int yTest) {
this.yTest = yTest;
}
public int getxTest() {
return xTest;
}
public int getyTest() {
return yTest;
}
public int getPanelHeight ()
{
return testP.getHeight();
}
/** 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() {
testP = new javax.swing.JPanel()
{//////////////////////EIGENER CODE///////////////////
public void paintComponent(Graphics g)
{
Graphics2D g2d = (Graphics2D) g;
super.paintComponent(g2d);
g2d.fillRect(xTest, yTest, 10, 10);
}
////////////////////////////////////////////////////
}
;
jButton1 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
testP.setBorder(javax.swing.BorderFactory.createTitledBorder("Test"));
javax.swing.GroupLayout testPLayout = new javax.swing.GroupLayout(testP);
testP.setLayout(testPLayout);
testPLayout.setHorizontalGroup(
testPLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 239, Short.MAX_VALUE)
);
testPLayout.setVerticalGroup(
testPLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 246, Short.MAX_VALUE)
);
jButton1.setText("jButton1");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
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(40, 40, 40)
.addComponent(testP, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(47, 47, 47)
.addComponent(jButton1)
.addContainerGap(94, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(19, 19, 19)
.addComponent(testP, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(129, 129, 129)
.addComponent(jButton1)))
.addContainerGap(114, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
Main.animation(this);
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NewJFrame().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JPanel testP;
// End of variables declaration
}
Das Problem tritt auf, wenn die Prozedur "animation ( NewJFrame frame )" per Button aufgerufen wird.
Dann sieht man nur die Startposition und die Endposition, obwohl sich die y-Koordinate verändert (kann man der Ausgabe sehen).
Wenn die Prozedur jedoch in der Main-Methode aufgerufen wird (zur Zeit auskommentiert), bewegt sich das schwarze Rechteck normal nach unten und wird richtig animiert.
Könnt ihr mir hierbei helfen und sagen wo mein Fehler liegt?
Vielen Dank
Stromberg