Click here to Skip to main content
15,901,122 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi!
A view weeks ago I changed from Visual Studio C# to Java and now I've tried my first project with Swing and WindowBuilder. So far so good and everything works fine - except drawing that line. There's no error message but also no line. Neither paint(... nor paintComponents(... gets a call. What's wrong?
Thanks.

package Test2GUI;

//import java.time.*;
import java.time.format.*;
import java.time.temporal.ChronoUnit;
import java.awt.EventQueue;
import javax.swing.*;
import java.awt.*;
//import java.awt.BorderLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
//import java.awt.geom.*;
import java.time.LocalDate;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Line2D;
//import java.awt.BasicStroke;
import javax.swing.JFrame;
//import javax.swing.SwingUtilities;


public class Test2GUI extends JFrame {

	public static JFrame frmBiorhythmus;
	public static JTextField textFieldD1;
	public static JTextField textFieldD2;
	public static JLabel lblNewLabelTage = new JLabel("");

	/**
	 * 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) {
				LocalDate d2 = LocalDate.now();
				DateTimeFormatter dFormat = DateTimeFormatter.ofPattern("dd.MM.yyyy");
				textFieldD2.setText(d2.format(dFormat));
			}
		});
		frmBiorhythmus.setResizable(false);
		frmBiorhythmus.setTitle("Biorhythmus");
		frmBiorhythmus.setBounds(100, 100, 516, 519);
		frmBiorhythmus.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		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);
		
		textFieldD2 = new JTextField();
		textFieldD2.setText("01.01.2022");
		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();
			}
		});
		btnNewButtonOK.setBounds(285, 60, 105, 23);
		frmBiorhythmus.getContentPane().add(btnNewButtonOK);
		lblNewLabelTage.setVerticalAlignment(SwingConstants.TOP);
		
		lblNewLabelTage.setBounds(153, 136, 237, 116);
		frmBiorhythmus.getContentPane().add(lblNewLabelTage);
		
		centreWindow(frmBiorhythmus);
	}


	static void centreWindow(Window frame) {
		Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
		int x = (int) ((dimension.getWidth() - frame.getWidth()) / 2);
		int y = (int) ((dimension.getHeight() - frame.getHeight()) / 2);
		frame.setLocation(x, y);
	}


	static void GetData() {
	   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
	   // ...

	   // calc
	   double d = ChronoUnit.DAYS.between(d1, d2);
	   int k = (int) (Math.sin((d / 23 - Math.rint(d / 23)) * Math.PI * 2) * 100);
	   int s = (int) (Math.sin((d / 28 - Math.rint(d / 28)) * Math.PI * 2) * 100);
	   int g = (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 += "körperlich: " + (k >= 0 ? "+" : "") + k + "%<br>";
	   txt += "seelisch: " + (s >= 0 ? "+" : "") + s + "%<br>";
	   txt += "geistig: " + (g >= 0 ? "+" : "") + g + "%<br>";
	   lblNewLabelTage.setText(txt);

	   // some basics
	   double x0 = frmBiorhythmus.getWidth() / 2;
	   double y0 = frmBiorhythmus.getHeight() - lblNewLabelTage.getAlignmentY() + lblNewLabelTage.getHeight() - 50;
	   double dx = frmBiorhythmus.getWidth() * 0.8 / 200;
	   double dy = (frmBiorhythmus.getHeight() - x0 - 20) / 10;
	   frmBiorhythmus.revalidate();
       frmBiorhythmus.repaint();
	}


	public void paint(Graphics g) {
		super.paint(g);
		Graphics2D g2 = (Graphics2D) g;
		Line2D lin = new Line2D.Float(0, 0, 2500, 2000);
		g2.draw(lin);
		JOptionPane.showMessageDialog(null, "#1", "Info", JOptionPane.INFORMATION_MESSAGE);
	}

	public void paintComponent(Graphics g){
        super.paintComponents(g);
        g.drawLine(0,20,100,20);
		JOptionPane.showMessageDialog(null, "#2", "Info", JOptionPane.INFORMATION_MESSAGE);
    }


}


What I have tried:

Some swing drawing examples from the net.
Posted
Updated 7-Dec-22 22:26pm
Comments
Richard MacCutchan 8-Dec-22 4:20am    
So somewhere in all that code something is not happening ... but you decline to explain where or what.

1 solution

 
Share this answer
 

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