// DrawImageFromFile.java (based on BasicGraphics.java) // Place an image named "sampleImage.jpg" in the same directory as // this file, then compile and run this code to display the image. import java.awt.*; import java.awt.event.*; import java.util.*; import javax.swing.*; import java.awt.geom.*; public class DrawImageFromFile extends JComponent { // Convert a jpg or png file into an awt Image (this is a quick way; // we really should use an ImageObserver or some other more reliable way). public static Image getImageFromFile(String filename) { return new ImageIcon(filename).getImage(); } // Load the image only once -- so make it static private static Image sampleImage = getImageFromFile("sampleImage.jpg"); // Sample paint method: public void paint(Graphics page) { // get the dimensions of the draw area int width = this.getWidth(); int height = this.getHeight(); // paint the background a pleasing olive green to complement our image page.setColor(new Color(120,120,50)); page.fillRect(0,0,width,height); // center the full-sized, non-rotated image in the window paintCenteredImage(page, sampleImage, width/2, height/2, 1.0, 0.0); // now draw a version of the image with its left-top at (0,0) and scaled to be smaller paintImage(page, sampleImage, 0, 0, (1.0/5.0), 0.0); // and draw a 1/5th version in the top-right corner rotated by 45 degrees // This requires that we obtain the size of the rotated image so we // can compute its resulting left-top Dimension newSize = getImageSize(sampleImage, (1.0/5.0), Math.toRadians(45)); double newLeft = width - newSize.getWidth(); double newTop = 0; paintImage(page, sampleImage, newLeft, newTop, (1.0/5.0), Math.toRadians(45)); } // Paint the given image rotated and scaled with a top-left at x,y public void paintImage(Graphics page, Image image, double x, double y, double scale, double radians) { double imageWidth = image.getWidth(null); double imageHeight = image.getHeight(null); Dimension newSize = getImageSize(image, scale, radians); AffineTransform transform = new AffineTransform(); transform.translate(x+newSize.getWidth()/2,y+newSize.getHeight()/2); // last (not first!) transform.rotate(radians); transform.scale(scale,scale); transform.translate(-imageWidth/2, -imageHeight/2); // first ((Graphics2D)page).drawImage(image,transform,null); } // Same as paintImage, only here we position the image by its center public void paintCenteredImage(Graphics page, Image image, double cx, double cy, double scale, double radians) { Dimension newSize = getImageSize(image, scale, radians); paintImage(page, image, cx - newSize.getWidth()/2, cy - newSize.getHeight()/2, scale, radians); } // get the size of this image after it has been scaled and rotated public Dimension getImageSize(Image image, double scale, double radians) { double imageWidth = image.getWidth(null); double imageHeight = image.getHeight(null); AffineTransform transform = new AffineTransform(); transform.rotate(radians); transform.scale(scale,scale); transform.translate(-imageWidth/2, -imageHeight/2); // first double[] x = { 0, imageWidth, imageWidth , 0 }; double[] y = { 0, 0 , imageHeight, imageHeight }; double minx=0, maxx=0, miny=0, maxy=0; Point2D.Double src = new Point2D.Double(), dst = new Point2D.Double(); for (int i=0; i<4; i++) { src.setLocation(x[i],y[i]); transform.transform(src,dst); if (i == 0) { minx = maxx = dst.getX(); miny = maxy = dst.getY(); } else { minx = Math.min(dst.getX(),minx); miny = Math.min(dst.getY(),miny); maxx = Math.max(dst.getX(),maxx); maxy = Math.max(dst.getY(),maxy); } } return new Dimension((int)(maxx-minx), (int)(maxy-miny)); } public static void main(String[] args) { JComponent myGraphics = new DrawImageFromFile(); launch(myGraphics, 600, 500); // Set the initial dimensions here! } ////////////////////////////////////////////////////////////////////////////// ///////////////// END OF YOUR CODE ///////////////////// ///////////////// (you may ignore all the code below!!! ///////////////////// ////////////////////////////////////////////////////////////////////////////// public static void launch(JComponent jc, int width, int height) { JFrame frame = new JFrame(jc.getClass().getName()); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel cp = new JPanel(); cp.setLayout(new BorderLayout()); cp.add(jc); frame.setContentPane(cp); frame.setSize(new Dimension(width,height)); frame.setVisible(true); } }