大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
java实现图形的放大和缩小,其实就是在画图时,改变图片的长和宽。以下代码参考一下:
创新互联建站主要从事网站设计制作、网站建设、网页设计、企业做网站、公司建网站等业务。立足成都服务阜平,十多年网站建设经验,价格优惠、服务专业,欢迎来电咨询建站服务:18982081108
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(
"JPG GIF 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 - 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();
}
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);
}
}
}
}
;
如果用 JLabel 显示图像,稍微覆盖它的 paintComponent 方法后根据键盘输入调整它的尺寸既可。
比如:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class C extends JFrame {
public static void main(String[] args) { new C(); }
public C() {
final JLabel imgView = new JLabel(new ImageIcon(你的图片路径)) {
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(((ImageIcon)getIcon()).getImage(), 0, 0, getWidth(), getHeight(), null);
}
};
addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
double zoomFactor = e.getKeyCode() == KeyEvent.VK_UP ? 1.01 :
e.getKeyCode() == KeyEvent.VK_DOWN ? .99 : 1;
imgView.setSize((int)(imgView.getWidth() * zoomFactor),
(int)(imgView.getHeight() * zoomFactor));
}
});
add(imgView);
setSize(800, 600);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
private File file;
private String fileFileName;
private String picture;
//都有getter 和 setter
InputStream is = new FileInputStream(file);
//引入一个IO流的输入流
String root = ServletActionContext.getRequest()
.getRealPath("/bookpicture");
//通过REQUEST来得到相对地址,并在后面加上/bookpicture
File f = new File(root, this.getFileFileName());
//定义一个FILE文件,第一个参数是文件的路径,第二个是文件的名字
picture="."+"\\"+"bookpicture"+"\\"+this.getFileFileName();
//为PICTURE字符串赋值,/地址/文件名
System.out.println
("======picture====="+picture);
//从控制台输出Picture
OutputStream os = new FileOutputStream(f);
//第一个文件的输出流
byte[] buffer = new byte[1024];
//定义一个bufer的字符串,长度为1024
int len = 0;
while ((len = is.read(buffer)) 0) {
//如果从制定文件中读取到的信息为结束就继续循环
os.write(buffer, 0, len);
//将文件读出的内容写入到指定的文件中
}
写一个线程,在run方法中控制图片x,y的坐标位置,不能超过窗体的控制就是当你的x和Y的坐标不能大于窗体的最大值和最小值。
你
import java.awt.*;
import java.util.Date;
import javax.swing.*;
import sun.security.krb5.internal.rcache.ReplayCache;
public class CanvasPanel extends JPanel implements Runnable {
private Image back;
private Image cloud;
int time;
private Thread thread;
private boolean play = true;
int width = 458;
int height = 357;
int x = width;
public CanvasPanel() {
super();
back = new ImageIcon(getClass().getResource("back.jpg")).getImage();
cloud = new ImageIcon(getClass().getResource("cloud.png")).getImage();
time = 100;
play = true;
if (thread == null || !thread.isAlive())
thread = new Thread(this);
thread.start();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(back, 0, 0, width, height, this);
g.drawImage(cloud, x, 10, this);
x -= 2;
if (x = -cloud.getWidth(this))
x = width;
}
public void run() {
while (play) {
try {
Thread.sleep(time);
} catch (Exception e) {
e.printStackTrace();
}
repaint();
}
}
}
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class ImageRun extends JFrame {
private JPanel panel;
public static void main(String args[]) {
try {
ImageRun frame = new ImageRun();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
public ImageRun() {
super();
setBounds(100, 100, 458, 357);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().add(getPanel(), BorderLayout.CENTER);
//
}
protected JPanel getPanel() {
if (panel == null) {
panel = new CanvasPanel();
}
return panel;
}
}
back.jpg和cloud.jpg是两张图片