Menu

(Solved) : Example 68 Circle Class Intended Drawing Circles Anderson Franceschi Import Javaawtgraphic Q37250894 . . .

Example 6.8:
/** Circle class* intended for drawing circles* Anderson, Franceschi*/
import java.awt.Graphics;import java.awt.Color;
public class Circle{private int x, y, diameter; // x, y coordinates of upper leftcorner// diameter of the circleprivate Color color; // filled color of circle
/** Default constructor* sets (x,y) to upper left corner of window* sets default diameter of 10 pixels* sets default color to black*/public Circle(){x = 0;y = 0;diameter = 10;color = Color.BLACK;}
/** Overloaded constructor* @param sX x value upper left corner bounding rectangle* @param sY y value upper left corner bounding rectangle* @param sDiameter diameter of circle* @param sColor color of circle*/public Circle( int sX, int sY, int sDiameter, Color sColor){x = sX;y = sY;diameter = sDiameter;color = sColor;}
/** accessor for x* @return current value of x*/public int getX( ){return x;}
/** accessor for y* @return current value of y*/public int getY( ){return y;}
/** accessor for diameter* @return current value of diameter*/public int getDiameter( ){return diameter;}
/** accessor for color* @return current value of color*/public Color getColor( ){return color;}
/** mutator for x* @param newX new value for x*/public void setX( int newX ){x = newX;}
/** mutator for y* @param newY new value for y*/public void setY( int newY ){y = newY;}
/** mutator for diameter* @param newDiameter new value for diameter*/public void setDiameter( int newDiameter ){diameter = newDiameter;}
/** mutator for color* @param newColor new value for color*/public void setColor( Color newColor ){color = newColor;}
/** draw thecircle* @param g Graphics object*/public void draw( Graphics g ){g.setColor( color );g.fillOval( x, y, diameter, diameter );}}

/* Pause class* Anderson, Franceschi*/
public class Pause{/** wait method* @param seconds number of seconds to pause*/public static void wait( double seconds ){try{Thread.sleep( (int)( seconds * 1000 ) );}catch ( InterruptedException e ){e.printStackTrace( );}}}

/* A Graphical Application RollABall, version 2Anderson, Franceschimchang 20190222*/
import javax.swing.JFrame;import java.awt.Graphics;import java.awt.Color;import javax.swing.JOptionPane;
public class RollABall2 extends JFrame{public void paint( Graphics g ){super.paint( g );// include graphics code herefinal int X = 10; // the x value of the ballfinal int Y = 50; // the y value of the ballfinal int DIAMETER = 15; // the diameter of the ballfinal Color COLOR = Color.BLUE; // the color of the ballfinal int SPACER = 5; // space between balls
// instantiate the ball as a Circle objectCircle ball = new Circle( X, Y, DIAMETER, COLOR );
// get ball diameter and width & height of the appletwindowint ballDiameter = ball.getDiameter( );int windowWidth = getWidth( );int windowHeight = getHeight( );
// rolling horizontally// check whether ball is at right edge of windowwhile ( ball.getX( ) + ballDiameter < windowWidth ){ball.draw( g ); // draw the ball
Pause.wait( 0.03 ); // wait 3/100th of a second
// clear the windowg.clearRect( 0, 0, windowWidth, windowHeight );
// position to next location for drawing ballball.setX( ball.getX( ) + SPACER ); // increment x by 2}
ball.draw( g ); // draw the ball in the current position}  public static void main( String [] args ){RollABall2 app = new RollABall2( );app.setSize( 400, 300 );app.setVisible( true );}}
3. Animation program. Refer to Example 6.8. When the ball touches the edge of the window, the ball will rebound.

FindMaximumGr FindMaximumGr grades.txt 0.06 KB 2019/3/7 ade.java 0.88 KB 2019/3/7 ade.class 1.28 KB 2019/3/7

Circle.java 2.15 KB 2019/3/7 Pause.javaRollABall2.java 0.33 KB 2019/3/7 1.55 KB 2019/3/73. Animation program. Refer to Example 6.8. When the ball touches the edge of the window, the ball will rebound. FindMaximumGr FindMaximumGr grades.txt 0.06 KB 2019/3/7 ade.java 0.88 KB 2019/3/7 ade.class 1.28 KB 2019/3/7 Circle.java 2.15 KB 2019/3/7 ‘ Pause.javaRollABall2.java 0.33 KB 2019/3/7 ‘ 1.55 KB 2019/3/7 Show transcribed image text

Expert Answer


Answer to Example 68 Circle Class Intended Drawing Circles Anderson Franceschi Import Javaawtgraphic Q37250894 . . .

OR