0

Im attempting to make a live view of a controller's inputs in a gui. I'm use the .poll(); method to get new information from the controller's axes. I need to poll it constantly for the elements to move in real time but I can only get either the loop to run or the window to open, not both.

Here's the code that i've boiled down to just a loop and a gui element, this runs the loop and prints out "hello" every second but doesn't open the gui elements:

import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;

public class movedot extends JFrame {

    private JPanel contentPane;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    movedot frame = new movedot();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     * @throws InterruptedException 
     */
    public movedot() throws InterruptedException {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JLabel lblDot = new JLabel("dot");
        lblDot.setBounds(0,0,2,2);
        contentPane.add(lblDot);
        loop();
    /*  while (true){
            lblDot.setBounds(new Rectangle(new Point(x++,y++),lblDot.getPreferredSize()));
        Thread.sleep(1000);
    }*/
    }
    public void loop() throws InterruptedException{
        while (true){
        System.out.println("hello");
        Thread.sleep(1000);
    }
    }
}

And here's what I have with the controller implemented, same with this one, runs the logic and prints out the finalnumber but doesn't open the gui (there's probably a lot of extra stuff in there that I still need to clean up):

package controller2;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import org.lwjgl.LWJGLException;
import org.lwjgl.input.Controller;
import org.lwjgl.input.Controllers;


public class controllergui {

    private JFrame frame;
    static Controller controller;
    public static boolean Start;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    controllergui window = new controllergui();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

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

    /**
     * Initialize the contents of the frame.
     */
    public void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 218, 241);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);





//logic     
        try {
            Controllers.create();
        } catch (LWJGLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
            controller=Controllers.getController(16);
            JLabel label = new JLabel("");
            label.setIcon(new ImageIcon("C:\\Users\\The Pheonix\\workspace\\controller2\\img\\dot.jpg"));
            label.setBounds(100, 100, 2, 2);
            frame.getContentPane().add(label);
            while(true){
                controller.poll();
                Start=controller.isButtonPressed(0);
            //      System.out.println(Start);
            //      System.out.println(controller.getAxisValue(0));
                    float axis1= controller.getAxisValue(1);
                    float axis2= controller.getAxisValue(3);
            //      System.out.println(controller.getAxisValue(2));
            //      System.out.println(controller.getAxisValue(3));
            //      System.out.println(controller.getAxisValue(4));
            //      System.out.println(controller.getAxisValue(5));
                    float axis1Stage1 = axis1 + 1 ;
                    float axis1Stage2 = axis1Stage1 *100;
                    int finalNumber = Math.round(axis1Stage2);
                    System.out.println(finalNumber);
                //  System.out.println(axis3Stage1);


            }
    }
}
  • 2
    1) Don't block the EDT (Event Dispatch Thread). The GUI will 'freeze' when that happens. See [Concurrency in Swing](https://docs.oracle.com/javase/tutorial/uiswing/concurrency/) for details and the fix. 2) See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) .. – Andrew Thompson Nov 24 '16 at 17:31
  • 2
    Your code does not respect Swing threading rules. For more, please see: [Lesson: Concurrency in Swing](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/) – Hovercraft Full Of Eels Nov 24 '16 at 17:31
  • 2
    .. 3) Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson Nov 24 '16 at 17:31

0 Answers0