
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、翻转功能需要改进 楼主自己试着完善下...
页面:<%@ Page language="c#" Codebehind="filesystem.aspx.cs" AutoEventWireup="false" Inherits="WebShop.filesystem" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>filesystem</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
<LINK href="CssStyle.css" type="text/css" rel="stylesheet">
<form id="Form1" method="post" runat="server">
</HEAD>
<body MS_POSITIONING="GridLayout">
<FONT face="宋体">
<table cellSpacing="1" cellPadding="0" width="777" align="center" bgColor="#336600" border="0">
<tr>
<td bgColor="#08498c" colSpan="3"><IMG src="images/FileSystemBaner.gif"></td>
</tr>
<tr>
<td width="170" bgColor="#e3e3e3" rowSpan="2">
<asp:linkbutton id="LinkButton1" runat="server">后退</asp:linkbutton></td>
<td width="604" bgColor="#efefef" colSpan="2">
<asp:datalist id="DataList1" runat="server" RepeatColumns="4" RepeatDirection="Horizontal" DataKeyField="Name"
Width="604">
<ItemTemplate>
<table width="150" align="center">
<tr>
<td align="center" width="10"></td>
<td align="left" width="140"><%#DataBinder.Eval(Container.DataItem,"Image")%>
</td>
</tr>
<tr>
<td width="10" align="right">
<asp:CheckBox ID="checkBox1" Runat="server" /></td>
<td width="140" align="left"><font face="宋体"><%#DataBinder.Eval(Container.DataItem,"Name")%></font></td>
</tr>
</table>
</ItemTemplate>
</asp:datalist></td>
</tr>
<tr>
<td align="right" bgColor="#efefef" colSpan="2"><asp:button id="Button3" runat="server" Text="删除选中项"></asp:button>
<input id="fileFeild1" type="file" name="fileFeild1" runat="server">
<asp:button id="Button1" runat="server" Text="确定上传"></asp:button><br>
<asp:textbox id="TextBox1" runat="server"></asp:textbox>
<asp:button id="Button2" runat="server" Text="创建目录"></asp:button></td>
</tr>
<tr>
<td bgColor="#888888" colSpan="3"><span class="STYLE1"><asp:label id="Label2" runat="server" ForeColor="White">当前所在位置:</asp:label><SPAN class="STYLE1"><asp:label id="Label1" runat="server" ForeColor="White" Width="87px"></asp:label></SPAN></span></td>
</tr>
</table>
</FONT></FORM>
</body>
</HTML>
后台代码:
using System
using System.Collections
using System.ComponentModel
using System.Data
using System.IO
using System.Drawing
using System.Web
using System.Web.SessionState
using System.Web.UI
using System.Web.UI.WebControls
using System.Web.UI.HtmlControls
namespace WebShop
{
/// <summary>
/// filesystem 的摘要说明。
/// </summary>
public class filesystem : System.Web.UI.Page
{
protected System.Web.UI.WebControls.LinkButton LinkButton1
protected System.Web.UI.WebControls.DataList DataList1
protected System.Web.UI.WebControls.Button Button3
protected System.Web.UI.WebControls.Button Button1
protected System.Web.UI.WebControls.TextBox TextBox1
protected System.Web.UI.WebControls.Button Button2
protected System.Web.UI.WebControls.Label Label2
protected System.Web.UI.WebControls.Label Label1
protected System.Web.UI.HtmlControls.HtmlInputFile fileFeild1
private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
Bind()
}
}
private void Bind()
{
string initpath=""
if(Request["path"]==null)
{
initpath=Server.MapPath("FileSystem")
}
else
{
initpath=Request["path"]
}
this.Label1.Text=initpath
DataTable dt=new DataTable()
DataColumn dc0=new DataColumn("Image",System.Type.GetType("System.String"))
dt.Columns.Add(dc0)
DataColumn dc1=new DataColumn("Name",System.Type.GetType("System.String"))
dt.Columns.Add(dc1)
DirectoryInfo di=new DirectoryInfo(this.Label1.Text)
DirectoryInfo[] dis=di.GetDirectories()
foreach(DirectoryInfo d in dis)
{
DataRow dr=dt.NewRow()
dr[0]="<a href='filesystem.aspx?path="+HttpUtility.UrlEncode(d.FullName,System.Text.Encoding.UTF8)+"'><img src='images/folder.gif' border=0/></a>"
dr[1]=d.Name
dt.Rows.Add(dr)
}
FileInfo[] fis=di.GetFiles()
foreach(FileInfo f in fis)
{
string ex=f.Extension.ToLower()
if(ex==".jpg" || ex==".jpeg" || ex==".gif" || ex==".png" || ex==".bmp")
{
string fullname=f.FullName
string urlpath=fullname.Substring(fullname.IndexOf("FileSystem"))
string url=HttpUtility.UrlEncode(urlpath,System.Text.Encoding.UTF8)
DataRow dr=dt.NewRow()
dr[0]="<a href='"+url+"' target='_blank'><img src='"+url+"' border=0 width=100 height=128/></a>"
dr[1]=f.Name
dt.Rows.Add(dr)
}
}
this.DataList1.DataSource=dt
this.DataList1.DataBind()
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent()
base.OnInit(e)
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.LinkButton1.Click += new System.EventHandler(this.LinkButton1_Click)
this.Button3.Click += new System.EventHandler(this.Button3_Click)
this.Button1.Click += new System.EventHandler(this.Button1_Click)
this.Button2.Click += new System.EventHandler(this.Button2_Click)
this.Load += new System.EventHandler(this.Page_Load)
}
#endregion
private void LinkButton1_Click(object sender, System.EventArgs e)
{
string Parent=Directory.GetParent(this.Label1.Text).ToString()
if(Parent.IndexOf("FileSystem")>-1)
{
Response.Redirect("filesystem.aspx?path="+Parent)
}
else
{
return
}
}
private void Button1_Click(object sender, System.EventArgs e)
{
HttpPostedFile hpf=this.fileFeild1.PostedFile
string ClientPath=hpf.FileName
string filename=Path.GetFileName(ClientPath)
string ex=Path.GetExtension(filename)
if(ex==".jpg" || ex==".jpeg" || ex==".gif" || ex==".png" || ex==".bmp")
{
string SavePath=this.Label1.Text+"\\"+filename
hpf.SaveAs(SavePath)
Bind()
}
else
{
Response.Write(Tools.GetAlertJS("所上传的图片格式不正确!"))
return
}
}
private void Button2_Click(object sender, System.EventArgs e)
{
string filename=this.TextBox1.Text
Directory.CreateDirectory(this.Label1.Text+"\\"+filename)
Bind()
}
private void Button3_Click(object sender, System.EventArgs e)
{
for(int i=0i<this.DataList1.Items.Counti++)
{
if(((CheckBox)this.DataList1.Items[i].FindControl("CheckBox1")).Checked)
{
int index=this.DataList1.Items[i].ItemIndex
string filePath=this.Label1.Text+"\\"+this.DataList1.DataKeys[index].ToString()
if(Directory.Exists(filePath))
{
Directory.Delete(filePath,true)
}
if(File.Exists(filePath))
{
File.Delete(filePath)
}
Bind()
}
}
}
}
}
这个是C#的 你改下 Java的就行
import javax.swing.ImageIconimport javax.swing.JFrame
import javax.swing.JLabel
import javax.swing.JPanel
public class text
{
JFrame jf
JLabel jl
JPanel jp1
public static void main(String[] args)
{
text t=new text()
t.go()
}
void go()
{
final ImageIcon image1 = new ImageIcon("1.gif")
jf=new JFrame()
jl = new JLabel(image1)
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
jf.setSize(300, 300)
jf.setLocation(400, 300)
jp1 = new JPanel()
jp1.add(jl)
jf.getContentPane().add(jp1)
jf.setVisible(true)
}
}
把你的找一张gif 格式的照片 .名字改成1.gif 放在你的项目根目录下
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)