Читайте также:
|
|
Рис.1 Диалог выбора файла.
Рис.3 Поворот изображения в программе.
Листинг программы
package image_view;
import java.awt.EventQueue;
import java.awt.Graphics2D;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.BorderLayout;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JMenuBar;
import javax.swing.AbstractAction;
import java.awt.event.ActionEvent;
import javax.swing.Action;
import java.awt.event.ActionListener;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileWriter;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
public class window {
String filepath = null;
File[] files = null;
int count = 0;
BufferedImage bi = null;
private JFrame frame;
private final Action action = new SwingAction();
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue. invokeLater (new Runnable() {
public void run() {
try {
window window = new window();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public window() throws IOException {
initialize();
}
ImageIcon fOpen = new ImageIcon("images/document-open-6.png");
ImageIcon fPrev = new ImageIcon("images/draw-triangle1.png");
ImageIcon fNext = new ImageIcon("images/draw-triangle2.png");
ImageIcon fClock = new ImageIcon("images/edit-redo-8.png");
ImageIcon fCounter = new ImageIcon("images/edit-undo-8.png");
/**
* Initialize the contents of the frame.
*/
private void initialize() throws IOException {
frame = new JFrame("Java Image Viewer");
frame.setBounds(100, 100, 640, 480);
frame.setDefaultCloseOperation(JFrame. EXIT_ON_CLOSE);
JMenuBar menuBar = new JMenuBar();
frame.setJMenuBar(menuBar);
final JFileChooser fc = new JFileChooser("C:\\");
final processImage pi = new processImage();
final buttonPerformer bt = new buttonPerformer();
JButton btnNewButton = new JButton(fOpen);
menuBar.add(btnNewButton);
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
int returnVal = fc.showOpenDialog(null);
if (returnVal == JFileChooser. APPROVE_OPTION)
{
filepath = bt.getFilePath(fc);
BufferedImage pic = bt.getImage(filepath);
String dir = bt.getDir(fc);
files = bt.getFilesInDir(dir);
bt.showImage(pic);
frame.revalidate();
bi = pic;
}
}
});
JButton button = new JButton(fPrev);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
if (count == 1){
count = files.length;
}
filepath = files[count--].getAbsolutePath();
BufferedImage pic = bt.getImage(filepath);
bt.showImage(pic);
frame.revalidate();
bi = pic;}
});
menuBar.add(button);
JButton button_1 = new JButton(fNext);
button_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
if (count == files.length) {
count = 0;
}
filepath = files[count++].getAbsolutePath();
BufferedImage pic = bt.getImage(filepath);
bt.showImage(pic);
frame.revalidate();
bi = pic;}
});
menuBar.add(button_1);
JButton btnRotateLeft = new JButton(fCounter);
btnRotateLeft.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
bi = pi.rotateImage(bi, Rotation. COUNTERCLOCKWISE);
bt.showImage(bi);
frame.revalidate();
}
});
menuBar.add(btnRotateLeft);
JButton btnRotateRight = new JButton(fClock);
btnRotateRight.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
bi = pi.rotateImage(bi, Rotation. CLOCKWISE);
bt.showImage(bi);
frame.revalidate();
}
});
menuBar.add(btnRotateRight);
}
private class SwingAction extends AbstractAction {
public SwingAction() {
putValue(NAME, "SwingAction");
putValue(SHORT_DESCRIPTION, "Some short description");
}
public void actionPerformed(ActionEvent e) {
}
}
private class buttonPerformer
{
private buttonPerformer()
{
// return null;
}
private String getFilePath(JFileChooser fc)
{
return fc.getSelectedFile().getAbsolutePath();
}
private String getDir(JFileChooser fc)
{
return fc.getSelectedFile().getParent();
}
private BufferedImage getImage(String filepath)
{
File file = new File(filepath);
BufferedImage image = null;
try {
image = ImageIO. read (file);
} catch (IOException e) {
e.printStackTrace();
}
return image;
}
private void showImage(BufferedImage img)
{
JLabel lblNewLabel = new JLabel(new ImageIcon(img));
frame.getContentPane().removeAll();
if (img.getWidth() > frame.getWidth() || img.getHeight() > frame.getHeight())
{
frame. resize (img.getWidth()+10,img.getHeight()+10);
}
frame.getContentPane().add(lblNewLabel, BorderLayout. CENTER);
}
private File[] getFilesInDir(String dirName)
{
File dir = new File(dirName);
return dir.listFiles(new FilenameFilter() {
public boolean accept(File dir, String filename)
{ return filename.endsWith(".jpg"); }
});
}
}
private class processImage
{
private processImage()
{}
private BufferedImage scaleImage(BufferedImage img, int width, int height)
{
BufferedImage newImage = new BufferedImage(width,height,BufferedImage. TYPE_INT_RGB);
Graphics2D g = newImage.createGraphics();
try {
g.setRenderingHint(RenderingHints. KEY_INTERPOLATION, RenderingHints. VALUE_INTERPOLATION_BILINEAR);
g.clearRect(0, 0, width, height);
g.drawImage(img, 0, 0, width, height, null);
} finally {
g.dispose();
}
return newImage;
}
private BufferedImage rotateImage(BufferedImage img, Rotation rotation) {
int w = img.getWidth();
int h = img.getHeight();
BufferedImage rot = new BufferedImage(h, w, BufferedImage. TYPE_INT_RGB);
double theta;
switch (rotation) {
case CLOCKWISE:
theta = Math. PI / 2;
break;
case COUNTERCLOCKWISE:
theta = -Math. PI / 2;
break;
default:
throw new AssertionError();
}
AffineTransform xform = new AffineTransform();
xform.translate(0.5*h, 0.5*w);
xform.rotate(theta);
xform.translate(-0.5*w, -0.5*h);
Graphics2D g = (Graphics2D) rot.createGraphics();
g.drawImage(img, xform, null);
g.dispose();
return rot;
}
}}
Вывод
В результате написания курсовой работы, были освоены приемы работы с библиотекой графического интерфейса, файловой системой и работа со средой разработки Eclipse IDE.
Дата добавления: 2015-09-11; просмотров: 11 | Поможем написать вашу работу | Нарушение авторских прав |
<== предыдущая лекция | | | следующая лекция ==> |
Теоретическая часть | | | Web-контейнер, Servlet, JSP. Структура, назначение и пример сервлета или JSP. |