//显示对象为jpg,gif canvas中画图使用double buffering,使用scroll bar显示,显示区域固定了,可以扩展成按照canvas的大小来显示的模式,因为canvas放在center位置。 import java.awt.*; import java.awt.event.*; import java.awt.image.*; import javax.swing.*; class Frameext extends Frame { } public class Images extends Frame implements ActionListener, WindowListener ,WindowStateListener { //Frameext fr = new Frameext(); Label Label1 = new Label(" ",Label.LEFT); //will show the file name Button Button1 = new Button("open file"); int xpos,ypos; int imageHeight; int w,h; Image offimg, bg; Graphics offg; boolean first=true; //extends Canvas. This code will use its paint method. public MyCanvas Canvas1 = new MyCanvas(); static Scrollbar horizontal, vertical; //scrollbar used FileDialog fd = new FileDialog(this,"Open", FileDialog.LOAD); Image Image1; public void initialize() { //System.out.println(" dddddd "+this.MAXIMIZED_BOTH); //this.setLocation(0, 0); //this.setSize(this.getToolkit().getScreenSize()); this.setSize(600,600); this.setLocation(30,30); this.setBackground(Color.lightGray); Canvas1.setBackground(Color.white); Canvas1.setSize(580,580); //ColorChooserDialog colorDialog = new ColorChooserDialog(this, "@fff", this); this.add ("North", horizontal = new Scrollbar (Scrollbar.HORIZONTAL)); this.add ("East", vertical = new Scrollbar (Scrollbar.VERTICAL)); Panel pa=new Panel(); pa.setBackground(Color.white); Label1.setBackground(Color.lightGray); Label1.setAlignment(Label.LEFT); pa.add(Label1); pa.add(Button1); //add label and button on panel this.add("South",pa); //add panel at south this.add("Center",Canvas1); this.addWindowListener(this); this.addWindowStateListener (this); Button1.addActionListener(this); try { System.out.println ("getsize" + Canvas1.getSize () + "getwidth" + Canvas1.getWidth () + "getheight " + Canvas1.getHeight ()); } catch (Exception e) {System.out.println(e.toString()); } this.pack(); this.show(); } void imageload () { fd.setFile("*.jpg;*.gif"); //file speicific fd.show(); if(fd.getFile() == null) { Label1.setText("You have not chosen any image files yet"); } else { String d = (fd.getDirectory() + fd.getFile()); Image1 = Toolkit.getDefaultToolkit().getImage(d); if(Image1==null) {System.out.println("image is null of this file:"+ d); return; } Label1.setText(d); System.out.println("image loaded "+d); //It is important to use Canvas1 as the ImageObserver. MediaTracker mt = new MediaTracker(Canvas1); //here should be noticed like the author say mt.addImage(Image1, 0); try { mt.waitForAll(); } catch (InterruptedException e) { System.out.println(e.toString()); } w = Image1.getWidth (this); h = Image1.getHeight (this); //set w and h the image's height and width System.out.println("image width and height is"+w+" "+h); if (offimg != null && (offimg.getHeight (this) < h || offimg.getWidth (this) < w)) { //shall we recreate the offimg for double buffering if the image is larger System.out.println ("offimage w and h " + offimg.getHeight (this) + " " + offimg.getWidth (this) + " w " + w + " h " + h); offimg = createImage (w, h); offg = offimg.getGraphics (); } vertical.setMaximum(h-390); //here important set scroll bar 's maximum //imagesize minus (showsize minus insects size first) , //you can determ the insects by program or simple set the maximum to //a certain number,and then you scroll it down and trace when it reach //the boundary,the value does not agree with the maximum number you set //and the difference may be the insects size you shall minus first // vertical.setVisibleAmount((h-290)/10); //if we try to set the visibleamount to a variable ,the scroll by will be uncertain and //hard to contral horizontal.setMaximum(w-440); // horizontal.setVisibleAmount((w-390)/10); Canvas1.repaint(); //image loaded so repaint the canvas // Canvas1.paint(); } } public boolean handleEvent (Event e) { // System.out.println("catched "+e);//we can see clearly what event happen if we don't comment this sentence if (e.id == Event.WINDOW_DESTROY) { System.exit(0); } else if (e.target instanceof Scrollbar) { System.out.println(" scoll of handleevent " ); if (e.target == horizontal) { xpos = ((Integer)e.arg).intValue(); //trace the scroll value for image show later } else if (e.target == vertical) { ypos = ((Integer)e.arg).intValue(); } System.out.println(" "+xpos+""+ypos); //System.out.println(" checking hereeeeeeeeeeeeeee" ); Canvas1.repaint(); } return super.handleEvent(e); } public void windowStateChanged (WindowEvent e) { System.out.println ("window state changed"); Canvas1.repaint (); } public void windowClosing(WindowEvent e) { // Use this.hide(); for subsequent forms in multi form applications System.exit(0); } public void windowActivated(WindowEvent e) {System.out.println ("window activated");Canvas1.repaint();} public void windowClosed(WindowEvent e) {} public void windowDeactivated(WindowEvent e) {} public void windowDeiconified(WindowEvent e) {} public void windowIconified(WindowEvent e) {} public void windowOpened(WindowEvent e) {} public void actionPerformed(ActionEvent event) { System.out.println("catched2 "+event); Button b = (Button)event.getSource(); if(b == Button1) { imageload(); } } public static void main(String args[]) { Images a = new Images(); a.initialize(); } /* public void update(Graphics g) { Canvas1.update(g); } public void paint(Graphics g) { Canvas1.paint(g); } */ //A simple inner class to show the basics. class MyCanvas extends Canvas { public void update(Graphics g) { System.out.println(" update" ); if (offg != null&&Image1!=null) { offg.clearRect(50,50, 500,450); offg.drawImage(Image1,50,50, 500,450, xpos, ypos,450+xpos,400+ypos ,this); g.drawImage (offimg, 50, 50, this); System.out.println ("w and h is" + w + " " + h + " and count update is" + " offg " + offg); } } public void paint(Graphics g) { if (first) { offimg = createImage (getWidth (), getHeight ()); offg = offimg.getGraphics (); System.out.println ("w and h is" + w + " " + h + " and count update is" + " offg " + offg); first= false; } if (offg != null&&Image1!=null) //if delete this when the window state change,the image will not show { //you must scroll the bar by hand then it will show we show take care what the function of paint when repaint offg.clearRect(50,50, 500,450); offg.drawImage(Image1,50,50, 500,450, xpos, ypos,450+xpos,400+ypos ,this); g.drawImage (offimg, 50, 50, this); System.out.println ("w and h is" + w + " " + h + " and count update is" + " offg " + offg); } System.out.println ("this is paint"); } }//End of inner class. }

欢迎大家阅读《java图像浏览器》,跪求各位点评,若觉得好的话请收藏本文,by 搞代码

原创文章,转载请注明: 转载自搞代码

微信 赏一包辣条吧~

支付宝 赏一听可乐吧~

java可以搞图像,java图像浏览器相关推荐

  1. java:均值哈希实现图像内容相似度比较

    这阵子发现我的图像数据库中有不少内容一样的图像需要剔除,这些内容一样的图像可能尺寸不一样,通道数也可能不一样(灰度/彩色),如下三张图内容完全一样,只是亮度或色彩通道数不同, 于是想到了用google ...

  2. java 验证码图片识别_JavaSE图像验证码简单识别程序详解

    本文为大家分享了JavaSE图像验证码简单识别程序,供大家参考,具体内容如下 首先你应该对图片进行样本采集,然后将样本进行灰度处理,也就是变成黑白两色. 然后你就可以使用该类,对目标文件进行分析.具体 ...

  3. JAVA设置新视口,java – 在更大的图像上移动视口; JLablel JScrollPane

    这是一个非常基本的例子.它使用一个图像文件并将其放置在一个滚动窗格内(在一个圆形的方式). 从那里,它只是使用Swing Timer来随机生成点(在图像的边界内). 每次生成一个新点时,我只需使用sc ...

  4. java 把图片插入窗体,JAVA JFrame窗体添加背景图像的两种方法

    首先还是要了解框架JFrame中的层次结构.JFrame中的层次分布及相对关系是:最底层是:JRootPane:第二层是:JlayerPane:最上层就是ContentPane,也正是我们常说的内容面 ...

  5. java imageio_Java使用imageio 读写图像

    Java中进行图像I/O(即读图片和写图片,不涉及到复杂图像处理)有三个方法: JAI 中的 Image I/O Tools,支持更多图片类型,例如JPEG-LS, JPEG2000, 和 TIFF. ...

  6. Java OpenCV 图像处理34 图形图像 分水岭 watershed

    Java OpenCV 图像处理34 图形图像 分水岭 watershed package com.xu.opencv;import org.opencv.core.Core; import org. ...

  7. java实现缩放图像、切割图像、图像类型转换、彩色转黑白、文字水印、图片水印等

    可实现以下常用功能:缩放图像.切割图像.图像类型转换.彩色转黑白.文字水印.图片水印等 代码如下 复制代码 import java.awt.AlphaComposite; import java.aw ...

  8. java只更新部分图像,java – 绘制从角落偏移的图像的某些部分...

    public abstract boolean drawImage(Image img, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, i ...

  9. java 纹理_图像处理之图像纹理添加效果(利用JAVA 2D纹理画笔)

    一:基本思想 做法非常简单,首先找到一张你想要的纹理素材图片,然后将该图片通过灰度 转换成一张灰度级别的纹理图,取得像素数据,扫描每个像素的灰度值来设置 纹理图像的每个像素的透明度.灰度值越高越接近白 ...

最新文章

  1. 这些年我对微服务的理解
  2. 双赢思维的五个要领_可访问网站双赢的4个原因
  3. 不规则炫彩创意渐变海报设计模板素材|带来十足的时尚感
  4. 5 句话,带你看准区块链和分布式账本最新趋势!
  5. redux解决了什么问题?
  6. 动态规划 —— 阶乘
  7. 使用Mac电脑内置的屏幕共享功能,进行远程桌面管理
  8. 基础算法1——插入排序和希尔排序
  9. NodeJS连接MySQL
  10. jvm垃圾回收机制_深入理解 JVM 垃圾回收机制及其实现原理
  11. spss 因子分析非正定矩阵解决方案
  12. 广东省计算机一级网络题分值,计算机一级考试的试题分值如何分配的?
  13. 台式机计算机里所有文档都打不开,电脑里的所有word文档都打不开,如何处理掉?...
  14. python使用pd.to_numeric()方法将数据转为数字类型int或float
  15. 某某证券大数据开发工程师招聘笔试题
  16. 如何在笔记本上设置wifi热点(菜鸟篇)
  17. 正则表达式(二)常用正则表达式——验证邮箱
  18. 使用浏览器抓包获取API
  19. RubyRails环境的离线安装
  20. 5月3日云栖精选夜读:乾隆会判阿尔法狗死刑吗 ——浅谈当前人工智能的技术进化...

热门文章

  1. 【毕业设计】基于springboot的大学生综合素质测评系统——2023最新推荐
  2. Robotaxi和车联网会擦出什么爱情火花?
  3. js map满足条件跳出循环_js for等循环 跳出多层循环
  4. js 数组遍历符合条件跳出循环体_JavaScript跳出forEach循环
  5. Vue 系列一 之 Vue 基础
  6. android studio 中制作简易的桌面组件
  7. 工作不顺跳槽后的程序员,后来怎么样了?
  8. 微信上的python训练营_用 Python 爬了爬自己的微信朋友(实例讲解)
  9. 18929 最优二叉树
  10. 打开/关闭NFC 功能