INTP Forum  

Go Back   INTP Forum > Within > Science & Technology

Reply
 
Thread Tools Search this Thread Display Modes
Old 23rd-December-2016, 06:53 AM   #1
Animekitty
A royal color
 
Animekitty's Avatar
 

Join Date: Apr 2010
Location: Your Heart
Posts: 3,541
windows_98_nt_2000safari
Default Java Button

I am trying to make java program with a start button to begin a thread. The problem is that I need the thread to stop inside the run method because when I press the start button twice it gives me an error message. I also need to draw numbers from a loop in the thread onto the JFrame with paint. I pass the numbers with variable (i) into paint. The number should change with every repaint. I am not sure what I am doing wrong in my code. If someone could check it out I would be grateful.

Code:
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package javaapplication5;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.GridBagLayout;
import java.awt.event.*;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

/**
 *
 * @author Jeremy
 */

class MyClass implements Runnable {
    public void run (){
        for(int i=0; i<10; i++){
        repaint(i);
        System.out.println("Error value: " + i);
        }
        requestStop();
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    public void requestStop() {
        Thread.interrupted();
    }
}

public class JavaApplication5 extends JPanel{

    /**
     * @param args the command line arguments
     */
    int i = 0;
    
    public void paint(Graphics g) { 
        int i = 0;
        g.drawString("Watch the number change:" + i , 50, 50);        
    }
    
    public static void main(String[] args) {
        JPanel p = new JPanel(new GridBagLayout());
        JTextField jt = new JTextField(20);
        JButton b = new JButton("number");
        JFrame f = new JFrame("Recurrent Neural Net");
        f.add(new JavaApplication5());
        f.setSize((516), (539));
        f.setVisible(true);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        p.add(b);
        p.add(jt);
        f.add(p);
        
        Thread t = new Thread(new MyClass());
        
        b.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                t.start();     
            }          
        });
    }   
}
Animekitty is offline   Reply With Quote
Old 19th-January-2017, 08:23 PM   #2
Animekitty
A royal color
 
Animekitty's Avatar
 

Join Date: Apr 2010
Location: Your Heart
Posts: 3,541
windows_98_nt_2000safari
Default Re: Java Button

I do not understand code that well.
These two sections in green I cannot figure out.

Code:
package javaapplication5;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.GridBagLayout;
import java.awt.event.*;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class JavaApplication5 extends JPanel{
    
    public static void main(String[] args) {
        JPanel p = new JPanel(new GridBagLayout());
        JTextField jt = new JTextField(20);
        JButton b = new JButton("Start");
        JFrame f = new JFrame("Recurrent Neural Net");
        f.add(new JavaApplication5());
        f.setSize((516), (539));
        f.setVisible(true);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        p.add(b);
        p.add(jt);
        f.add(p);
        
        Thread t = new Thread(new MyClass());
        
        b.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                int x = 0;
                t.start();
                x++;
                
                if(x >= 2) {
                    t.interrupted();
                }
                
            }          
        });
    }
    
    int i = 0;
    public void paint(Graphics g) {
        int i = 0;
        g.drawString("Watch the number change:" + i , 50, 50);        
    }
}

class MyClass implements Runnable {
    public void run (){
        for(int i=0; i<10; i++){
        //main.paint(i);
        System.out.println("Error value: " + i);
        }
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
Animekitty is offline   Reply With Quote
Old 19th-January-2017, 09:45 PM   #3
Blarraun
straightedgy
 
Blarraun's Avatar
 

Join Date: Nov 2013
Location: someplace windswept
Posts: 4,101
windows_98_nt_2000firefox
Default Re: Java Button

actionPerformed() keeps track of :

x is 0

t.start is a start() method of object t of a constructor class thread T of class MyClass

x is postfix incremented to 1

if(x >= 2) is the break/interrupt routine for actionPerformed counter on x=2

t.interrupted() is the actual interrupt method for the object/thread t

it's what's going to display the error message after 2 actions, button presses

main.paint(i) is commented out and doesn't run in the code, if it did it would probably make use of the object main and method drawstring of the function paint that's probably inherited from one of the import graphics libraries from the beginning

You can find the .interrupted() method of MyClass in your previous post. Which is used for interrupting the thread t.
Spoiler:
public void requestStop() { Thread.interrupted(); } }
Try running it without the interrupt exception and explain what's not showing the way you expect it to show, I don't understand what you expect it to do.
__________________
Spoiler:
~life is the exercise of will
Many valued logic
mail preferred

Blarraun is offline   Reply With Quote
Old 20th-January-2017, 12:12 AM   #4
Animekitty
A royal color
 
Animekitty's Avatar
 

Join Date: Apr 2010
Location: Your Heart
Posts: 3,541
windows_98_nt_2000safari
Default Re: Java Button

Quote:
Originally Posted by Blarraun View Post
You can find the .interrupted() method of MyClass in your previous post. Which is used for interrupting the thread t.
Spoiler:
public void requestStop() { Thread.interrupted(); } }
Try running it without the interrupt exception and explain what's not showing the way you expect it to show, I don't understand what you expect it to do.
Press button once = thread start

main.paint(i); should paint i to the gui as a loop (1 then 2 then 3 ...)

press button a second time = thread stop

---

t.stop(); does not work. it does not stop the thread so I did x >= 2 to represent the button pressed twice. When x = 2 then start button is pressed twice and the thread should stop. action listener should stop the thread when start button is pressed twice.

main.paint(i); when not commented out sends errors and the program does not run.

Animekitty is offline   Reply With Quote
Old 20th-January-2017, 07:11 PM   #5
Blarraun
straightedgy
 
Blarraun's Avatar
 

Join Date: Nov 2013
Location: someplace windswept
Posts: 4,101
windows_98_nt_2000firefox
Default Re: Java Button

I don't see how you change i in the "watch this number change" so that it increases. You have some loop in implements runnable.

I'm sick and too distracted to try it on my own, but one of the best ways to learn functionality is focus on one function at a time.

If you want to start and stop threads, then write the most basic program you can just for that and confirm that it works, then write another one for counting starts and try to link the two later. Now you're stuck in a situation where you have to fix two functions in a single program without altering the rest that you wrote.

If an app crashes or doesn't deliver the results you like then setup breakpoints in code and follow it step by step watching the critical variables change. Not sure which IDE you're using but Eclipse certainly allows you to debug / breakpoint the code, it's also free and open source and all that jazz.

Also some info that might be of use is that Java suggests you stop using .stop and find another way of ending threads like .interrupt or other method. As far as I know in java you don't have to completely close threads as long as you cease its functionality, java does the rest for you.
__________________
Spoiler:
~life is the exercise of will
Many valued logic
mail preferred

Blarraun is offline   Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:53 AM.


Powered by: vBulletin
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Template-Modifications by TMS
no new posts