add files

This commit is contained in:
saad 2023-10-07 19:01:57 -04:00
parent 5523fe0240
commit 9ff7f6c8fd
5 changed files with 157 additions and 0 deletions

View File

@ -1,2 +1,3 @@
# ProcessingSwingDemo
A way to integrate Swing and Processing. Seems to be deprecated but an interesting entry point for rapid prototyping of applications.

BIN
core-2.2.1.jar Normal file

Binary file not shown.

23
src/GUI.java Normal file
View File

@ -0,0 +1,23 @@
import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.*;
public class GUI extends JFrame{
private ShakerPanel myShakerPanel;
public GUI() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
myShakerPanel = new ShakerPanel();
myShakerPanel.init();
this.add(myShakerPanel);
}
public static void main(String[] args) {
GUI mainPanel = new GUI();
mainPanel.pack();
//mainPanel.setSize(400, 400);
mainPanel.setResizable(false);
mainPanel.setVisible(true);
}
}

76
src/ShakerController.java Normal file
View File

@ -0,0 +1,76 @@
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ShakerController extends JFrame {
private ShakerPanel myShakerPanel;
private JTextField myShiftField;
private JButton myStartButton, myPauseButton;
// continued from previous slide
class StartButtonListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent ae) {
myShakerPanel.setRunning(true);
myStartButton.setEnabled(false);
myPauseButton.setEnabled(true);
}
}
class PauseButtonListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent ae) {
myShakerPanel.setRunning(false);
myStartButton.setEnabled(true);
myPauseButton.setEnabled(false);
}
}
class ShiftFieldListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent ae) {
try {
myShakerPanel.setShift(Integer.parseInt(myShiftField.getText()));
} catch (Exception e) {
myShiftField.setText("Invalid shift value. Please try again.");
}
}
}
public ShakerController() {
setTitle("Shaker");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new BorderLayout());
myShakerPanel = new ShakerPanel();
myShakerPanel.init();
add(myShakerPanel, BorderLayout.NORTH);
// a control panel
JPanel controlPanel = new JPanel(new FlowLayout());
myStartButton = new JButton("Start");
myStartButton.setEnabled(false);
myStartButton.addActionListener(new StartButtonListener());
controlPanel.add(myStartButton);
myPauseButton = new JButton("Pause");
myPauseButton.setEnabled(true);
myPauseButton.addActionListener(new PauseButtonListener());
controlPanel.add(myPauseButton);
controlPanel.add(new JLabel("Shift factor:"));
myShiftField = new JTextField(3);
myShiftField.setText(String.format("%d", myShakerPanel.getShift()));
controlPanel.add(myShiftField);
myShiftField.addActionListener(new ShiftFieldListener());
add(controlPanel, BorderLayout.CENTER);
}
public static void main(String[] args) {
ShakerController controller = new ShakerController();
controller.setResizable(false);
controller.pack();
controller.setVisible(true);
}
} // closes ShakerController class declaration

57
src/ShakerPanel.java Normal file
View File

@ -0,0 +1,57 @@
import processing.core.PApplet;
/**
* ShakerPanel1 is a simple Java encapsulation of a shaker animation from Processing.
* It draws a shaking circle and allows the user to reposition the circle using a mouse click.
*
* @author kvlinden
* @version Fall, 2009
*/
public class ShakerPanel extends PApplet {
private static final int SIZE = 300;
private int myX, myY, myShift;
private boolean myRunningStatus;
//constructor
public ShakerPanel() {
myX = myY = SIZE / 2;
myShift = 10;
myRunningStatus = true;
}
public void setup() {
size(SIZE, SIZE);
smooth();
background(255);
}
public void draw() {
if (myRunningStatus) {
background(255);
strokeWeight(2);
ellipse(myX + random(-myShift / 2, myShift / 2),
myY + random(-myShift / 2, myShift / 2), 50, 50);
}
}
public void mousePressed() {
myX = mouseX;
myY = mouseY;
}
public int getShift() {
return myShift;
}
public void setShift(int shift) throws Exception {
if (shift < 0) {
throw new Exception("invalid shift value: " + shift);
}
myShift = shift;
}
public void setRunning(boolean status) {
myRunningStatus = status;
}
}