
正文
java中新建相册代码 如何给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(w0) w+= rate;
else w-= rate;
if(h0)h+= rate;
else h-= rate;
}else if(e.getSource ()==smaller){//缩小
if(w0) w-= rate;
else w+= rate;
if(h0) h-= rate;
else h+= rate;
}else if(e.getSource ()==rotate){//翻转
if(w0h0){
h*=-1;
}else if(w0h0){
w*=-1;
}else if(w0h0){
h*=-1;
}else if(w0h0){
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、翻转功能需要改进 楼主自己试着完善下...
相关问答
Q1: Java中如何进行文件(及文件夹)的新建,移动,删除等?给出代码
File
F=new
File(路径);/通过将给定路径名字符串转换为抽象路径名来创建一个新
File
实例。
F.delete();//删除此抽象路径名表示的文件或目录。
文件的移动的话,得通过输入输出流
FileInputStream
FI=new
FileInputStream(F);
FileOutputStream
FO=new
FileOutputStream(F);
wile(FI.read()!=EOF)
{
FO.write();
}
Q2: 使用java实现创建本地文件的代码
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
public class CreateFile {
public static void main(String[] args) {
String str = "需要写入的字";
String fileName = "D:\\a\\a.xml";
OutputStream output = null;// 输出字节流
OutputStreamWriter outputWrite = null;// 输出字符流
PrintWriter print = null;// 输出缓冲区
try {
output = new FileOutputStream(fileName);
outputWrite = new OutputStreamWriter(output);
print = new PrintWriter(outputWrite);
print.print(str);
print.flush();// 一定不要忘记此句java中新建相册代码,否则数据有可能不能被写入文件
output.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
再一个问题就是只要java中新建相册代码你编码正确就可以正常打开。
Q3: netbeans里面的javaweb如何创建一个放照片背景的
netbeans里面的javaweb如何创建一个放照片背景的方法为java中新建相册代码:
1、新建项目:建立Java应用程序java中新建相册代码,项目名称:HousePaintTest,下面的创建主类前的对号去掉,不创建主类,在缺省包上右键单击,选择新建Java类,类名:MyPanel包:house
2、编写MyPanel类的代码,MyPanel继承JComponent,重写里面的protectedvoidpaintComponent(Graphicsg)方法.右键单击MyPanel的代码区,选择插入代码。
3、选择覆盖方法,选择JComponent中的paintComponent(Graphicsg),在里面自己添加代码,写自己的内容,具体代码看源码。然后必须进行编译,否则没法添加到JFrame内。
4、在里面自己添加代码,写自己的内容,具体代码看源码。然后必须进行编译,否则没法添加到JFrame内。出现窗体设计界面后右键单击JFrame窗体,选择Bean,在类名中写要添加的类的具体路径,这里是MyPanel类,前面是包名,点击确定后,自定义面板就加到了Frame上。
5、点击确定后,自定义面板就加到了Frame上,单击那几个省略号,选择外部图像,接着进行导入图片的工作。然后选择一个图片就行了,可以随意向上面拖按钮。
Q4: java 手机中调用相册和相机
安卓中调用摄像头和相册中要声明权限,用Android Studio做app的话就可以
java中新建相册代码的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于如何给java建立图片文件、java中新建相册代码的信息别忘了在本站进行查找喔。






