Once again, I need help. Recursion and displaying multiple images in a window.
Hi all,
Once again I am in need of help. I am working on a program for my data structures class and I am ready to pull my hair out. Its almost the end of my semester and I have several programs to complete with little help from my prof.
The assignment is to write a program that displays images in a window and then recursively tiling the images to each corner. The program needs to prompt the user for 3 things, a scaling factor (.1 - .9) for decreasing the size of the images, a minimal size in pixels to scale down to, and 4 different images to be tiled.
I have been able to get it working using one image, but when I prompt the user for a second image file, the program will only display the second file. I could be wrong and it may display the first image fast and then repaint with the second image and I am just not seeing it. Right now, I have it set to recursively tile a single image to all for corners of the window. I need to be able to have different images at each corner, but I don't know how to do it. I can promt the user for more than one file, but I get lost after that.
Any help will be appreciated. I've been working on this for days and my professor isn't a big help. He just answers questions with questions and confuses everyone more than they were previously.
Below is my code...
Thanks
Code:
import java.util.*;
import java.awt.Graphics;
import java.awt.Panel;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;
public class ImageRecursion extends Panel {
private static BufferedImage image;
private int size;
private double sc;
private static int imageWidth;
private static int imageHeight;
private static int i = 1;
public ImageRecursion(File file, double scale, int minSize)
{
size = minSize;
sc = scale;
try
{
image = ImageIO.read(file);
}
catch (IOException ie)
{
ie.printStackTrace();
}
}
public void paint( Graphics g) //paints the original image in the entire window
{
imageHeight = image.getHeight();
imageWidth = image.getWidth();
g.drawImage(image, 0, 0, imageWidth, imageHeight, null);
tileImage(imageWidth, imageHeight, image, g);
}
private void tileImage(int width, int height, BufferedImage img, Graphics g) //paints the recursive images
{
if(height > size)
{
g.drawImage(img, 0, 0, width, height, null); //top left
g.drawImage(img, imageWidth - width, 0, width, height, null); //top right
g.drawImage(img, 0, imageHeight - height, width, height, null); //bottom left
g.drawImage(img, imageWidth - width, imageHeight - height, width, height, null); //bottom right
tileImage((int)(width * sc), (int)(height * sc), img, g);
}
}
static public void main(String args[]) throws Exception {
JFrame frame = new JFrame("Image Resizing");
String minSizeIn = JOptionPane.showInputDialog("Enter minimum image size in pixels: ");
int mSI = Integer.parseInt(minSizeIn);
String scaleFactor = JOptionPane.showInputDialog("Enter scaling factor in fractional notation (.1 to .9): ");
double sF = Double.parseDouble(scaleFactor);
// while (i <=4)
{
FileFilter ff = new FileNameExtensionFilter("JPG, GIF, & PNG Image Files", "jpg","jpeg", "gif", "png");
JFileChooser file1 = new JFileChooser();
file1.addChoosableFileFilter(ff);
int returnVal = file1.showDialog(null, "Open file");
if (returnVal == JFileChooser.APPROVE_OPTION)
{
File file = file1.getSelectedFile();
Panel panel = new ImageRecursion(file,sF,mSI);
frame.getContentPane().add(panel);
//i++;
}
JFileChooser file2 = new JFileChooser();
file2.addChoosableFileFilter(ff);
int returnVal2 = file2.showDialog(null, "Open file");
if (returnVal2 == JFileChooser.APPROVE_OPTION)
{
File file = file2.getSelectedFile();
Panel panel = new ImageRecursion(file,sF,mSI);
frame.getContentPane().add(panel);
//i++;
}
frame.setSize(image.getWidth(), image.getHeight()+40);
//frame.setSize(imageWidth, imageHeight);
//frame.setSize(1000, 1000);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
//i++;
}
}
}