Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Homework 4: Ball Bouncer thingy
- Paul MacLean (MAC7537@calu.edu), Michael Gorse (GOR9632@calu.edu), Anthony Carrola (CAR3766@calu.edu)
- Group 8 (2^3)
- CET 350 - Technical Computer using Java
- */
- import java.io.*;
- import java.awt.*;
- import java.util.*;
- import java.util.concurrent.TimeUnit;
- import java.lang.*;
- import java.awt.List;
- import java.awt.event.*;
- class Bounce extends Frame implements ActionListener,WindowListener,AdjustmentListener,ComponentListener,Runnable
- {
- private static final long serialVersionUID = 1L;
- //SCROLLER are only used to get the ratio of the scrollbar, regardless of its purpose.
- private final int SCROLLER_THUMBOFFSET = 10;
- private final int SCROLLER_MIN = 0;
- private final int SCROLLER_MAX = 100;
- private final int SCROLLER_RANGE = SCROLLER_MAX - SCROLLER_MIN;
- private final int SCROLLER_RANGE_VIS = SCROLLER_RANGE - SCROLLER_THUMBOFFSET;
- private final int SCROLLER_DEFAULT = SCROLLER_RANGE_VIS / 2;
- private final int SCROLLER_BLOCKVAL = SCROLLER_RANGE_VIS / 10;
- //SPEED constants
- private final float SPEED_DELTA_MIN = 1; //milliseconds
- private final float SPEED_DELTA_MAX = 100; //milliseconds
- private final float SPEED_RANGE = Math.abs(SPEED_DELTA_MAX - SPEED_DELTA_MIN); //Range should be positive.
- private final int SPEED_DEFAULT = (int) (SPEED_RANGE / 2 + SPEED_DELTA_MIN); //Half of the range of values, plus the _MIN_ value
- //SIZE constants
- private final int SIZE_MIN = 10;
- private final int SIZE_MAX = 100;
- private final int SIZE_RANGE = Math.abs(SIZE_MAX - SIZE_MIN);
- private static int ButtonHeight = 20;
- int MoveRate = SPEED_DEFAULT;
- private volatile boolean TimePause = true;
- private Thread SteppingThread;
- private Button RunButton = new Button("Run");
- private Button PauseButton = new Button("Pause");
- private Button QuitButton = new Button("Quit");
- private Scrollbar SpeedBar;
- private Scrollbar SizeBar;
- private Label SpeedLabel = new Label("Speed");
- private Label SizeLabel = new Label("Size");
- private CanObj canvas = null;
- Bounce() {
- initComponents();
- sizeElements();
- start();
- }
- public static void main(String[] args) throws IOException
- {
- new Bounce();
- }
- private void initComponents() {
- SpeedBar = new Scrollbar(Scrollbar.HORIZONTAL);
- SpeedBar.setVisible(true);
- SpeedBar.setValues(SCROLLER_DEFAULT, SCROLLER_THUMBOFFSET, SCROLLER_MIN, SCROLLER_MAX);
- SpeedBar.setBlockIncrement(SCROLLER_BLOCKVAL);
- SpeedBar.setUnitIncrement(2);
- SpeedBar.setBackground(Color.ORANGE);
- SpeedBar.addAdjustmentListener(this);
- this.add(SpeedBar);
- SpeedLabel.setAlignment(Label.CENTER);
- SpeedLabel.setVisible(true);
- this.add(SpeedLabel);
- RunButton.setVisible(true);
- RunButton.addActionListener(this);
- this.add(RunButton);
- PauseButton.setVisible(true);
- PauseButton.addActionListener(this);
- this.add(PauseButton);
- QuitButton.setVisible(true);
- QuitButton.addActionListener(this);
- this.add(QuitButton);
- SizeBar = new Scrollbar(Scrollbar.HORIZONTAL);
- SizeBar.setVisible(true);
- SizeBar.setValues(SCROLLER_DEFAULT, SCROLLER_THUMBOFFSET, SCROLLER_MIN, SCROLLER_MAX);
- SizeBar.setBlockIncrement(SCROLLER_BLOCKVAL);
- SizeBar.setUnitIncrement(2);
- SizeBar.setBackground(Color.ORANGE);
- SizeBar.addAdjustmentListener(this);
- this.add(SizeBar);
- SizeLabel.setAlignment(Label.CENTER);
- SizeLabel.setVisible(true);
- this.add(SizeLabel);
- this.setLayout(null);
- this.setBounds(50, 50 , 800, 500);
- this.setVisible(true);
- this.setResizable(true);
- this.setTitle("");
- this.setMinimumSize(new Dimension(700, 400));
- this.addWindowListener(this);
- this.requestFocus();
- canvas = new CanObj(this.getWidth(), this.getHeight()-120, this.getInsets());
- this.addComponentListener(this);
- this.add(canvas);
- canvas.setVisible(true);
- }
- private void sizeElements() {
- int FRAME_X = this.getWidth();
- int FRAME_Y = this.getHeight();
- int ButtonSpace =(int) (0.05 * FRAME_X);
- int ButtonWidth = (int) (0.05 * FRAME_X);
- int NetBarWidth = (int) (0.175 * FRAME_X);
- int MenuElementYPos = FRAME_Y - 60;
- int LabelYPos = MenuElementYPos+20;
- SpeedBar.setBounds(ButtonSpace, MenuElementYPos, NetBarWidth, ButtonHeight);
- SpeedLabel.setBounds(ButtonSpace, LabelYPos, NetBarWidth, ButtonHeight);
- RunButton.setBounds(2*ButtonSpace+NetBarWidth, MenuElementYPos, ButtonWidth,ButtonHeight );
- PauseButton.setBounds(6*ButtonSpace+NetBarWidth, MenuElementYPos, ButtonWidth, ButtonHeight);
- QuitButton.setBounds(10*ButtonSpace+NetBarWidth, MenuElementYPos, ButtonWidth, ButtonHeight);
- SizeBar.setBounds(12*ButtonSpace+NetBarWidth, MenuElementYPos, NetBarWidth, ButtonHeight);
- SizeLabel.setBounds(12*ButtonSpace+NetBarWidth, LabelYPos, NetBarWidth, ButtonHeight);
- canvas.onScreenResize(FRAME_X, MenuElementYPos-20, this.getInsets());
- canvas.CheckEdge();
- }
- //=======================================================METHODS FOR THREAD
- public void start()
- {
- if(SteppingThread == null)
- {
- SteppingThread = new Thread(this);
- SteppingThread.start();
- }
- }
- public void run()
- {
- SteppingThread.setPriority(Thread.MAX_PRIORITY);
- while(true) {
- while(!TimePause)
- {
- try
- {
- SteppingThread.sleep(MoveRate);
- } catch(InterruptedException e){}
- //canvas.repaint();
- canvas.setXandY();
- }
- }
- }
- //=======================================================METHOD TO QUIT
- public void stop()
- {
- SteppingThread.setPriority(Thread.MIN_PRIORITY);
- this.removeWindowListener(this);
- System.exit(0);
- this.dispose();
- }
- //=======================================================METHODS FOR BUTTONS
- public void run_buttonclk()
- {
- SteppingThread.interrupt();
- TimePause = !TimePause;
- if(TimePause) {
- RunButton.setLabel("Run");
- }
- else {
- RunButton.setLabel("Stop");
- try {
- SteppingThread.start();
- } catch(IllegalThreadStateException e) {}
- }
- }
- public void pause_buttonclk()
- {
- //write pause button code here
- }
- //=======================================================WINDOW EVENTS
- public void windowClosed(WindowEvent e) {
- System.exit(0);
- }
- public void windowClosing(WindowEvent e) {
- stop();
- }
- public void windowActivated(WindowEvent e) {}
- public void windowDeactivated(WindowEvent e) {}
- public void windowDeiconified(WindowEvent e) {}
- public void windowIconified(WindowEvent e) {}
- public void windowOpened(WindowEvent e) {}
- //=======================================================INTERACTION EVENTS
- public void actionPerformed(ActionEvent e) {
- Object click = e.getSource();
- if(click == RunButton) {
- run_buttonclk();
- }
- else if(click == PauseButton) {
- pause_buttonclk(); //not functional yet
- }
- else if(click == QuitButton) {
- stop();
- }
- }
- public void adjustmentValueChanged(AdjustmentEvent e) {
- Scrollbar temp = (Scrollbar) e.getSource();
- float SBRatio = (float) temp.getValue() / (float) SCROLLER_RANGE_VIS;
- if (temp == SpeedBar) {
- SBRatio = 1-SBRatio; //This allows the left of the bar to hold a larger value
- MoveRate = (int) ((SBRatio * SPEED_RANGE) + SPEED_DELTA_MIN); //Add min to percentage applied to speed
- } else if (temp == SizeBar) {
- float Size = (SBRatio * SIZE_RANGE) + SIZE_MIN;
- int constrainedsize = canvas.setBallSize((int)Size);
- if (constrainedsize != -1) {
- SizeBar.setValue(constrainedsize);
- }
- }
- }
- public void componentResized(ComponentEvent e) {
- sizeElements();
- }
- public void componentMoved(ComponentEvent e) {}
- public void componentShown(ComponentEvent e) {}
- public void componentHidden(ComponentEvent e) {}
- }
- class CanObj extends Canvas
- {
- private static final long serialVersionUID = 1L;
- private class Vec2 {
- public int x;
- public int y;
- Vec2(int x, int y) {
- this.x = x;
- this.y = y;
- }
- }
- private Vec2 Dir = new Vec2(1,1);
- private Vec2 CurrPos = new Vec2(40,275);
- private Vec2 LastPos = new Vec2(10,10);
- private int LastSize;
- private int CurrSize;
- private int width;
- private int height;
- CanObj(int width, int height, Insets insets) {
- LastSize = 33;
- CurrSize = 33;
- this.setBackground(Color.lightGray);
- onScreenResize(width, height, insets);
- CheckEdge();
- }
- //======================================
- //=======================METHODS
- //======================================
- //=======================SETS/MODIFIERS
- public int setBallSize(int size) { //resizes object based on sb
- int retval;
- LastSize = CurrSize;
- if(size + CurrPos.x + 1 > width || size+CurrPos.y + 1 > height) //ball size too big
- {
- retval = CurrSize;
- }
- else
- {
- retval = -1;
- CurrSize = size;
- }
- update(this.getGraphics());
- return retval;
- }
- public void setXandY()
- {
- Vec2 nextpos = new Vec2(CurrPos.x + Dir.x,CurrPos.y + Dir.y); //make predictive vector
- if(nextpos.x - 1 < 0 || nextpos.x + CurrSize + 1 > width)
- Dir.x *= -1;
- if(nextpos.y - 1 < 0 || nextpos.y + CurrSize + 1 > height)
- Dir.y *= -1;
- LastPos = CurrPos;
- CurrPos = nextpos;
- update(this.getGraphics());
- }
- //======================================
- //=======================GETS
- //======================================
- //======================================
- //=======================REACTORS
- //======================================
- public void onScreenResize(int framewidth, int frameheight, Insets insets) //resizes canvas for screen
- {
- this.width = framewidth - (2*insets.left) - 10;
- this.height = frameheight - insets.top;
- this.setBounds(insets.left+5, insets.top+5, this.width, this.height);
- }
- public void update(Graphics g)
- {
- g.setColor(this.getBackground());
- g.fillOval(LastPos.x, LastPos.y, LastSize, LastSize);
- drawOval(g);
- }
- public void CheckEdge()
- {
- if(CurrPos.x>=width-CurrSize)
- {
- CurrPos.x = width-CurrSize;
- }
- if(CurrPos.y>=height-CurrSize)
- {
- CurrPos.y = height-CurrSize;
- }
- }
- public void paint(Graphics g)
- {
- update(g);
- }
- public void drawOval(Graphics g) //draws oval
- {
- int size = CurrSize;
- Vec2 pos = CurrPos;
- g.setColor(Color.orange);
- g.fillOval(pos.x, pos.y, size, size);
- g.setColor(Color.darkGray);
- g.fillOval(pos.x+1, pos.y+1, size-2, size-2);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement