導航:首頁 > 編程語言 > java圖片放大

java圖片放大

發布時間:2025-02-18 16:01:41

① 怎麼用java代碼放大或縮小圖片不失真。

放大圖像不會導致失真,而縮小圖像將不可避免的失真。Java中也同樣是這樣。但java提供了4個縮放的微調選項。image.SCALE_SMOOTH //平滑優先image.SCALE_FAST//速度優先image.SCALE_AREA_AVERAGING //區域均值image.SCALE_REPLICATE //像素復制型縮放image.SCALE_DEFAULT //默認縮放模式調用方法Image new_img=old_img.getScaledInstance(1024, 768, Image.SCALE_SMOOTH);得到一張縮放後的新圖。怎麼用java代碼放大或縮小圖片不失真。

② 相冊管理 java實現 功能是圖片顯示與圖片放大縮小 急~~

上樓的我只想說你根本不懂java,這么簡單的功能都不能實現,還是一門編程語言嗎? 一、部分截圖: ①打開 ②放大 ③翻轉 ④縮小 二、源程序: import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.File;
public class PhotoFrame extends JFrame implements ActionListener{ Canvas photo;
JPanel p;
JButton open,bigger,smaller,rotate,exit;
JScrollPane sp;
JFileChooser fc;
int w = 150;
int h = 150;
Image image;
int rate = 10;//圖片放縮率(單位:像素)

public PhotoFrame(){
init();
this.setTitle ("Java圖片查看器");
this.setSize (600,500);
this.setLocationRelativeTo (this);//窗口居中
this.setVisible (true);
this.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

}
public void init(){
photo = new Photo();
sp = new JScrollPane(photo);
p = new JPanel();
open = new JButton("open");
bigger = new JButton(" + ");
smaller = new JButton(" - ");
rotate = new JButton(" の ");
exit = new JButton("exit");
//設置前景色
open.setForeground (Color.BLUE);
bigger.setForeground (Color.GREEN);
smaller.setForeground (Color.GREEN);
rotate.setForeground (Color.GREEN);
exit.setForeground (Color.RED);
//設置提示文本
open.setToolTipText ("打開文件");
bigger.setToolTipText ("放大");
smaller.setToolTipText ("縮小");
rotate.setToolTipText ("翻轉");
exit.setToolTipText ("退出程序");
//設置邊框
p.setBorder (BorderFactory.createEtchedBorder ());
p.add (open);
p.add (bigger);
p.add (smaller);
p.add (rotate);
p.add (exit);
add(sp,BorderLayout.CENTER);
add(p,BorderLayout.SOUTH);
open.addActionListener (this);
bigger.addActionListener (this);
smaller.addActionListener (this);
rotate.addActionListener (this);
exit.addActionListener (this);
}
public static void main(String[] args){
new PhotoFrame();
} public void actionPerformed (ActionEvent e){
if(e.getSource ()==open){//打開
fc = new JFileChooser();
int returnval = fc.showOpenDialog(this);
if(returnval == JFileChooser.APPROVE_OPTION){
File f = fc.getSelectedFile ();
String fileName = f.getName ();
String filePath=fc.getSelectedFile().getAbsolutePath();
System.out.println(filePath);
this.setTitle (fileName+"-Java圖片查看器");
//通過文件路徑獲得圖片
image = new ImageIcon(filePath).getImage ();
//獲取圖片的寬和高
w = image.getWidth (this);
h = image.getHeight (this);
}

}else if(e.getSource ()==bigger){//放大
if(w>0) w+= rate;
else w-= rate;
if(h>0)h+= rate;
else h-= rate;

}else if(e.getSource ()==smaller){//縮小
if(w>0) w-= rate;
else w+= rate;
if(h>0) h-= rate;
else h+= rate;

}else if(e.getSource ()==rotate){//翻轉
if(w>0&&h>0){
h*=-1;
}else if(w>0&&h<0){
w*=-1;
}else if(w<0&&h<0){
h*=-1;
}else if(w<0&&h>0){
w*=-1;
}
}else{//退出
System.exit(0);
}
photo.repaint ();//重新繪制
}

class Photo extends Canvas{

public void paint(Graphics g){

int width = this.getWidth();
int height = this.getHeight();
//設置圖片左上角坐標
int x = (width-w)/2;
int y = (height-h)/2;
//繪制圖片
g.drawImage(image, x, y, w, h,this);

}
}
}
三、補充:1、滾動面板功能沒有具體實現2、放大縮小率應該按照圖片寬高比來設置,最好用一個滾動條來放大縮小3、翻轉功能需要改進 樓主自己試著完善下...

③ 如何用Java實現圖形的放大和縮小

java實現圖形的放大和縮小,其實就是在畫圖時,改變圖片的長和寬。以下代碼參考一下:


importjava.awt.Graphics;
importjava.awt.MouseInfo;
importjava.awt.Point;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjava.awt.event.MouseEvent;
importjava.awt.event.MouseListener;
importjava.io.File;

importjavax.swing.ImageIcon;
importjavax.swing.JButton;
importjavax.swing.JFileChooser;
importjavax.swing.JFrame;
importjavax.swing.JPanel;
importjavax.swing.filechooser.FileNameExtensionFilter;

,ActionListener{

intx=0;
inty=0;
File[]selectedFiles=null;
intfileIndex=0;
intwidth=200;
intheight=200;

publicApp(){

setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setSize(400,300);
setResizable(false);
getContentPane().setLayout(null);

JPanelpanel=newImagePanel();
panel.setBounds(12,40,370,218);
getContentPane().add(panel);

addMouseListener(this);
JButtonbtnBrowse=newJButton("Browse");
btnBrowse.addActionListener(this);
btnBrowse.setBounds(12,9,91,21);
getContentPane().add(btnBrowse);
setVisible(true);
}

publicstaticvoidmain(String[]args){
newApp();
}

publicvoidactionPerformed(ActionEvente){
JFileChooserchooser=newJFileChooser();
chooser.setMultiSelectionEnabled(true);
FileNameExtensionFilterfilter=newFileNameExtensionFilter(
"JPG&GIFImages","jpg","gif");
//設置文件類型
chooser.setFileFilter(filter);
//打開選擇器面板
intreturnVal=chooser.showOpenDialog(this);
if(returnVal==JFileChooser.APPROVE_OPTION){
selectedFiles=chooser.getSelectedFiles();
repaint();
}
}

publicvoidmouseClicked(MouseEvente){

}

publicvoidmouseEntered(MouseEvente){

}

publicvoidmouseExited(MouseEvente){

}

publicvoidmousePressed(MouseEvente){
Pointpoint=MouseInfo.getPointerInfo().getLocation();
x=point.x;
y=point.y;
}

publicvoidmouseReleased(MouseEvente){
Pointpoint=MouseInfo.getPointerInfo().getLocation();
intthisX=point.x;
intthisY=point.y;
System.out.println("thisX="+thisX+""+"thisY="+thisY);
if((y-thisY<20&&y-thisY>0)
||(y-thisY<0&&y-thisY>-20)){
//Y在20范圍內移動認為是水平移動
if(x<thisX){
//right
if(selectedFiles!=null
&&fileIndex<selectedFiles.length-1){
fileIndex++;
}
}else{
//left
if(selectedFiles!=null&&fileIndex>0){
fileIndex--;
}
}
}else{
if(x<thisX){
//右下
width+=20;
height+=20;
}else{
//左上
width-=20;
height-=20;
}
}

repaint();
}

classImagePanelextendsJPanel{

publicvoidpaint(Graphicsg){
super.paint(g);

if(selectedFiles!=null){
ImageIconicon=newImageIcon(selectedFiles[fileIndex]
.getPath());
g.drawImage(icon.getImage(),0,0,width,height,this);
}
}
}
}
閱讀全文

與java圖片放大相關的資料

熱點內容
程序員脖子痛如何緩解 瀏覽:529
java加密aes對稱加密演算法 瀏覽:595
格式工廠視頻壓縮方法 瀏覽:475
編譯後的函數和原始函數如何對應 瀏覽:621
闡述郵件加密解密過程 瀏覽:400
敲沙子聲控解壓 瀏覽:54
計算機教室用什麼伺服器 瀏覽:800
華為暢享9怎麼設置簡訊加密 瀏覽:285
中國現代編譯器 瀏覽:850
如何得到app專欄 瀏覽:453
魔獸世界日本伺服器什麼職業多 瀏覽:729
表格加密怎麼設置只讀模式打開 瀏覽:884
哪個app可以不用花唄分期 瀏覽:860
SSL是對稱加密嗎 瀏覽:46
捷途app鑰匙怎麼用 瀏覽:960
享省油app怎麼在加油站使用 瀏覽:250
crc演算法的實現c語言 瀏覽:187
風光攝影pdf 瀏覽:938
頭部按摩器可以緩解壓力嗎 瀏覽:652
格式工廠壓縮圖片大小 瀏覽:892