分享
 
 
 

java图像浏览器

王朝java/jsp·作者佚名  2008-05-31
窄屏简体版  字體: |||超大  

//显示对象为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. }

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有