Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please follow all instructions, thank you. In this homework you will develop a J

ID: 3885245 • Letter: P

Question

Please follow all instructions, thank you.

In this homework you will develop a Java 2D graphics application that creates 5 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 -10 in x direction, Translate +12 in the y direction.
b. Rotate 55 degrees counter clockwise.
c. Rotate 75 clockwise
d. Scale 3 times for the x component, scale 1.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.

Explanation / Answer

package servletdemo;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import java.text.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import javax.swing.*;
import Acme.JPM.Encoders.GifEncoder;

public class ImageContentWithDrawing extends HttpServlet {
/**Initialize global variables*/
private final static int width = 300;
private final static int height = 300;

/**Process the HTTP Get request*/
public void doGet(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {
response.setContentType("image/gif");
OutputStream out = response.getOutputStream();

// Create image
Image image = new BufferedImage(width, height,
BufferedImage.TYPE_INT_ARGB);

// Get Graphics context of the image
Graphics g = image.getGraphics();

drawClock(g); // Draw a clock on graphics

// Encode the image and send to the output stream
new GifEncoder(image, out, true).encode();

out.close(); // Close stream
}

private void drawClock(Graphics g) {
// Initialize clock parameters
int clockRadius =
(int)(Math.min(width, height)*0.7*0.5);
int xCenter = (width)/2;
int yCenter = (height)/2;

// Draw circle
g.setColor(Color.black);
g.drawOval(xCenter - clockRadius,yCenter - clockRadius,
2*clockRadius, 2*clockRadius);
g.drawString("12", xCenter-5, yCenter-clockRadius+12);
g.drawString("9", xCenter-clockRadius+3, yCenter+5);
g.drawString("3", xCenter+clockRadius-10, yCenter+3);
g.drawString("6", xCenter-3, yCenter+clockRadius-3);

// Get current time using GregorianCalendar
TimeZone timeZone = TimeZone.getDefault();
GregorianCalendar cal = new GregorianCalendar(timeZone);

// Draw second hand
int second = (int)cal.get(GregorianCalendar.SECOND);
int sLength = (int)(clockRadius*0.9);
int xSecond =
(int)(xCenter + sLength*Math.sin(second*(2*Math.PI/60)));
int ySecond =
(int)(yCenter - sLength*Math.cos(second*(2*Math.PI/60)));
g.setColor(Color.red);
g.drawLine(xCenter, yCenter, xSecond, ySecond);

// Draw minute hand
int minute = (int)cal.get(GregorianCalendar.MINUTE);
int mLength = (int)(clockRadius*0.75);
int xMinute =
(int)(xCenter + mLength*Math.sin(minute*(2*Math.PI/60)));
int yMinute =
(int)(yCenter - mLength*Math.cos(minute*(2*Math.PI/60)));
g.setColor(Color.blue);
g.drawLine(xCenter, yCenter, xMinute, yMinute);

// Draw hour hand
int hour = (int)cal.get(GregorianCalendar.HOUR_OF_DAY);
int hLength = (int)(clockRadius*0.6);
int xHour = (int)(xCenter +
hLength*Math.sin((hour+minute/60.0)*(2*Math.PI/12)));
int yHour = (int)(yCenter -
hLength*Math.cos((hour+minute/60.0)*(2*Math.PI/12)));
g.setColor(Color.green);
g.drawLine(xCenter, yCenter, xHour, yHour);

// Set display format in specified style, locale, and timezone
DateFormat formatter = DateFormat.getDateTimeInstance
(DateFormat.MEDIUM, DateFormat.LONG);

// Display current date
g.setColor(Color.red);
String today = formatter.format(cal.getTime());
FontMetrics fm = g.getFontMetrics();
g.drawString(today, (width -
fm.stringWidth(today))/2, yCenter+clockRadius+30);
}
}