Hallo,
seit einigen Stunden arbeite ich nun schon an diesem Problem und ich finde einfach nicht heraus was fehlt bzw. falsch ist. Eigentlich hatte alles gut funktioniert, aber nachdem ich etwas geändert hatte bleibt das Panel einfach die ganze Zeit grau. Dummerweise kann ich auch nicht genau sagen, welche Änderung das verursacht hat.
Was passieren soll:
Nach dem Programmstart sollte im JFrame eigentlich alles weiß sein. Nachdem man das MenuItem im Menü Datei drückt und im JDialog der erscheint "Ok" drückt, sollte nochmal was anders gezeichnet werden.
Allerdings sieht man zumindest im JFrame die ganze Zeit nur das "Standardgrau".
Hier wäre mein Code:
seit einigen Stunden arbeite ich nun schon an diesem Problem und ich finde einfach nicht heraus was fehlt bzw. falsch ist. Eigentlich hatte alles gut funktioniert, aber nachdem ich etwas geändert hatte bleibt das Panel einfach die ganze Zeit grau. Dummerweise kann ich auch nicht genau sagen, welche Änderung das verursacht hat.
Was passieren soll:
Nach dem Programmstart sollte im JFrame eigentlich alles weiß sein. Nachdem man das MenuItem im Menü Datei drückt und im JDialog der erscheint "Ok" drückt, sollte nochmal was anders gezeichnet werden.
Allerdings sieht man zumindest im JFrame die ganze Zeit nur das "Standardgrau".
Hier wäre mein Code:
Java:
public class PianoSchedule {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
MainFrm mainFrm=new MainFrm();
}
});
}
}
Java:
public class MainFrm extends javax.swing.JFrame {
public static int mx=0;
public static int my=0;
public static String state="Programmstart";
public static String werksname;
public static String komponist;
public static String stilrichtung;
private PaintBox paintBox;
private final Projektstartfenster projektstartfenster;
/**
* Creates new form mainFrm
*/
public MainFrm() {
this.paintBox = new PaintBox();
projektstartfenster = new Projektstartfenster(this ,paintBox ,true);
// paintBox.addMouseMotionListener(new MouseMotionListener() {
//
// @Override
// public void mouseDragged(MouseEvent e) {
//
// }
//
// @Override
// public void mouseMoved(MouseEvent e) {
// mx=e.getX();
// my=e.getY();
// paintBox.repaint();
// }
// });
add(paintBox, BorderLayout.CENTER);
setVisible(true);
initComponents();
}
/**
* 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() {
}// </editor-fold>
private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {
projektstartfenster.setVisible(true);
}
/**
* @param args the command line arguments
*/
// Variables declaration - do not modify
private javax.swing.JMenu jMenu1;
private javax.swing.JMenu jMenu2;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JMenuItem jMenuItem1;
// End of variables declaration
}
Java:
public class Projektstartfenster extends javax.swing.JDialog {
PaintBox paintBox ;
/**
* Creates new form Projektstartfenster
* @param parent
* @param paintBox
* @param modal
*/
public Projektstartfenster(java.awt.Frame parent,PaintBox paintBox, boolean modal) {
super(parent, modal);
this.paintBox=paintBox;
initComponents();
pack();
setLocationRelativeTo(parent);
}
/**
* 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() {
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
PaintBox.werksname=feldWerksname.getText();
PaintBox.komponist=feldKomponist.getText();
PaintBox.stilrichtung=feldStilrichtung.getText();
MainFrm.state="Neues Projekt";
paintBox.repaint();
Projektstartfenster.this.setVisible(false);
}
/**
* @param args the command line arguments
*/
// Variables declaration - do not modify
private javax.swing.JTextField feldKomponist;
private javax.swing.JTextField feldStilrichtung;
private javax.swing.JTextField feldWerksname;
private javax.swing.JButton jButton1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JPanel jPanel1;
// End of variables declaration
}
Java:
public class PaintBox extends JPanel{
public static String werksname;
public static String komponist;
public static String stilrichtung;
@Override
protected void paintComponent(Graphics g) {
int mx=MainFrm.mx;
int my=MainFrm.my;
int l=Math.round((this.getWidth())/2);
int h=Math.round((this.getHeight()-24)/2);
int mix=Math.round((this.getWidth())/2);
super.paintComponent(g);
Graphics2D g2d = (Graphics2D)g;
g2d.setColor(Color.white);
g2d.fillRect(0, 0, this.getWidth(), this.getHeight());
g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
if ("Neues Projekt".equals(MainFrm.state)) {
g2d.setColor(Color.white);
g2d.fillRect(0, 0, this.getWidth(), this.getHeight());
if ((mx<=l) && (my<=h)) {
g2d.setColor(Color.decode("#595959"));
} else {
g2d.setColor(Color.decode("#404040"));
}
g2d.fillRect(0, 0, l, h);
if ((mx>=l) && (my<=h)) {
g2d.setColor(Color.decode("#7bd5f1"));
} else {
g2d.setColor(Color.decode("#6dbdd6"));
}
g2d.fillRect(mix, 0, l, h);
if ((mx<=l) && (my>=h)) {
g2d.setColor(Color.decode("#d2172d"));
} else {
g2d.setColor(Color.decode("#B71427"));
}
g2d.fillRect(0, h, l, h);
if ((mx>=l) && (my>=h)) {
g2d.setColor(Color.decode("#FFE658"));
} else {
g2d.setColor(Color.decode("#e6cf4f"));
}
g2d.fillRect(mix, h, l, h);
g2d.setColor(Color.decode("#f0f0f0"));
g2d.fillRect(0, 2*h, 2*l, 24);
g2d.setColor(Color.white);
g2d.setFont(new Font("default", Font.PLAIN, 30));
g2d.drawString("Dekodieren", 40, Math.round(h/2)+10);
g2d.drawString("Automatisieren", l+40, Math.round(h/2)+10);
g2d.drawString("Balancieren", 40, h+Math.round(h/2)+10);
g2d.drawString("Kolorieren", l+40, h+Math.round(h/2)+10);
g2d.setColor(Color.gray);
g2d.setFont(new Font("default", Font.PLAIN, 13));
g2d.drawString("Name des Werks: " + werksname + " " + "Komponist: " + komponist + " " + "Stilrichtung: " + stilrichtung, 5, 2*h+17);
}
}
}
Zuletzt bearbeitet: