diff --git a/README.md b/README.md index 4d9c14f..04f2338 100644 --- a/README.md +++ b/README.md @@ -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. \ No newline at end of file diff --git a/core-2.2.1.jar b/core-2.2.1.jar new file mode 100644 index 0000000..0ab6130 Binary files /dev/null and b/core-2.2.1.jar differ diff --git a/src/GUI.java b/src/GUI.java new file mode 100644 index 0000000..49cb672 --- /dev/null +++ b/src/GUI.java @@ -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); + } +} diff --git a/src/ShakerController.java b/src/ShakerController.java new file mode 100644 index 0000000..605adb0 --- /dev/null +++ b/src/ShakerController.java @@ -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 + \ No newline at end of file diff --git a/src/ShakerPanel.java b/src/ShakerPanel.java new file mode 100644 index 0000000..085bf2d --- /dev/null +++ b/src/ShakerPanel.java @@ -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; + } +} \ No newline at end of file