import java.util.Scanner; import javax.swing.JFrame; /** * Demonstrate how to have the contents of a frame change on demand. * */ public class OliveViewer { /** * Displays a frame with a moveable olive. Demonstrates updating a scene. * * @param args * unused */ public static void main(String[] args) { // The frame is the window for the whole game. // It'll be 600 pixels wide and 300 pixels high. // It will close when the game ends. JFrame frame = new JFrame("The Teleporting Olive"); final int width = 600; final int height = 600; frame.setSize(width, height); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // This is where we instantiate the olive. Olive olive = new Olive(100, 100); // Add it to the frame so it can be displayed. frame.add(new OliveComponent(olive)); // Display the initial olive. frame.setVisible(true); // Get new coordinates for the olive. // I'm going to use Scanner to get input. The Dialog // box would have worked too. Scanner in = new Scanner(System.in); System.out.print("The olive is about to teleport. " + "Enter its new x coordinate (an int): "); int x = in.nextInt(); System.out.print("Enter its new y coordinate (an int): "); int y = in.nextInt(); // Change the olive olive.setCoordinates(x, y); // Update the scene frame.repaint(); } }