Click here to Skip to main content
15,867,488 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
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 {

	// following public declarations to get access from everywhere
	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();

	/**
	 * Launch the application.
	 */
	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();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public Test2GUI() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	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);

		// center window
		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);
		// check dates
		// ...

		// calculations
		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() {
            // set a preferred size for the custom panel.
            // setPreferredSize(new Dimension(420,420));
    		revalidate();
    		repaint();
        }

        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);

            // test drawing
            g.drawString("BLAH", 20, 20);
            g.drawRect(10, 50, 200, 150);
        }
    }

}


What I have tried:

Spent hours to read code examples.
Posted
Updated 13-Dec-22 2:21am
Comments
CHill60 13-Dec-22 7:31am    
What is wrong indeed? What happens or doesn't happen. State your problem
heinkurz 13-Dec-22 8:22am    
The text output in "JLabel lblNewLabelTage" in "GetData()" works fine but there's no graphic output on "JPanel panelGrafik". Nothing happens. And there's also no error message.
Richard MacCutchan 13-Dec-22 8:29am    
You have declared a JPanel named panelGrafik, so that is what you are writing into. The class that you have named panelGrafik is never used.
heinkurz 13-Dec-22 9:03am    
ok - and how can I do that?
Richard MacCutchan 13-Dec-22 9:40am    
Are you the same person as pitwi who posted this question?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900