Java中完成可拖放圖片剪裁入門教程。本站提示廣大學習愛好者:(Java中完成可拖放圖片剪裁入門教程)文章只能為提供參考,不一定能成為您想要的結果。以下是Java中完成可拖放圖片剪裁入門教程正文
這是一篇入門級文章,高手請略過。
在這篇文章中我們將進修若何用 Java 對圖象停止剪裁並將剪裁出來的部門零丁保留到文件中。
我們將經由過程以下步調來進修:
1.輸出圖象,指定要處置的圖象途徑
2.許可用戶拖放要剪裁的部門
3.選擇後應用 Robot 類來肯定剪裁部門的坐標
4.剪裁所選圖象並堅持
接上去我們開端編碼部門。
Listing1: 引入的類
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
解釋:
1.Graphics 類包括繪制矩形的辦法
2.我們應用 Rectangle 類作為拖沓的矩形區域以作剪裁
3.Robot 類用於捕捉屏幕截圖
4.應用鼠標偵聽器來獲得鼠標的拖沓時光
5.Robot 類應用 BufferedImage 來做圖象處置
6.File 類用於翻開圖象文件
7.ImageIO 類用於將圖象寫到 png 或許 jpg 圖象文件中
8.JFrame 用於顯示界面
如今我們編寫包括 main 辦法的進口類
Listing2: 進口類
public class CropImage extends JFrame implements MouseListener, MouseMotionListener
{
int drag_status=0,c1,c2,c3,c4;
public static void main(String args[])
{
new CropImage().start();
}
解釋:
1.編寫了一個名為 CropImage 的類
2.該類擴大了 JFrame 以完成 frame 的一切功效
3.完成了分歧的鼠標事宜偵聽器以便曉得用戶甚麼時刻開端拖動鼠標指針
4.drag_status 變量用於保留鼠標開端拖動時的坐標
5.我們界說了 main 辦法去挪用一個 start 辦法,這個辦法將鄙人面界說
接上去是 start 辦法
Listing 2
public void start()
{
ImagePanel im=new ImagePanel("F:\\Wallpaper\\wallpapers\\1.jpg");
add(im);
setSize(400,400);
setVisible(true);
addMouseListener(this);
addMouseMotionListener( this );
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
解釋:
1.我們界說一個名為 ImagePanel 的類,應用要處置的圖象作為參數
2.在 JFrame 中放置這個要顯示圖片的 ImagePanel,並開端偵聽鼠標事宜
上面我們界說用於處置鼠標事宜的辦法
Listing 3: 鼠標事宜處置函數
@Override
public void mouseClicked(MouseEvent arg0) {
}
@Override
public void mouseEntered(MouseEvent arg0) {
}
@Override
public void mouseExited(MouseEvent arg0) {
}
@Override
public void mousePressed(MouseEvent arg0) {
repaint();
c1=arg0.getX();
c2=arg0.getY();
}
@Override
public void mouseReleased(MouseEvent arg0) {
repaint();
if(drag_status==1)
{
c3=arg0.getX();
c4=arg0.getY();
try
{
draggedScreen();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
@Override
public void mouseDragged(MouseEvent arg0) {
repaint();
drag_status=1;
c3=arg0.getX();
c4=arg0.getY();
}
@Override
public void mouseMoved(MouseEvent arg0) {
}
public void paint(Graphics g)
{
super.paint(g);
int w = c1 - c3;
int h = c2 - c4;
w = w * -1;
h = h * -1;
if(w<0)
w = w * -1;
g.drawRect(c1, c2, w, h);
}
解釋:
1.當鼠標按下時存儲以後坐標到 c1 和 c2
2.當鼠標按下並開端拖動時將拖動狀況變量 drag_status 設置為 true
3.到鼠標按鍵松開時表現圖象剪裁區域曾經選擇終了,挪用 draggedscreen 辦法
4.paint 辦法用於拖動時刻的矩形顯示,經由過程以後坐標和初始記載的坐標來繪制矩形
上面是 draggedscreen 辦法的代碼
Listing 4: draggedScreen 辦法
public void draggedScreen()throws Exception
{
int w = c1 - c3;
int h = c2 - c4;
w = w * -1;
h = h * -1;
Robot robot = new Robot();
BufferedImage img = robot.createScreenCapture(new Rectangle(c1, c2,w,h));
File save_path=new File("screen1.jpg");
ImageIO.write(img, "JPG", save_path);
System.out.println("Cropped image saved successfully.");
}}
解釋:
1.起首盤算圖象的高度和寬度
2.應用 Robot 類來對剪裁的區域停止截圖並堅持到別的一個文件 screen1.jpg
完全的代碼
Listing 5: ImagePanel.java
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
class ImagePanel extends JPanel {
private Image img;
public ImagePanel(String img) {
this(new ImageIcon(img).getImage());
}
public ImagePanel(Image img) {
this.img = img;
Dimension size = new Dimension(img.getWidth(null), img.getHeight(null));
// Dimension size = new Dimension(10,10);
setPreferredSize(size);
setMinimumSize(size);
setMaximumSize(size);
setSize(size);
setLayout(null);
}
public void paintComponent(Graphics g) {
g.drawImage(img, 0, 0, null);
}
}
Listing 6:CropImage.java
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
public class CropImage extends JFrame implements MouseListener, MouseMotionListener
{
int drag_status=0,c1,c2,c3,c4;
public static void main(String args[])
{
new CropImage().start();
}
public void start()
{
ImagePanel im=new ImagePanel("F:\\Wallpaper\\wallpapers\\1.jpg");
add(im);
setSize(400,400);
setVisible(true);
addMouseListener(this);
addMouseMotionListener( this );
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void draggedScreen()throws Exception
{
int w = c1 - c3;
int h = c2 - c4;
w = w * -1;
h = h * -1;
Robot robot = new Robot();
BufferedImage img = robot.createScreenCapture(new Rectangle(c1, c2,w,h));
File save_path=new File("screen1.jpg");
ImageIO.write(img, "JPG", save_path);
System.out.println("Cropped image saved successfully.");
}
@Override
public void mouseClicked(MouseEvent arg0) {
}
@Override
public void mouseEntered(MouseEvent arg0) {
}
@Override
public void mouseExited(MouseEvent arg0) {
}
@Override
public void mousePressed(MouseEvent arg0) {
repaint();
c1=arg0.getX();
c2=arg0.getY();
}
@Override
public void mouseReleased(MouseEvent arg0) {
repaint();
if(drag_status==1)
{
c3=arg0.getX();
c4=arg0.getY();
try
{
draggedScreen();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
@Override
public void mouseDragged(MouseEvent arg0) {
repaint();
drag_status=1;
c3=arg0.getX();
c4=arg0.getY();
}
@Override
public void mouseMoved(MouseEvent arg0) {
}
public void paint(Graphics g)
{
super.paint(g);
int w = c1 - c3;
int h = c2 - c4;
w = w * -1;
h = h * -1;
if(w<0)
w = w * -1;
g.drawRect(c1, c2, w, h);
}
}