Hallo!
Ich habe ein Programm geschrieben welches einen Frakatal Baum in der Klasse FractalController zeichnet. Nach jeder "Ebene" wird die Liste der Linien an das Model gegeben und der Controller pausiert etwas. Das Model gibt die Änderung an den Painter.
Auszug aus dem Model:
Auszug aus dem Painter
Habe ich nun mit Netbeans ein Form erstellt, so wird erst das Endresultat angezeigt. Die Repaintmethode wird nicht bei jedem Update aufgerufen (es erscheint kein "pc" in der Konsole).
Habe ich das Form selber geschrieben wird es wie gewünscht angezeigt. Ich frage mich nun was ich der Unterschied in diesem Fall zwischen den beiden JFrames, das es beim einen nicht funktioniert und bei anderen schon.
Selber gemacht (stark abgespeckt), funktioniert:
NetBeans, funktioniert nicht:
Warum ist das so?
Liebe Grüße Martin
Ich habe ein Programm geschrieben welches einen Frakatal Baum in der Klasse FractalController zeichnet. Nach jeder "Ebene" wird die Liste der Linien an das Model gegeben und der Controller pausiert etwas. Das Model gibt die Änderung an den Painter.
Auszug aus dem Model:
Java:
public void addLines(List<Line> lines){
this.setChanged();
this.notifyObservers(lines);
System.out.println("Lines added. Now " + lines.size() + " lines");
}
Auszug aus dem Painter
Java:
@Override
public void update(Observable o, Object o1) {
if(o1.getClass() == LinkedList.class){
try{
lines.addAll((Collection<? extends Line>) o1);
System.out.println("lines received");
this.repaint();
}catch (Exception e){
System.out.println("Probleme beim AddAll");
}
}
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
System.out.println("pc");
for(Line line : lines){
g.setColor(line.getColor());
g.drawLine(line.getX1(),line.getY1(),line.getX2(),line.getY2());
}
}
Habe ich nun mit Netbeans ein Form erstellt, so wird erst das Endresultat angezeigt. Die Repaintmethode wird nicht bei jedem Update aufgerufen (es erscheint kein "pc" in der Konsole).
Habe ich das Form selber geschrieben wird es wie gewünscht angezeigt. Ich frage mich nun was ich der Unterschied in diesem Fall zwischen den beiden JFrames, das es beim einen nicht funktioniert und bei anderen schon.
Selber gemacht (stark abgespeckt), funktioniert:
Java:
public class MainTest extends JFrame{
public MainTest() {
this.setTitle("Test");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(500,500);
this.setVisible(true);
Model model = new Model();
FractalController fc = new FractalController(model);
PainterMan pm = new PainterMan();
pm.setPreferredSize(new Dimension(1000,1000));
pm.setSize(1000, 1000);
this.setLayout(new FlowLayout());
this.add(pm);
model.addObserver(pm);
JFileChooser chooser = new JFileChooser();
FileFilter ff = new FileFilter() {
@Override
public boolean accept(File file) {
return file.getName().endsWith(".tree") || file.isDirectory();
}
@Override
public String getDescription() {
return ".tree";
}
};
chooser.setFileFilter(ff);
int chooseInt = chooser.showOpenDialog(null);
if(chooseInt == JFileChooser.APPROVE_OPTION){
LoadTree loadTree = new LoadTree();
loadTree.loadSettings(chooser.getSelectedFile().getAbsolutePath());
fc.startTree(loadTree.getRoot(),loadTree.getRootEnd(),loadTree.getBranchA(),loadTree.getBranchB());
}
}
public static void main(String args[]) {
new MainTest();
}
}
NetBeans, funktioniert nicht:
Java:
package fractaltree2;
import java.awt.Dimension;
import java.awt.FlowLayout;
/**
*
* @author Admin
*/
public class GUI extends javax.swing.JFrame{
/**
* Creates new form GUI
*/
public GUI() {
initComponents();
PainterMan painter = new PainterMan();
painter.setPreferredSize(new Dimension(panel.getWidth()-20,panel.getHeight()-20));
painter.setSize(panel.getWidth()-20, panel.getHeight()-20);
panel.setLayout(new FlowLayout());
panel.add(painter);
loadButton.addActionListener(controller);
speedSlider.addChangeListener(controller);
model.addObserver(painter);
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
try {
panel =(javax.swing.JPanel)java.beans.Beans.instantiate(getClass().getClassLoader(), "fractaltree2.GUI_panel");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (java.io.IOException e) {
e.printStackTrace();
}
loadButton = new javax.swing.JButton();
speedSlider = new javax.swing.JSlider();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setPreferredSize(new java.awt.Dimension(900, 600));
javax.swing.GroupLayout panelLayout = new javax.swing.GroupLayout(panel);
panel.setLayout(panelLayout);
panelLayout.setHorizontalGroup(
panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 669, Short.MAX_VALUE)
);
panelLayout.setVerticalGroup(
panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 398, Short.MAX_VALUE)
);
loadButton.setText("Baum laden");
speedSlider.setMajorTickSpacing(10);
speedSlider.setMaximum(500);
speedSlider.setPaintTicks(true);
speedSlider.setSnapToTicks(true);
speedSlider.setToolTipText("");
jLabel1.setText("langsamer");
jLabel2.setText("schneller");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(panel, javax.swing.GroupLayout.DEFAULT_SIZE, 671, Short.MAX_VALUE)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(jLabel2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(speedSlider, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jLabel1)
.addGap(26, 26, 26)
.addComponent(loadButton)))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(panel, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(loadButton)
.addComponent(speedSlider, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(jLabel2)
.addContainerGap()))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(jLabel1)
.addContainerGap())))
);
pack();
}// </editor-fold>
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see [url=http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html]How to Set the Look and Feel (The Java™ Tutorials > Creating a GUI With JFC/Swing > Modifying the Look and Feel)[/url]
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
new GUI().setVisible(true);/*
//Create and display the form
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new GUI().setVisible(true);
}
}); */
}
// Variables declaration - do not modify
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
public static javax.swing.JButton loadButton;
private javax.swing.JPanel panel;
public static javax.swing.JSlider speedSlider;
// End of variables declaration
private Model model = new Model();
private FractalController controller = new FractalController(model);
}
Warum ist das so?
Liebe Grüße Martin