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 BouncingBall extends Frame implements ActionListener,WindowListener,AdjustmentListener,ComponentListener,Runnable, MouseListener, MouseMotionListener
- {
- 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;
- private int X1 = 0;
- private int Y1 = 0;
- private int X2 = 0;
- private int Y2 = 0;
- private boolean rightMouseClicked = false;
- BouncingBall() {
- initComponents();
- sizeElements();
- start();
- }
- public static void main(String[] args) throws IOException
- {
- new BouncingBall();
- }
- 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);
- canvas.addMouseListener(this);
- canvas.addMouseMotionListener(this);
- }
- 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.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) {}
- //=======================================================MOUSE EVENTS
- public void mouseDragged(MouseEvent e) {
- if(rightMouseClicked == false)
- {
- System.out.println("Dragged");
- X2 = e.getX();
- Y2 = e.getY();
- System.out.println(X2 +" "+ Y2);
- canvas.setRectPerameters(X1,Y1, X2, Y2);
- }
- System.out.println("Right Mouse Clicked: " + rightMouseClicked);
- }
- public void mouseMoved(MouseEvent e) {
- }
- public void mouseClicked(MouseEvent e) {
- Point clk = new Point(0, 0);
- if( e.getButton() == MouseEvent.BUTTON3)
- {
- System.out.println("delete");
- clk = e.getPoint();
- canvas.deleteRectangle(clk);
- }
- }
- public void mouseEntered(MouseEvent e) {
- }
- public void mouseExited(MouseEvent e) {
- System.out.println("Exited");
- }
- public void mousePressed(MouseEvent e) {
- if(e.getButton() == MouseEvent.BUTTON3)
- {
- rightMouseClicked = true;
- }
- if(rightMouseClicked == false)
- {
- System.out.println("Pressed");
- X1 = e.getX();
- Y1 = e.getY();
- System.out.println(X1 +" "+ Y1);
- }
- }
- public void mouseReleased(MouseEvent e) {
- if(rightMouseClicked == false)
- {
- canvas.storeRectangle();
- }
- rightMouseClicked = false;
- }
- /*public void drawRect()
- {
- int length = Math.abs(X1-X1);
- int height = Math.abs(Y1-Y2);
- }*/
- }
- 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 Graphics og;
- private Image Buffer;
- 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;
- private boolean addRect = false;
- //=======================Variables for rectangles
- //===========Rectangle
- private Rectangle newRect = new Rectangle(0, 0, 0, 0);
- private Vector<Rectangle> Walls = new Vector<Rectangle>();
- //==============================================
- 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;
- }
- paint(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;
- paint(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 CheckEdge()
- {
- if(CurrPos.x>=width-CurrSize)
- {
- CurrPos.x = width-CurrSize;
- }
- if(CurrPos.y>=height-CurrSize)
- {
- CurrPos.y = height-CurrSize;
- }
- }
- public void paint(Graphics g)
- {
- //under normal circumstances, LastPos and LastSize would be set at the end of this function, but
- //due to threading, artifacting can occur at fast enough resizes if we do not store them into temp vars
- //and use the temp variables as the true purpose
- Vec2 templastpos = LastPos;
- int templastsize = LastSize;
- LastPos = CurrPos;
- LastSize = CurrSize;
- Buffer = createImage(this.width, this.height);
- if (og != null)
- {
- og.dispose();
- }
- og = Buffer.getGraphics();
- try
- {
- drawOval(og);
- //if(addRect == true)
- //{
- drawRect(og);
- //}
- }
- catch (Exception e)
- {}
- g.drawImage(Buffer, 0, 0, null);
- }
- 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);
- }
- public void setRectPerameters(int X1, int Y1, int X2, int Y2)
- {
- int temp = 0;
- if(X1 > X2)
- {
- temp = X1;
- X1 = X2;
- X2 = temp;
- //System.out.println(X1+ " " +X2);
- }
- if(Y1 > Y2)
- {
- temp = Y1;
- Y1 = Y2;
- Y2 = temp;
- ///System.out.println(Y1+ " " +Y2);
- }
- newRect.x = X1;
- newRect.y = Y1;
- newRect.width = Math.abs(X2-X1);
- newRect.height = Math.abs(Y2-Y1);
- //System.out.println(" WIDTH:" + newRect.width+ " " + "HEIGHT:" + newRect.height);
- addRect = true;
- paint(this.getGraphics());
- }
- public void drawRect(Graphics g)
- {
- System.out.println("In drawRect");
- g.setColor(Color.PINK);
- g.fillRect(newRect.x, newRect.y, newRect.width, newRect.height);
- int i = 0;
- while (i < Walls.size())
- {
- g.setColor(Color.PINK);
- g.fillRect(Walls.elementAt(i).x, Walls.elementAt(i).y, Walls.elementAt(i).width, Walls.elementAt(i).height);
- i++;
- }
- addRect = false;
- }
- public void storeRectangle()
- {
- Rectangle RectTemp = new Rectangle( newRect);
- System.out.print("Adding walls");
- Walls.add(RectTemp);
- newRect.setBounds(0,0,0,0);
- System.out.print("Wall size:"+ Walls.size());
- }
- public void deleteRectangle(Point clk)
- {
- int i = 0;
- boolean deleted = false;
- while(i < Walls.size() && deleted == false)
- {
- System.out.println("INside the loop" + clk);
- if (Walls.elementAt(i).contains(clk))
- {
- System.out.println(Walls.elementAt(i).contains(clk));
- Walls.removeElementAt(i);
- deleted = true;
- }
- else
- {
- paint(this.getGraphics());
- i++;
- }
- System.out.println("After delete, wall size is: " + Walls.size());
- }
- addRect = true;
- paint(this.getGraphics());
- addRect = false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement