For my graphics project, I am trying to recreate the opening scene from the clas
ID: 3569854 • Letter: F
Question
For my graphics project, I am trying to recreate the opening scene from the classic James Bond movies. A white circle starts at the left side of a black screen, then moves to the right, then moves back to the left and stops at the center. The white circle then expands, then fills up with red paint. I'm struggling with getting the circle to stop in the middle and I can't figure out how to get the circle to expand then fill up red. Thank you for looking and all help is appreciated!
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
//import java.awt.Graphics2D;
import java.util.concurrent.TimeUnit;
//import java.util.logging.Level;
//import java.util.logging.Logger;
import javax.swing.JFrame;
public class Jamesbond {
public static void main(String[] args) throws InterruptedException {
// create a MyCanvas object
MyCanvas canvas1 = new MyCanvas();
// set up a fram to hold the canvas object
JFrame frame = new JFrame();
frame.setTitle("James Bond");
frame.setSize(1000, 750);
frame.setLocation(100, 100);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// add the canvas to the frame as a content panel
frame.getContentPane().add(canvas1);
frame.setVisible(true);
} // end main
} // end class
class MyCanvas extends Canvas{
//set coordinates for x axis and y axis for drawing objects to start
int xWhite = 251; // intitial X postion for white circle
int yWhite = 250; // y position of white circle
int xBlack = 250; // intitial X postion for black circle
int yBlack = 250; // y position of black circle
int diameter = 50; // diameter for both circles
@SuppressWarnings("empty-statement")
public void paint(Graphics graphics)
{
int i; // loop counter
//paint the background of the canvas black
graphics.setColor(Color.BLACK);
graphics.fillRect(0, 0, 1000, 750);
// loop to make circles move right
for (i = 1; i != 400; ++i) {
//draw a white circle
graphics.setColor(Color.WHITE);
xWhite++; // add 1 to the white circle's X position to make it appear to move right
graphics.fillOval(xWhite, yWhite, diameter, diameter);
//draw the black circle
graphics.setColor(Color.BLACK);
xBlack++; // // add 1 to the trailing black circle's X position
graphics.drawOval(xBlack, yBlack, diameter, diameter);
// implement a delay after drawing these before next round of the loop
try {
TimeUnit.MILLISECONDS.sleep(5);
} //end try
catch (Exception e) { System.out.println("Exception caught"); } // catch (Exception e)
} // end for
xBlack++; // add 1 to black x position to kind of fix a bad trailing effect
// loop to make circles move left
for (i = 1; i != 275; ++i) {
//draw a white circle
graphics.setColor(Color.WHITE);
xWhite--; // subtract 1 to the white circle's X position to make it appear to move left
graphics.fillOval(xWhite, yWhite, diameter, diameter);
//draw the black circle
graphics.setColor(Color.BLACK);
xBlack--; // // subtract 1 to the trailing black circle's X position
graphics.drawOval(xBlack, yBlack, diameter, diameter);
// implement a delay after drawing these before next round of the loop
try {
TimeUnit.MILLISECONDS.sleep(5);
} //end try
catch (Exception e) { System.out.println("Exception caught"); } // catch (Exception e)
}
// end for
} // end public void paint(Graphics graphics)
} // end class MyCanvas
Explanation / Answer
Program Code:
Note: The code highlighted with bold letters is a add code.
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
//import java.awt.Graphics2D;
import java.util.concurrent.TimeUnit;
//import java.util.logging.Level;
//import java.util.logging.Logger;
import javax.swing.JFrame;
class MyCanvas extends Canvas{
//set coordinates for x axis and y axis for drawing objects to start
int xWhite = 251; // intitial X postion for white circle
int yWhite = 250; // y position of white circle
int xBlack = 250; // intitial X postion for black circle
int yBlack = 250; // y position of black circle
int diameter = 50; // diameter for both circles
@SuppressWarnings("empty-statement")
public void paint(Graphics graphics)
{
int i; // loop counter
//paint the background of the canvas black
graphics.setColor(Color.BLACK);
graphics.fillRect(0, 0, 1000, 750);
// loop to make circles move right
for (i = 1; i != 400; ++i)
{
//draw a white circle
graphics.setColor(Color.WHITE);
xWhite++; // add 1 to the white circle's X position to make it appear to move right
graphics.fillOval(xWhite, yWhite, diameter, diameter);
//draw the black circle
graphics.setColor(Color.BLACK);
xBlack++; // // add 1 to the trailing black circle's X position
graphics.drawOval(xBlack, yBlack, diameter, diameter);
// implement a delay after drawing these before next round of the loop
try
{
TimeUnit.MILLISECONDS.sleep(5);
} //end try
catch (Exception e)
{
System.out.println("Exception caught");
} // catch (Exception e)
} // end for
xBlack++; // add 1 to black x position to kind of fix a bad trailing effect
// loop to make circles move left
for (i = 1; i != 275; ++i)
{
//draw a white circle
graphics.setColor(Color.WHITE);
xWhite--; // subtract 1 to the white circle's X position to make it appear to move left
graphics.fillOval(xWhite, yWhite, diameter, diameter);
//draw the black circle
graphics.setColor(Color.BLACK);
xBlack--; // // subtract 1 to the trailing black circle's X position
graphics.drawOval(xBlack, yBlack, diameter, diameter);
// implement a delay after drawing these before next round of the loop
try
{
TimeUnit.MILLISECONDS.sleep(5);
} //end try
catch (Exception e)
{
System.out.println("Exception caught");
} // catch (Exception e)
}// end for
graphics.setColor(Color.BLACK);
graphics.fillOval( xBlack, yBlack, diameter, diameter);
for(int j=0;j<200;j++)
{
if(j==199) x=j;
graphics.setColor(Color.RED);
graphics.drawOval(xBlack-diameter, yBlack-diameter, xBlack-j-diameter, xBlack-j-diameter);
}
} // end public void paint(Graphics graphics)
} // end class MyCanvas
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.