// Import statements import javax.swing.JFrame; import java.awt.Image; import java.awt.image.BufferedImage; import java.awt.Point; import java.awt.Color; import java.awt.Graphics; import javax.swing.JLabel; /** This class allows simple pictures to be manually draw on the screen. */ public class Artist extends JLabel { // The number of pixels per inch private static final int PIXELS_TO_INCHES = 50; // The default width of the frame the Artist object draws on. private static final int DEFAULT_WIDTH = 900; // The default height of the frame the Artist object draws on. private static final int DEFAULT_HEIGHT = 900; // The current position of the Artist's "pen" private Point currentPosition; // An auxiliary image to draw on so that drawing can be done outside of the paintComponent method. private Image image; /** Constructs an Artist object and displays its frame to the screen. */ public Artist() { // Creates the frame JFrame frame = new JFrame(); // Sets the frame up frame.setDefaultLookAndFeelDecorated(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT); setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT); frame.setVisible(true); frame.getContentPane().add(this); // Initializes the current position currentPosition = new Point(PIXELS_TO_INCHES, PIXELS_TO_INCHES); // Initializes the auxiliary image image = new BufferedImage(DEFAULT_WIDTH, DEFAULT_HEIGHT, BufferedImage.TYPE_INT_RGB); // Sets the background color to white Graphics g = image.getGraphics(); g.setColor(Color.white); g.fillRect(0, 0, DEFAULT_WIDTH, DEFAULT_HEIGHT); } /** This method does the actual painting to the screen. */ protected void paintComponent(Graphics g) { g.drawImage(image, 0, 0, this); } /** This method draws a line of length length up from the current position, * and sets the current position equal to the end of that line. * @param length The length of the line to be drawn. */ public void drawLineUp(int length) { Graphics g = image.getGraphics(); g.setColor(Color.black); // Calculates the new position int ny = currentPosition.y - (length * PIXELS_TO_INCHES); // Draws the line g.drawLine(currentPosition.x, currentPosition.y, currentPosition.x, ny); // Updates the position currentPosition.y = ny; } /** This method draws a line of length length down from the current position, * and sets the current position equal to the end of that line. * @param length The length of the line to be drawn. */ public void drawLineDown(int length) { Graphics g = image.getGraphics(); g.setColor(Color.black); // Calculates the new position int ny = currentPosition.y + (length * PIXELS_TO_INCHES); // Draws the line g.drawLine(currentPosition.x, currentPosition.y, currentPosition.x, ny); // Updates the position currentPosition.y = ny; } /** This method draws a line of length length left from the current position, * and sets the current position equal to the end of that line. * @param length The length of the line to be drawn. */ public void drawLineLeft(int length) { Graphics g = image.getGraphics(); g.setColor(Color.black); // Calculates the new position int nx = currentPosition.x - (length * PIXELS_TO_INCHES); // Draws the line g.drawLine(currentPosition.x, currentPosition.y, nx, currentPosition.y); // Updates the position currentPosition.x = nx; } /** This method draws a line of length length right from the current position, * and sets the current position equal to the end of that line. * @param length The length of the line to be drawn. */ public void drawLineRight(int length) { Graphics g = image.getGraphics(); g.setColor(Color.black); // Calculates the new position int nx = currentPosition.x + (length * PIXELS_TO_INCHES); // Draws the line g.drawLine(currentPosition.x, currentPosition.y, nx, currentPosition.y); // Updates the position currentPosition.x = nx; } /** Moves the current position d inches right of the current position. * @param d The number of inches to move the current position. */ public void moveRight(int d) { currentPosition.x += (PIXELS_TO_INCHES * d); } /** Moves the current position d inches left of the current position. * @param d The number of inches to move the current position. */ public void moveLeft(int d) { currentPosition.x -= (PIXELS_TO_INCHES * d); } /** Moves the current position d inches up from the current position. * @param d The number of inches to move the current position. */ public void moveUp(int d) { currentPosition.y -= (PIXELS_TO_INCHES * d); } /** Moves the current position d inches down fromthe current position. * @param d The number of inches to move the current position. */ public void moveDown(int d) { currentPosition.y += (PIXELS_TO_INCHES * d); } }