怎么用java代码放大或缩小图片不失真 。放大图像不会导致失真,而缩小图像将不可避免的失真 。
Java中也同样是这样 。
但java提供了4个缩放的微调选项 。
image.SCALE_SMOOTH //平滑优先
image.SCALE_FAST//速度优先
image.SCALE_AREA_AVERAGING //区域均值
【java绘图缩放代码大全 java缩小图片大小】image.SCALE_REPLICATE //像素复制型缩放
image.SCALE_DEFAULT //默认缩放模式
调用方法
Image new_img=old_img.getScaledInstance(1024, 768, Image.SCALE_SMOOTH);
得到一张缩放后的新图 。
java 图片缩放代码直接给你一个类,直接套用就好了
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.WritableRaster;
import java.io.File;
import javax.imageio.ImageIO;
public class Resize {
BufferedImage bufImage;
int width;
int height;
public Resize() {
// TODO Auto-generated constructor stub
}
public Resize(String srcPath,int width,int height) {
this.width = width;
this.height = height;
try{
this.bufImage = ImageIO.read(new File(srcPath));
}catch(Exception e){
e.printStackTrace();
}
}
public static BufferedImage rize(BufferedImage srcBufImage,int width,int height){
BufferedImage bufTarget = null;
double sx = (double) width / srcBufImage.getWidth();
double sy = (double) height / srcBufImage.getHeight();
int type = srcBufImage.getType();
if(type == BufferedImage.TYPE_CUSTOM){
ColorModel cm = srcBufImage.getColorModel();
WritableRaster raster = cm.createCompatibleWritableRaster(width,
height);
boolean alphaPremultiplied = cm.isAlphaPremultiplied();
bufTarget = new BufferedImage(cm, raster, alphaPremultiplied, null);
}else
bufTarget = new BufferedImage(width, height, type);
Graphics2D g = bufTarget.createGraphics();
g.setRenderingHint(RenderingHints.KEY_RENDERING,
RenderingHints.VALUE_RENDER_QUALITY);
g.drawRenderedImage(srcBufImage, AffineTransform.getScaleInstance(sx, sy));
g.dispose();
return bufTarget;
}
}
java如何实现图片拖动,放大缩小 , 旋转 。这个只是实现了移动,你参考以下吧 !
public class MoveImage {
static int x,y;
private static int num=0;
private static Icon icon=null;
public static void main(String[] args) throws Exception{
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().setLayout(null);//这个要设置成 null
//图片
icon = new ImageIcon("d:/test.gif");//d:/test.gif本地一张图片
JLabel l = new JLabel(icon); //创建具有指定图像的 JLabel 实例 。
l.setSize(icon.getIconWidth(),icon.getIconHeight());//设置面板的宽度和高度
l.setBorder(BorderFactory.createLineBorder(Color.red));//给图片加上红色外框
f.getContentPane().add(l);
f.setSize(900,700);
f.setVisible(true);
l.addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent e){
x=e.getX();
y=e.getY();
}
});
l.addMouseMotionListener(new MouseMotionListener(){
public void mouseDragged(MouseEvent e) {
JLabel l = (JLabel)e.getSource();
l.setLocation(l.getX() e.getX()-x,l.getY() e.getY()-y);
}
public void mouseMoved(MouseEvent e) {}
});
}
如何用Java实现图形的放大和缩小java实现图形的放大和缩小java绘图缩放代码大全,其实就是在画图时java绘图缩放代码大全 , 改变图片的长和宽 。以下代码参考一下java绘图缩放代码大全:
import java.awt.Graphics;
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.filechooser.FileNameExtensionFilter;
public class App extends JFrame implements MouseListener, ActionListener {
int x = 0;
int y = 0;
File[] selectedFiles = null;
int fileIndex = 0;
int width = 200;
int height = 200;
public App() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setSize(400, 300);
setResizable(false);
getContentPane().setLayout(null);
JPanel panel = new ImagePanel();
panel.setBounds(12, 40, 370, 218);
getContentPane().add(panel);
addMouseListener(this);
JButton btnBrowse = new JButton("Browse");
btnBrowse.addActionListener(this);
btnBrowse.setBounds(12, 9, 91, 21);
getContentPane().add(btnBrowse);
setVisible(true);
}
public static void main(String[] args) {
new App();
}
public void actionPerformed(ActionEvent e) {
JFileChooser chooser = new JFileChooser();
chooser.setMultiSelectionEnabled(true);
FileNameExtensionFilter filter = new FileNameExtensionFilter(
"JPGGIF Images", "jpg", "gif");
// 设置文件类型
chooser.setFileFilter(filter);
// 打开选择器面板
int returnVal = chooser.showOpenDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
selectedFiles = chooser.getSelectedFiles();
repaint();
}
}
public void mouseClicked(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
Point point = MouseInfo.getPointerInfo().getLocation();
x = point.x;
y = point.y;
}
public void mouseReleased(MouseEvent e) {
Point point = MouseInfo.getPointerInfo().getLocation();
int thisX = point.x;
int thisY = point.y;
System.out.println("thisX="thisX"""thisY="thisY);
if ((y - thisY20y - thisY0)
|| (y - thisY0y - thisY-20)) {
// Y 在20范围内移动认为是水平移动
if (xthisX) {
// right
if (selectedFiles != null
fileIndexselectedFiles.length - 1) {
fileIndex;
}
} else {
// left
if (selectedFiles != nullfileIndex0) {
fileIndex--;
}
}
} else {
if (xthisX) {
// 右下
width= 20;
height= 20;
} else {
// 左上
width -= 20;
height -= 20;
}
}
repaint();
}
class ImagePanel extends JPanel {
public void paint(Graphics g) {
super.paint(g);
if (selectedFiles != null) {
ImageIcon icon = new ImageIcon(selectedFiles[fileIndex]
.getPath());
g.drawImage(icon.getImage(), 0, 0, width, height, this);
}
}
}
}
求java编程实现一个正方形放大缩小的代码package bdzhidao;
import java.awt.*;
import javax.swing.*;
public class Square extends JFrame{
public static void main(String[] args){
JFrame frame=new JFrame("可变正方形");
Spanel jp=new Spanel();
frame.add(jp);
frame.setSize(1366,768);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(true);
frame.setLocation(0,0);
frame.setVisible(true);
}
}
class Spanel extends JPanel{
int x=640;
int Width=8;
protected void paintComponent(Graphics g){
super.paintComponent(g);
g.setColor(new Color(10,200,40));
try{
Thread.sleep(1000);
}catch(InterruptedException e){
}
g.drawRect(x-=20,x-=20,Width =10,Width =10);
if(x=0){
System.exit(0);//当正方形移到屏幕里面时关闭程序java绘图缩放代码大全,如果没有该语句则最后屏幕会变成绿色
}
repaint();
}
}
//缩小就只需要把相关数据更改一下就OKjava绘图缩放代码大全了java绘图缩放代码大全,希望对java绘图缩放代码大全你有帮助!
//其实就是利用线程不断重新画图
java小程序图片查看器代码 , 实现放大、缩小、旋转功能我只实现了放大缩小功能 , 旋转还有点问题package com.lzw;import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
/*
* @功能:实现图片的放大缩小与旋转功能
* @日期:2011.6.29
*/
public class OperationOnPic extends JFrame{
private JPanel imageJpanel;
private JPanel southJpanel;
private JButton rorateButton;
private Mycanvas canvas ;
private int imgWidth, imgHeight;
private JSlider jSlide;//滑动块组建
//private JScrollPane jScrollPane ;
Image img;
private static int num=0;
privatestatic int newWidth=0;
privatestaticint newHeigth=0;
public static boolean isroate=false;
public OperationOnPic(){
setTitle("图片操作");
//设置窗体大小
setSize(800, 600);
//设置窗体显示在中央位置
setLocationRelativeTo(null);
//URL imgUrl = OperationOnPic.class.getResource("2.jpg");//获得图片路径
//img = Toolkit.getDefaultToolkit().getImage(imgUrl);// 获取图片资源
ImageIcon image=new ImageIcon(OperationOnPic.class.getResource("2.jpg"));
img=image.getImage();
canvas=new Mycanvas();
//调用布局方法
init();
//设置窗体可见
setVisible(true);
}
public void init(){
setDefaultCloseOperation(this.EXIT_ON_CLOSE);//设置窗体可关闭
imageJpanel=new JPanel();
southJpanel=new JPanel();
imageJpanel.setLayout(new BorderLayout());
imageJpanel.add(canvas,BorderLayout.CENTER);
imageJpanel.add(getJSlider(),BorderLayout.SOUTH);
rorateButton=new JButton("图片旋转");
rorateButton.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
num =5;
isroate=true;
canvas.repaint();
isroate=false;
}
});
southJpanel.add(rorateButton);//将按钮添加到面板上
this.getContentPane().add(imageJpanel,BorderLayout.CENTER);
this.getContentPane().add(southJpanel,BorderLayout.SOUTH);
}
public JSlider getJSlider(){
if(jSlide==null){
jSlide=new JSlider();//实例化一个滑动块对象
jSlide.setMaximum(500);//设置滑动块的最大取值
jSlide.setMinimum(1);//设置滑动块的最小取值
jSlide.setValue(50);//设置滑动块当前值
jSlide.addChangeListener(new ChangeListener(){public void stateChanged(ChangeEvent e) {
// TODO Auto-generated method stub
canvas.repaint();//重新绘制图像
}
});
}
return jSlide;
}
public static void main(String[] args) {
new OperationOnPic();
}
class Mycanvas extends Canvas{
public void paint(final Graphics g){//重写paint
if(isroate==false){
imgWidth=img.getWidth(this);
imgHeight=img.getHeight(this);
float value=https://www.04ip.com/post/jSlide.getValue();//取得滑动块的值
newWidth=(int)(imgWidth*value/100);
newHeigth=(int)(imgHeight*value/100);
g.drawImage(img,0,0,newWidth,newHeigth,this);
g.dispose();
}else{
num =5;
Graphics2D g2=(Graphics2D)g;
g2.rotate(Math.toRadians(num));
g2.drawImage(img,0,0,400,400,this);
g.dispose();
}
}
}
关于java绘图缩放代码大全和java缩小图片大小的介绍到此就结束了 , 不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站 。
推荐阅读
- 推荐好玩的赛车竞速游戏,推荐好玩的赛车竞速游戏手机版
- 微信视频号pid在哪里,微信视频号的账号在哪显示
- net4.7.2安装失败无法建立到信任,net40无法安装
- pg批量删除数据库表,批量删除数据库语句
- linux命令行进桌面 linux进入桌面系统
- 分布式系统redis数据,Redis的分布式锁
- oracle数据库备份怎么迁移,oracle数据库备份迁移和恢复
- java一百的阶乘代码,java阶乘和代码
- php随机读取几条数据库 php数据库随机读取十个数据