2

I'm working on this Java tutorial and it has me program a window that draws horizontal lines and allows the user to change the distance between the lines. The problem is, everything works splendidly, until I add the variable for the distance to the while loop, then the window goes blank and is unresponsive - but it reports no errors. I have rewritten the whole thing four times now, and have written it in different ways (do-while/while/for) yet the problem seems continuously to be the variable. I have no idea what I'm doing wrong.

Here's the class:

package h03horizontalelijnen2;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Paneel extends JPanel implements ActionListener {

    //declaratie objecten & variabelen
    private int afstand; // variabele voor afstand tussen lijnen
    private int yWaarde; //variabele voor yWaarde lijnen
    private JTextField inputAfstand; // tekstveld voor input afstand
    private JButton tekenKnop; // tekenknop

    public Paneel(){ //bevat tekstveld, knop & label
    //creatie objecten
    inputAfstand = new JTextField("2", 2); //creatie tekstvak: 2 getallen
    inputAfstand.addActionListener(this); //luistert naar actie
    inputAfstand.setToolTipText("Vul in dit vak de afstand tussen de lijnen in"); //tooltip

    tekenKnop = new JButton("Teken lijnen"); //creatie knop
    tekenKnop.addActionListener(this); //luistert naar actie
    inputAfstand.setToolTipText("Klik om de lijnen opnieuw te tekenen"); //tooltip

    //elementen aan paneel toevoegen
    add(new JLabel ("Afstand tussen de lijnen: "));
    add(inputAfstand);
    add(tekenKnop);
    }

    public void paintComponent(Graphics g){ //teken lijnen
        super.paintComponent(g);
        g.setColor(Color.RED); //maak kleur rood
        int onder = getHeight(); //bepaal hoogte scherm
        int midden = getHeight() /2; // midden
        int eindeScherm = getWidth();
        yWaarde = midden; // variabele voor yWaarde, startpunt = midden

        while (yWaarde <= onder) {
            g.drawLine(0, yWaarde, eindeScherm, yWaarde);
            yWaarde = yWaarde + afstand;
        }
    }

    public void bepaalAfstand(){ //haal getal uit inputAfstand tekstvak
        afstand = Integer.parseInt(inputAfstand.getText());
    }

    public void actionPerformed(ActionEvent e) { //klikken triggert:
        bepaalAfstand(); //bepaal input afstand
        repaint(); //opnieuw tekenen

    }

}
3
  • 3
    If paintComponent()is called before bepaalAfstand() is called, afstand will be zero, and your while loop will never end. Commented Jan 25, 2016 at 9:47
  • @khelwood I swapped bepaalAfstand() and paintComponent() around, but the problem persists. Commented Jan 25, 2016 at 9:54
  • 2
    Swapping two methods to try and fix this error makes no sense. You need to think about how you expect paintComponent to behave before bepaalAfstand has been called. It could be as simple as initialising afstand to 1. Commented Jan 25, 2016 at 10:03

2 Answers 2

4

As khelwood already pointed out is the problem that your variable afstand never gets initialized by bepaalAfstand(). It´s 0, so you will never exit the while loop since yWaarde = yWaarde + afstand; will not change the value.

I´m not sure what you are trying to do here since your variable and method names are not in english.

Maybe it´s as simple as add a gard condition to your while look

while (yWaarde <= onder && afstand > 0)

Regards, Rainer

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! I thought that I had initialized it by setting "2" in the textfield from which it was supposed to get its value, but that somehow didn't work. This fixed it.
Please mark my answer as the solving one (if so) - thanks
2

If this:

afstand = Integer.parseInt(inputAfstand.getText());

Results to 0, you will get stuck in your while loop. So if your inputAfstand starts off at 0, you get in trouble.

You can fix it by testing for 0, or initializing inputAfstand to 1.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.