#Swing Finishing Help

7 messages · Page 1 of 1 (latest)

turbid minnow
#

I posted about this project at 6ish AM, and I got some really good advice! I am nearly finished with the project now, and I wanted to ask for help with three quick, distinct issues. Help with any of them would be great.

  1. I wanted it to happen so that whenever I pressed a certain button (The "generate" button) my program would change the graphics on screen, including the stats in the JLabels. I found out through a println that the ActionListener i set up is being entered and things are being changed, but the graphics already up aren't changing. My code is a mess, but I'm gonna shove it in the thread.

  2. Essentially, I set up an infinite loop on purpose in the program so that it would continuously cycle over and over again until it was closed. I thought the "frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);" line i set up would close the window, but it isn't; any tips for what to do instead to make something run forever until the button is repressed or the app is closed? (also, any solution that makes it go slower would also be very cool)

rancid emberBOT
#

This post has been reserved for your question.

Hey @turbid minnow! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

turbid minnow
#
JButton gen = new JButton("Generate!");
        gen.addActionListener(new ActionListener(){  
            public void actionPerformed(ActionEvent e){  
                //Creates a tons of new ponds with the slider's count of ducks, and updates stats.
                int succ = 0, trial = 0;
                double freq = 0;
                do {
                    //new pond
                    int[] newDucks = new int[ducksSlider.getValue()];
                    for (int i = 0; i < newDucks.length; i++) {
                        newDucks[i] = (int)(Math.random()*361);
                    }
                    ((PondGraphics) pondPanel).setDucks(newDucks);
                    ((PondGraphics) pondPanel).setAngle(angleSlider.getValue());
                    
                    //newShit
                    int highDuck = 0;
                    int lowDuck = 0;
                    for (int i = 1; i < newDucks.length; i++) {
                        if (newDucks[i] > newDucks[highDuck])
                            highDuck = i;
                        if (newDucks[i] < newDucks[lowDuck])
                            lowDuck = i;
                    }
                    trial++;
                    if ((Math.abs(newDucks[highDuck]-newDucks[lowDuck])<((PondGraphics) pondPanel).getAngle())
                        ||(Math.abs(newDucks[lowDuck]-newDucks[highDuck])<((PondGraphics) pondPanel).getAngle()))
                        succ++;
                    
                    //newStats
                    successes.setText("Successes: " + succ);
                    trials.setText("Trials: " + trial);
                    relFreq.setText("Relative Frequency: " + ((double)succ/(double)trial));
                }
                while (true);
            }  
        });```
coarse mortar
turbid minnow
# coarse mortar Could you clarify these things for me: 1. What do you mean by graphics ? The obj...

alright, so i DID end up figuring things out. i actually forgot i had this thread open until now, but i do really appreciate the attempted help :)

if you want to know what was wrong:

  1. the overarching issue was that i was trying to make an infinite loop in a constructor which went about as well as you would expect; it prevented me from using repaint() on any components, thus, nothing really worked.
  2. the infinite loop was 1000% NOT necessary, and prevented me from closing the window. i did end up having to implement a button instead of just having smooth video, but it seems that was fine for this assignment actually.
#

anyways, i really do hope this thread sticks around for you to see after i use close (haven't really messed with Discord forums yet), but thank you again!