Immer wieder frag ich mich...du magst Java?...:bloed:
Mal so als Witz am Rande
Mein Problem: ich will bloß Zeichnen mit Java. Ich benütze Netbeans (neueste Version). Ich erstelle mit dem JFrame GUI Builder mal eine Klasse und pack ein Panel mit 2 Buttons darauf. Das jPanel ist als zeichenblatt gedacht.
Ich erstelle eine neue Klasse mit paint(Graphics g) und schreibe meine Linienkoordinaten rein. In der Main und GUI Klasse mache ich auch eine paint() Methode auf - und hier fängt das Problem dann an.
Zeichne ich auf dem Zeichenblatt, dann bugt es herum. Das Zeichenblatt-jPanel ist versetz und Buttons werden erst angezeigt wenn man mit der Maus drüber fährt.
Auf dem Bild, sieht man (li.) wie es auf der IDE aussieht, in der mitte dann wie es beim Ausführen ausschaut. Rechts ist das was man sieht wenn man auf Preview Design klickt.
In der Main Klasse "Fenster" mal bitte die paint Methode anschauen, kommentiert
Andere Infos:
Product Version: NetBeans IDE 7.4 (Build 201310111528)
Updates: NetBeans IDE is updated to version NetBeans 7.4 Patch 2
Java: 1.7.0_45; Java HotSpot(TM) 64-Bit Server VM 24.45-b08
Runtime: Java(TM) SE Runtime Environment 1.7.0_45-b18
System: Windows 7 version 6.1 running on amd64; Cp1252; de_DE (nb)
User directory: C:\Users\Tek\AppData\Roaming\NetBeans\7.4
Cache directory: C:\Users\Tek\AppData\Local\NetBeans\Cache\7.4
Mal so als Witz am Rande
Mein Problem: ich will bloß Zeichnen mit Java. Ich benütze Netbeans (neueste Version). Ich erstelle mit dem JFrame GUI Builder mal eine Klasse und pack ein Panel mit 2 Buttons darauf. Das jPanel ist als zeichenblatt gedacht.

Ich erstelle eine neue Klasse mit paint(Graphics g) und schreibe meine Linienkoordinaten rein. In der Main und GUI Klasse mache ich auch eine paint() Methode auf - und hier fängt das Problem dann an.
Zeichne ich auf dem Zeichenblatt, dann bugt es herum. Das Zeichenblatt-jPanel ist versetz und Buttons werden erst angezeigt wenn man mit der Maus drüber fährt.
Auf dem Bild, sieht man (li.) wie es auf der IDE aussieht, in der mitte dann wie es beim Ausführen ausschaut. Rechts ist das was man sieht wenn man auf Preview Design klickt.

In der Main Klasse "Fenster" mal bitte die paint Methode anschauen, kommentiert
Java:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.Java.Lernen.Zeichnen;
import java.awt.Graphics;
public class Fenster extends javax.swing.JFrame {
/**
* Creates new form Fenster
*/
// DrawMath m = new DrawMath();
public Fenster() {
initComponents();
}
// public void paint(Graphics g){
//
// Funktioniert nicht
// jPanel1.paint(g);
//
// Funtioniert auch nicht
// m.paint(g);
//
// Funtioniert, es wird aber halt auf super. gezeichnet
// super.paint(g);
// g.drawLine(10, 20, 30, 40);
// }
/**
* 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() {
jPanel1 = new javax.swing.JPanel();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jPanel1.setBackground(new java.awt.Color(204, 255, 255));
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 246, Short.MAX_VALUE)
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 243, Short.MAX_VALUE)
);
jButton1.setText("jButton1");
jButton2.setText("jButton2");
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()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jButton1)
.addComponent(jButton2))
.addContainerGap(44, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jButton1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton2))
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
/**
* @param args the command line arguments
*/
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(Fenster.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Fenster.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Fenster.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Fenster.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Fenster().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JPanel jPanel1;
// End of variables declaration
}
Java:
package com.Java.Lernen.Zeichnen;
import java.awt.Graphics;
public class DrawMath {
public void paint(Graphics g){
g.drawLine(20, 25, 120, 110);
}
}
Andere Infos:
Product Version: NetBeans IDE 7.4 (Build 201310111528)
Updates: NetBeans IDE is updated to version NetBeans 7.4 Patch 2
Java: 1.7.0_45; Java HotSpot(TM) 64-Bit Server VM 24.45-b08
Runtime: Java(TM) SE Runtime Environment 1.7.0_45-b18
System: Windows 7 version 6.1 running on amd64; Cp1252; de_DE (nb)
User directory: C:\Users\Tek\AppData\Roaming\NetBeans\7.4
Cache directory: C:\Users\Tek\AppData\Local\NetBeans\Cache\7.4