Wednesday, June 26, 2013

Graphics Examples

package com.spconger.GraphicsExamples;

import acm.graphics.*;
import acm.program.*;
import acm.io.*;

import java.awt.Color;
import java.awt.color.*;

@SuppressWarnings("serial")
public class Examples extends GraphicsProgram {
 /**This class will provide some
  * examples of graphics in acm
  * 6/26/2013
  */
 public void run(){
  //hello();
  println(getParent().getParent().getWidth());
  getParent().getParent().setBounds(400, 500,500,400);
  //setSize(400,500);
  //makeRectangle();
  createOval();
 }
 
 private void hello(){
  IODialog dialog = new IODialog();
  String name = dialog.readLine("Enter your name");
  GLabel label = new GLabel("hello, " + name,50, 20);
  label.setColor(Color.MAGENTA);
  add(label);
  GLabel label2 = new GLabel("the color  of label 1 is "
    + label.getColor(), 20, 100);
  add(label2);
 }
 
 private void makeRectangle(){
  GRect rect = new GRect(40,50, 60, 30);
  rect.setFillColor(Color.PINK);
  rect.setColor(Color.PINK);
  rect.setFilled(true);
  add(rect);
  GRect rect2 = new GRect(50,60, 60, 30);
  rect2.setFillColor(Color.BLUE);
  rect2.setFilled(true);
  add(rect2);
 }
 
 private void createOval(){
  GOval oval = new GOval(50, 60, 120,120);
  oval.setFillColor(Color.GREEN);
  oval.setFilled(true);
  add(oval);
  //take half the value of each height and width
  //and add it to the x y
  GOval oval2=new GOval(80,90,60,60);
  oval2.setFillColor(Color.BLUE);
  oval2.setFilled(true);
  add(oval2);
 }
 
}

Here is the infinite loop with the turtle

package com.spconger.TurtleOne;
import acm.graphics.*;
import acm.program.*;

public class Program extends GraphicsProgram{
 
 public void run(){
  setSize(500,600);
  turtleRun();
  
 }
 
 private void turtleRun(){
  GTurtle portia = new GTurtle(25,50);
  add(portia);
  waitForClick();
  
  while (true){
   if(portia.getX() >= getWidth()){
    portia.setDirection(180);
   }
   else if (portia.getX() <= 0){
    portia.setDirection(0);
   }
   portia.forward(20);
   
  }
 }
 
}

Here is the bouncing ball

package com.spconger.MovingGraphics;
import java.awt.Color;

import acm.graphics.*;
import acm.graphics.GOval;
import acm.program.*;

public class BouncingBall extends GraphicsProgram{
 // it is a good idea to set constants for any numbers
 private final int FORMWIDTH=400;
 private final int FORMHEIGHT=600;
 private final int BALLDIAMETER=30;
 
 
 public void run(){
  setSize(FORMWIDTH, FORMHEIGHT);
  movingBall();
  
 }
 
 private void movingBall(){
  //this places the ball in the center of the screen
  GOval ball = new GOval(FORMWIDTH/2-BALLDIAMETER/2,
    FORMHEIGHT/2-BALLDIAMETER/2,
    BALLDIAMETER,BALLDIAMETER);
  ball.setFillColor(Color.RED);
  ball.setFilled(true);
  add(ball);
  waitForClick();
  int posX = -15;
  int posY=10;
    
  while(true){
   //this makes it so when the ball hits the edges
   //it reverse direction
   if(ball.getX()>=FORMWIDTH-BALLDIAMETER || ball.getX() <=0){
    posX=-posX;
   }
   //same for top and bottom
   if (ball.getY()>=FORMHEIGHT-BALLDIAMETER || ball.getY() <=0){
    posY = -posY;
   }
   
   ball.move(posX, posY);
   //slight pause betwee positions
   pause(50);
  }
 }
 
 

}

No comments:

Post a Comment