Somehow fixed the problem with the timer activating too soon by changing the timer start to when it is in the "inner process" of the main method

This commit is contained in:
cs-powell
2024-12-17 14:18:53 -05:00
parent 33a4b8eb7e
commit 0eafd83c5b
15 changed files with 267 additions and 75 deletions

View File

@@ -0,0 +1,11 @@
package Visualizer;
import javax.swing.*;
import java.awt.*;
public class Screen extends JPanel {
public Screen(LayoutManager l) {
this.setLayout(l);
this.setVisible(true);
}
}

View File

@@ -0,0 +1,20 @@
package Visualizer;
import javax.script.ScriptEngine;
import javax.swing.*;
import java.awt.*;
public class ScreenFrame extends JFrame {
ScreenManager sm;
public ScreenFrame(ScreenManager sm, Dimension d) {
this.sm = sm;
this.setPreferredSize(d);
}
public void initialize() {
this.setVisible(true);
this.setFocusable(true);
this.add(sm);
this.pack();
}
}

View File

@@ -0,0 +1,80 @@
package Visualizer;
import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class ScreenManager extends JPanel {
JPanel display = new JPanel(new BorderLayout());
Screen[] screens = new Screen[2];
ScreenType currentScreen;
JButton toggle = new JButton("Cycle Screens");
public ScreenManager(Screen start, Screen data) {
super(new BorderLayout());
this.setVisible(true);
screens[0] = start;
screens[1] = data;
toggle.setVisible(true);
toggle.addActionListener(e -> {
if (currentScreen == ScreenType.START) {
switchScreens(ScreenType.DATA);
} else {
switchScreens(ScreenType.DATA2);
}
// if (currentScreen == ScreenType.DATA2) {
// switchScreens(ScreenType.DATA);
// }
});
this.add(toggle, BorderLayout.WEST);
currentScreen = ScreenType.START;
display.add(screens[0]);
this.add(display, BorderLayout.CENTER);
display.setBackground(Color.black);
}
public void switchScreens(ScreenType st) {
display.removeAll();
switch(st) {
case START:
System.out.println("Switch to start");
display.add(screens[0],BorderLayout.CENTER);
currentScreen = ScreenType.START;
display.repaint();
revalidate();
break;
case DATA:
System.out.println("Switch to data");
display.add(screens[1],BorderLayout.CENTER);
currentScreen = ScreenType.DATA;
display.repaint();
revalidate();
break;
case DATA2:
System.out.println("Switch to data 2");
display.add(((visualizer)screens[1]).exportDisplay(),BorderLayout.CENTER);
currentScreen = ScreenType.DATA2;
display.repaint();
revalidate();
break;
}
}
}

View File

@@ -0,0 +1,7 @@
package Visualizer;
public enum ScreenType {
START,
DATA,
DATA2
}

View File

@@ -0,0 +1,15 @@
package Visualizer;
import javax.swing.*;
public class StartScreen extends JPanel {
public StartScreen() {
JLabel l = new JLabel();
l.setText("Hello");
l.setVisible(true);
this.add(l);
}
}

View File

@@ -16,8 +16,7 @@ public class axis extends JComponent {
yBound = currentY;
}
public void paint(Graphics g)
{
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.green);
g2.drawLine(xBound/2, 0, xBound/2, yBound);

View File

@@ -5,17 +5,13 @@ import java.awt.Graphics;
import java.awt.*;
public class rudderPosition extends JComponent {
int xBound = 0;
int yBound = 0;
float rudderTravel = 0;
rudderPosition(int currentXBound, int currentYBound) {
xBound = currentXBound;
yBound = currentYBound;
}
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
int spacer = 10;
@@ -23,9 +19,9 @@ public class rudderPosition extends JComponent {
g2.fillRect(0, spacer, xBound, yBound - spacer);
g2.setColor(Color.red);
int width = (int) (xBound/2 * rudderTravel);
int width = (int) (xBound / 2 * rudderTravel);
//g2.drawRect(xBound/2, 0 +spacer, width, yBound);
g2.fillRect(xBound/2, 0 + spacer, width, yBound);
g2.fillRect(xBound / 2, 0 + spacer, width, yBound);
g.drawString("Yaw Left",0, yBound/2);
int textWidth = g.getFontMetrics().stringWidth("Roll Right");

View File

@@ -9,9 +9,8 @@ import java.io.IOException;
import CognitiveModel.ModelFiles.*;
public class visualizer {
public class visualizer extends Screen {
JFrame frame = new JFrame();
JPanel frameBottom = new JPanel();
JPanel controlDisplay = new JPanel();
JPanel modelDisplay = new JPanel();
@@ -27,25 +26,42 @@ public class visualizer {
JLabel two = new JLabel();
JLabel three = new JLabel();
JLabel four = new JLabel();
Timer t = new Timer(1, null);
Model m;
public visualizer(Model m){
public visualizer(Model m) {
super(new BorderLayout());
this.m = m;
t.addActionListener(e -> {
try {
if (m.exportXPC().getCTRL(0) != null) { // Check to Make sure the XPC is actually receiving data
m.next();
updateVisualizer(m);
} else {
System.out.print("XPC not returning data yet");
}
} catch (IOException ex) {
throw new RuntimeException(ex);
}
});
initializeDisplay();
}
public void initializeDisplay() {
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
} catch(Exception ignored){}
frame.setPreferredSize(new Dimension(1700,270));
frame.setLayout(new BorderLayout());
frameBottom.setLayout(new GridLayout(1,2));
frame.setVisible(true);
// try {
// UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
// } catch(Exception ignored){
//
// }
this.setPreferredSize(new Dimension(1700,270));
this.setVisible(true);
// frame.setPreferredSize(new Dimension(1700,270));
// frame.setLayout(new BorderLayout());
// frame.setVisible(true);
frameBottom.setLayout(new GridLayout(1,2));
controlDisplay.setPreferredSize(new Dimension(100,100));
@@ -136,14 +152,14 @@ public class visualizer {
ActionListener press = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(m.logPermission() == false){
if(m.logPermission() == false) {
m.startLog();
JOptionPane.showMessageDialog(frame,"Data Logging Enabled");
JOptionPane.showMessageDialog(visualizer,"Data Logging Enabled");
tool.setBackground(Color.green);
b1.setText("Data Log Enabled");
} else {
m.stopLog();
JOptionPane.showMessageDialog(frame,"Data Logging Disabled");
JOptionPane.showMessageDialog(visualizer,"Data Logging Disabled");
tool.setBackground(Color.white);
b1.setText("Data Log Disabled");
}
@@ -157,7 +173,7 @@ public class visualizer {
tool.setOrientation(1);
tool.add(b1);
tool.add(b2);
frame.add(tool,BorderLayout.WEST);
this.add(tool,BorderLayout.WEST);
@@ -199,35 +215,36 @@ public class visualizer {
frameBottom.add(mainDisplay); // Left Side
frameBottom.add(visualizer); // Right Side
frame.add(frameBottom,BorderLayout.CENTER);
frame.add(buttonBar, BorderLayout.SOUTH);
frame.pack();
this.add(frameBottom,BorderLayout.CENTER);
this.add(buttonBar, BorderLayout.SOUTH);
// frame.pack();
}
public void updateVisualizer(Model m) throws IOException {
float[] ctrl1 = m.getCurrentControls();
one.setText(String.valueOf(ctrl1[0]));
if(ctrl1[0] >= 0 ) {
if (ctrl1[0] >= 0 ) {
one.setForeground(Color.green);
} else {
one.setForeground(Color.red);
}
two.setText(String.valueOf(ctrl1[1]));
if(ctrl1[1] >= 0 ) {
if (ctrl1[1] >= 0 ) {
two.setForeground(Color.green);
} else {
two.setForeground(Color.red);
}
three.setText(String.valueOf(ctrl1[2]));
if(ctrl1[2] >= 0 ) {
if (ctrl1[2] >= 0 ) {
three.setForeground(Color.green);
} else {
three.setForeground(Color.red);
}
four.setText(String.valueOf(ctrl1[3]));
if(ctrl1[3] >= 0 ) {
if (ctrl1[3] >= 0 ) {
four.setForeground(Color.green);
} else {
four.setForeground(Color.red);
@@ -244,7 +261,6 @@ public class visualizer {
rudderGrid.setX(ctrl1[2]);
rudderGrid.repaint();
modelQueue.setText(m.getQueue().queueToString());
modelQueue.setPreferredSize(new Dimension(mainDisplay.getWidth(),100));
@@ -253,7 +269,6 @@ public class visualizer {
modelVision.setText("We just looked at: " + m.getStoredVisionTarget() +"\n" +
"Result: " + m.getStoredVisionResult()[0]);
// yaw.setYBound(grid.getHeight());
// yaw.setYBound(grid.getHeight());
// yaw.setX(ctrl1[2]);
@@ -262,8 +277,17 @@ public class visualizer {
axis.setXBound(yokeGrid.getWidth());
axis.setYBound(yokeGrid.getHeight());
axis.repaint();
JButton j = new JButton();
}
public JPanel exportDisplay() {
return visualizer;
}
public Timer exportTimer() {
return t;
}
}