Question: In this project you will create 3 simple, images or your choice and use Java 2D graphic methods to rotate, scale and translate each of
In this project you will create 3 simple, images or your choice and use Java 2D graphic methods to rotate, scale and translate each of the images.
1. Using Netbeans or Eclipse, develop a Java 2D graphics application that creates 3 images. The images should have the following specifications:
a. Size: minimum 25x25 pixels, larger images are Okay
b. Type: Color (consists of two or more colors)
c. Simple form or shape (Hint: consider a letter or number, or even simple shapes such as crossing lines, rectangles, or circles
d. You should generate the image inside of separate methods and store them as 2D arrays.
2. Use Java 2D graphics to display your original images.
3. For each image use the existing Java 2D graphics transformation methods to translate, rotate and scale each object. You should perform the following transformations on each image:
a. Translate -5 in x direction, Translate +7 in the y direction.
b. Rotate 45 counter clockwise.
c. Rotate 90 degrees clockwise
d. Scale 2 times for the x component, scale 0.5 times for the y component
e. Each of these transformations should be displayed in sequence with the images always starting from the previous transformation as opposed to the original image.
f. Use Java 2D graphics to display each transformation for each image. (Hint: review the Project 1 template for a good start for this project.)
4. All Java source code should be written using Google Java style guide.
5. Prepare, conduct and document a test plan verifying your application is working as expected. This plan should include a test matrix listing each method you tested, how you tested it, and the results of testing.
TEMPLATES:
IMAGE TEMPLATE import java.awt.*; import java.awt.image.BufferedImage; public class ImageTemplate { // Constants // X Size of Images private int x = 20; // Y Size of Images private int y = 20; private int z = 20; BufferedImage image; // Define a Simple Images based on the Alphabet // Use static so can send them into the getImage() method remotely // Note these are 10x10 and can be modified to any desired // image size by adding or removing rows and columns /*public static int[][] letterT = { {0, 1, 1, 1, 1, 1, 1, 1, 1, 0}, {0, 1, 1, 1, 1, 1, 1, 1, 1, 0}, {0, 0, 0, 0, 1, 1, 0, 0, 0, 0}, {0, 0, 0, 0, 1, 1, 0, 0, 0, 0}, {0, 0, 0, 0, 1, 1, 0, 0, 0, 0}, {0, 0, 0, 0, 1, 1, 0, 0, 0, 0}, {0, 0, 0, 0, 1, 1, 0, 0, 0, 0}, {0, 0, 0, 0, 1, 1, 0, 0, 0, 0}, {0, 0, 0, 0, 1, 1, 0, 0, 0, 0}, {0, 0, 0, 0, 1, 1, 0, 0, 0, 0}}; */ public static int[][] matrix = { { 0, 1, 0, 0, 234, 0, 0, 0, 0, 1 }, { 0, 1, 0, 1, 0, 0, 1, 0, 0, 0 }, { 0, 45, 0, 0, 0, 0, 0, 231, 0, 0 }, { 0, 0, 0, 1, 0, 0, 1, 0, 0, 0 }, { 0, 1, 0, 234, 0, 89, 0, 0, 0, 1 } }; /*x = matrix[0].length; y = matrix.length; image = new BufferedImage(x, y, BufferedImage.TYPE_BYTE_GRAY); for (int i = 0; i < y; i++) { for (int j = 0; j < x; j++) { int u = matrix[i][j]; image.setRGB(j, i, u); } } }*/ // You can add more images and interesting shapes here // Methods to apply pixel colors and public BufferedImage getImage(int[][] data) { BufferedImage image = new BufferedImage(x, y, BufferedImage.TYPE_INT_RGB); for (int x = 0; x < x; x++) { for (int y = 0; y < y; y++) { for (int z = 0; z < z; z++) { int pixelColor = data[x][y]; // Set Colors based on Binary Image value if (pixelColor == 0) { pixelColor = Color.WHITE.getRGB(); } else { pixelColor = Color.BLACK.getRGB(); } image.setRGB(x, y, pixelColor); } // End for y. } // End for x. }return image; } // End getData method. } TEMPLATE CLASS
import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.geom.AffineTransform; import java.awt.image.BufferedImage; public class Template extends JPanel{ // A counter that increases by one in each frame. private int frameNumber; // The time, in milliseconds, since the animation started. private long elapsedTimeMillis; // This is the measure of a pixel in the coordinate system // set up by calling the applyLimits method. It can be used // for setting line widths, for example. private float pixelSize; static int translateX = 0; static int translateY = 0; static double rotation = 0.0; static double scaleX = 1.0; static double scaleY = 1.0; ImageTemplate myImages = new ImageTemplate(); BufferedImage tImage = myImages.getImage(ImageTemplate.matrix); /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here JFrame window; window = new JFrame("Java Animation"); // The parameter shows in the window title bar. final CMSC405P1Template panel = new CMSC405P1Template(); // The drawing area. window.setContentPane(panel); // Show the panel in the window. window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // End program when window closes. window.pack(); // Set window size based on the preferred sizes of its contents. window.setResizable(false); // Don't let user resize window. Dimension screen = Toolkit.getDefaultToolkit().getScreenSize(); window.setLocation( // Center window on screen. (screen.width - window.getWidth()) / 2, (screen.height - window.getHeight()) / 2); Timer animationTimer; // A Timer that will emit events to drive the animation. final long startTime = System.currentTimeMillis(); // Taken from AnimationStarter // Modified to change timing and allow for recycling animationTimer = new Timer(1600, new ActionListener() { public void actionPerformed(ActionEvent arg0) { if (panel.frameNumber > 3) { panel.frameNumber = 0; } else { panel.frameNumber++; } panel.elapsedTimeMillis = System.currentTimeMillis() - startTime; panel.repaint(); } }); window.setVisible(true); // Open the window, making it visible on the screen. animationTimer.start(); // Start the animation running. } public CMSC405P1Template() { // Size of Frame setPreferredSize(new Dimension(800, 600)); } // This is where all of the action takes place // Code taken from AnimationStarter.java but modified to add the specific Images // Also added looping structure for Different transformations protected void paintComponent(Graphics g) { /* First, create a Graphics2D drawing context for drawing on the panel. * (g.create() makes a copy of g, which will draw to the same place as g, * but changes to the returned copy will not affect the original.) */ Graphics2D g2 = (Graphics2D) g.create(); /* Turn on antialiasing in this graphics context, for better drawing. */ g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); /* Fill in the entire drawing area with white. */ g2.setPaint(Color.WHITE); g2.fillRect(0, 0, getWidth(), getHeight()); // From the old graphics API! /* Here, I set up a new coordinate system on the drawing area, by calling * the applyLimits() method that is defined below. Without this call, I * would be using regular pixel coordinates. This function sets the value * of the global variable pixelSize, which I need for stroke widths in the * transformed coordinate system. */ // Controls your zoom and area you are looking at applyWindowToViewportTransformation(g2, -75, 75, -75, 75, true); AffineTransform savedTransform = g2.getTransform(); System.out.println("Frame is " + frameNumber); switch (frameNumber) { case 1: // First frame is unmodified. translateX = 0; translateY = 0; scaleX = 1.0; scaleY = 1.0; rotation = 0; break; case 2: // Second frame translates each image by (-9, 5). translateX = -9; translateY = 5; break; case 3: // Third frame rotates each image by 60 degrees Counter translateX = -9; translateY = 5; rotation = 60*Math.PI / 180.0; break; // Can add more cases as needed default: break; } // End switch g2.translate(translateX, translateY); // Move image. // To offset translate again g2.translate(-10,10); g2.rotate(rotation); // Rotate image. g2.scale(scaleX, scaleY); // Scale image. g2.drawImage(tImage, 0, 0, this); // Draw image. g2.setTransform(savedTransform); // Add another T image g2.translate(translateX, translateY); // Move image. // To offset translate again // This allows you to place your images across your graphic g2.translate(-30,30); g2.rotate(rotation); // Rotate image. g2.scale(scaleX, scaleY); // Scale image. g2.drawImage(tImage, 0, 0, this); // Draw image. g2.setTransform(savedTransform); // You can add more shapes/images as needed // } // Method taken directly from AnimationStarter.java Code private void applyWindowToViewportTransformation(Graphics2D g2, double left, double right, double bottom, double top, boolean preserveAspect) { int width = getWidth(); // The width of this drawing area, in pixels. int height = getHeight(); // The height of this drawing area, in pixels. if (preserveAspect) { // Adjust the limits to match the aspect ratio of the drawing area. double displayAspect = Math.abs((double) height / width); double requestedAspect = Math.abs((bottom - top) / (right - left)); if (displayAspect > requestedAspect) { // Expand the viewport vertically. double excess = (bottom - top) * (displayAspect / requestedAspect - 1); bottom += excess / 2; top -= excess / 2; } else if (displayAspect < requestedAspect) { // Expand the viewport vertically. double excess = (right - left) * (requestedAspect / displayAspect - 1); right += excess / 2; left -= excess / 2; } } g2.scale(width / (right - left), height / (bottom - top)); g2.translate(-left, -top); double pixelWidth = Math.abs((right - left) / width); double pixelHeight = Math.abs((bottom - top) / height); pixelSize = (float) Math.max(pixelWidth, pixelHeight); } } Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
