導航:首頁 > 編程語言 > 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圖片放大相關的資料

熱點內容
怎麼把百度雲資源壓縮 瀏覽:456
不會數學英語如何編程 瀏覽:88
如何能知道網站伺服器地址 瀏覽:648
程序員月薪5萬難嗎 瀏覽:138
如何評價程序員 瀏覽:803
雲虛機和伺服器的區別 瀏覽:403
廣西柳州壓縮機廠 瀏覽:639
arm開發編譯器 瀏覽:833
51單片機的核心 瀏覽:746
看電視直播是哪個app 瀏覽:958
將c源程序編譯成目標文件 瀏覽:787
再要你命3000pdf 瀏覽:558
ai軟體解壓軟體怎麼解壓 瀏覽:520
文件夾怎樣設置序列號 瀏覽:963
javascriptgzip壓縮 瀏覽:248
易語言怎麼取出文件夾 瀏覽:819
蘋果xs手機加密app哪裡設置 瀏覽:605
超聲霧化器與壓縮霧化器 瀏覽:643
模擬實現進程調度演算法 瀏覽:388
現在的壓縮包都是加密 瀏覽:331