Hi!
A view weeks ago I changed to Java and so far everything's fine - except drawing (with Swing). The code below works - except the part that draws a graphics. What's wrong?
package Test2GUI;
import java.time.format.*;
import java.time.temporal.ChronoUnit;
import java.awt.EventQueue;
import javax.swing.*;
import javax.swing.JFrame;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.time.LocalDate;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Line2D;
import java.awt.Color;
public class Test2GUI extends JFrame {
public static JFrame frmBiorhythmus;
public static JTextField textFieldD1;
public static JTextField textFieldD2;
public static JLabel lblNewLabelTage = new JLabel("Sie sind xxxx Tage alt.");
public static JPanel panelGrafik = new JPanel();
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Test2GUI window = new Test2GUI();
window.frmBiorhythmus.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Test2GUI() {
initialize();
}
private void initialize() {
frmBiorhythmus = new JFrame();
frmBiorhythmus.addWindowListener(new WindowAdapter() {
@Override
public void windowActivated(WindowEvent e) {
}
});
frmBiorhythmus.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frmBiorhythmus.setVisible(true);
frmBiorhythmus.setResizable(false);
frmBiorhythmus.setTitle("Biorhythmus");
frmBiorhythmus.setBounds(100, 100, 516, 519);
frmBiorhythmus.getContentPane().setLayout(null);
JLabel lblNewLabelD1 = new JLabel("Geburtsdatum:");
lblNewLabelD1.setBounds(28, 28, 116, 14);
frmBiorhythmus.getContentPane().add(lblNewLabelD1);
JLabel lblNewLabelD2 = new JLabel("Berechnungsdatum:");
lblNewLabelD2.setBounds(28, 64, 116, 14);
frmBiorhythmus.getContentPane().add(lblNewLabelD2);
textFieldD1 = new JTextField();
textFieldD1.setText("15.05.1962");
textFieldD1.setBounds(153, 25, 86, 20);
frmBiorhythmus.getContentPane().add(textFieldD1);
textFieldD1.setColumns(10);
LocalDate d2 = LocalDate.now();
DateTimeFormatter dFormat = DateTimeFormatter.ofPattern("dd.MM.yyyy");
textFieldD2 = new JTextField();
textFieldD2.setText(d2.format(dFormat));
textFieldD2.setBounds(154, 61, 86, 20);
frmBiorhythmus.getContentPane().add(textFieldD2);
textFieldD2.setColumns(10);
JButton btnNewButtonOK = new JButton("berechnen");
btnNewButtonOK.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
GetData(panelGrafik);
}
});
btnNewButtonOK.setBounds(285, 60, 105, 23);
frmBiorhythmus.getContentPane().add(btnNewButtonOK);
lblNewLabelTage.setVerticalAlignment(SwingConstants.TOP);
lblNewLabelTage.setBounds(153, 107, 237, 76);
frmBiorhythmus.getContentPane().add(lblNewLabelTage);
double h0 = lblNewLabelTage.getBounds().y + lblNewLabelTage.getBounds().height + 10;
panelGrafik.setBounds(0, (int)h0, frmBiorhythmus.getWidth(), (int)(frmBiorhythmus.getHeight() - h0));
frmBiorhythmus.getContentPane().add(panelGrafik);
Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
int x = (int) ((dimension.getWidth() - frmBiorhythmus.getWidth()) / 2);
int y = (int) ((dimension.getHeight() - frmBiorhythmus.getHeight()) / 2);
frmBiorhythmus.setLocation(x, y);
}
static void GetData(JPanel panelGrafik) {
String d1s = textFieldD1.getText();
String d2s = textFieldD2.getText();
DateTimeFormatter df = DateTimeFormatter.ofPattern("dd.MM.yyyy");
LocalDate d1 = LocalDate.parse(d1s, df);
LocalDate d2 = LocalDate.parse(d2s, df);
double d = ChronoUnit.DAYS.between(d1, d2);
int ko = (int) (Math.sin((d / 23 - Math.rint(d / 23)) * Math.PI * 2) * 100);
int se = (int) (Math.sin((d / 28 - Math.rint(d / 28)) * Math.PI * 2) * 100);
int ge = (int) (Math.sin((d / 33 - Math.rint(d / 33)) * Math.PI * 2) * 100);
String txt = "<html>";
txt += "Sie sind " + (int) d + " Tage alt.<br>";
txt += "<font color=\"blue\">körperlich</font>e Verfassung: " + (ko >= 0 ? "+" : "") + ko + "%<br>";
txt += "<font color=\"red\">seelisch</font>e Verfassung: " + (se >= 0 ? "+" : "") + se + "%<br>";
txt += "<font color=\"green\">geistig</font>e Verfassung: " + (ge >= 0 ? "+" : "") + ge + "%<br>";
lblNewLabelTage.setText(txt);
panelGrafik.revalidate();
panelGrafik.repaint();
}
class panelGrafik extends JPanel {
panelGrafik() {
revalidate();
repaint();
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawString("BLAH", 20, 20);
g.drawRect(10, 50, 200, 150);
}
}
}
What I have tried:
Spent hours to read code examples.