自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

用Swing制作精美的圖層疊加圖

開發(fā) 后端
前段時(shí)間,看著不少人用twaver的Swing在寫東西,比如我們武林中的Swing 刀客 和 Swing劍客(注三號(hào)管家chart圖的模仿),都用到了寫出了很漂亮的swing界面。下面我要分享的是用swing編寫的圖層疊加效果圖,其中也用到了twaver的一些功能。

前段時(shí)間,看著不少人用twaver的Swing在寫東西,比如我們武林中的Swing 刀客  和 Swing劍客(注三號(hào)管家chart圖的模仿),都用到了寫出了很漂亮的swing界面。下面我要分享的是用swing編寫的圖層疊加效果圖,其中也用到了twaver的一些功能。(在此僅僅是為了分享給大家比較美的界面,希望能在這酷暑之際為大家?guī)硪唤z涼意或美的享受就心滿意足了)。

在TWaver的各個(gè)使用手冊(cè),文檔或Demo中我們可以看到,twaver提供了Layer的概念,就是圖層,這與一些制圖軟件也有幾分相似。在實(shí)際應(yīng)用中也是比較的多。比如TWaver的水印、背景效果都是通過圖層來疊加的。

下面我們就來看看這個(gè)精美的圖層疊加圖到底美在何處,先上最終效果圖:

 這是一個(gè)使用TWaver Java制作的自動(dòng)布局的例子,有人能看得出這里使用了多少個(gè)圖層合并而成的嗎?
呵呵,我們先來看看整體的一個(gè)布局:首先frame中添加了一個(gè)LayeroutPanel,panel中放了一個(gè)network,network中間部分是用于存放網(wǎng)元,連線,右半部分是scrollPanel。

一、Network的疊加

我們先來看看中間這個(gè)network的圖層是如何疊加的

1.陰影層

首先是在network的Cushion上添加了一個(gè)網(wǎng)元陰影層,cushion在TWaver的定義中是處于所有圖層之下的一層。

  1. network.addCanvasCushion(new ShadowCushion(this)); 

 

陰影也可以這樣添加。

2.網(wǎng)元層 

在默認(rèn)圖層上添加布局的網(wǎng)元 

  1. this.cloud = this.createNode("/demo/layout/images/cloud.png");  
  2.  this.center1 = this.createNode("/demo/layout/images/center.png");  
  3.  this.center2 = this.createNode("/demo/layout/images/center.png");  
  4.  this.gather1 = this.createNode("/demo/layout/images/gather.png");  
  5.  this.gather2 = this.createNode("/demo/layout/images/gather.png");  
  6.  this.router1 = this.createNode("/demo/layout/images/router1.png"0"Router 1");  
  7.  this.router2 = this.createNode("/demo/layout/images/router2.png"1"Router 2");  
  8.  this.server1 = this.createNode("/demo/layout/images/pc.png"2"Spring Layout");  
  9.  this.server2 = this.createNode("/demo/layout/images/pc.png"3"Office Network");  
  10.  this.server3 = this.createNode("/demo/layout/images/pc.png"4"US Map");  
  11.  this.client1 = this.createNode("/demo/layout/images/pc.png"5"Bar Chart");  
  12.  this.client2 = this.createNode("/demo/layout/images/pc.png"6"Tag Cloud");  
  13.  this.client3 = this.createNode("/demo/layout/images/pc.png"7"Bus Layout");  
  14.  this.createLink(gather1, client1);  
  15.  this.createLink(gather1, client2);  
  16.  this.createLink(gather1, client3);  
  17.  this.createLink(gather2, server1);  
  18.  this.createLink(gather2, server2);  
  19.  this.createLink(gather2, server3);  
  20.  this.createLink(cloud, center1);  
  21.  this.createLink(cloud, center2);  
  22.  this.createLink(router1, center1);  
  23.  this.createLink(router2, center2);  
  24.  this.createLink(router1, gather1);  
  25.  this.createLink(router2, gather2); 

TWaver提供了多種布局的效果,這是一個(gè)左樹形布局,下面的toolbar上提供了更多的布局方式。

3.背景層 

設(shè)置network背景圖片,背景層也是處于所有數(shù)據(jù)層之下的一層,但是在cushion層之上 

  1. this.setImageBackground("/demo/layout/images/bottom.png"); 
 

 

 4.頂層 

添加top的圖層節(jié)點(diǎn),并設(shè)置圖層為1 

  1. this.top = this.createNode("/demo/layout/images/top.png");  
  2.  this.top.setLayerID("top");  
  3.  Layer topLayer = new Layer("top");  
  4.  topLayer.setMovable(false);  
  5.  topLayer.setSelectable(false);  
  6.  this.getDataBox().getLayerModel().addLayer(1, topLayer); 

使用一個(gè)Node最為最上層的圖片,哈哈,這也是TWaver中的一個(gè)使用技巧。

5.工具條層 

添加toolbar圖層并設(shè)置為1,這樣toolbar的圖層會(huì)在top層之上

  1. this.toolbar = this.createNode("/demo/layout/images/toolbar.png");  
  2.  this.toolbar.setLocation(2168);  
  3.  this.toolbar.setLayerID("toolbar");  
  4.  Layer toolbarLayer = new Layer("toolbar");  
  5.  toolbarLayer.setMovable(false);  
  6.  toolbarLayer.setSelectable(false);  
  7.  this.getDataBox().getLayerModel().addLayer(1, toolbarLayer); 

工具條也是一張圖片哦,哈哈,沒想到吧! 

工具條的動(dòng)畫效果

從上面分解中可以看出,工具條是疊加在top層之上的,這其中還有一個(gè)動(dòng)畫的效果,當(dāng)鼠標(biāo)移動(dòng)到工具條所有的區(qū)域范圍時(shí),才會(huì)出現(xiàn),移出并會(huì)隱藏。 

  1. this.getCanvas().addMouseMotionListener(new MouseMotionAdapter() {  
  2.      public void mouseMoved(MouseEvent e) {  
  3.          if(isAdjustingToolbar){  
  4.              return;  
  5.          }  
  6.          if(toolbarBounds.contains(e.getPoint())){  
  7.              if(!toolbar.isVisible()){  
  8.                  isAdjustingToolbar = true;  
  9.                  toolbar.setVisible(true);  
  10.                  TWaverUtil.animateMove(toolbar, toolbar.getWidth(), 0new Runnable(){  
  11.                      public void run() {  
  12.                          isAdjustingToolbar = false;  
  13.                      }  
  14.                  });  
  15.              }  
  16.          }else{  
  17.              if(toolbar.isVisible()){  
  18.                  isAdjustingToolbar = true;  
  19.                  TWaverUtil.animateMove(toolbar, -toolbar.getWidth(), 0new Runnable(){  
  20.                      public void run() {  
  21.                          toolbar.setVisible(false);  
  22.                          isAdjustingToolbar = false;  
  23.                      }  
  24.                  });  
  25.              }  
  26.          }  
  27.      }  
  28.  }); 

 

6.最終合并效果最后twaver根據(jù)添加的這些圖層順序,就會(huì)在network上疊加出一個(gè)左半部分的效果,如下: 

二.    ScrollPanel的疊加

看完network中間部分的疊加效果,我們?cè)賮砜纯催@張圖的右半部分scrollerPanel是如何疊加的

1. 組件層

這是最重要的放置內(nèi)容面板的一層,里面放置了24個(gè)獨(dú)立的組件。通過設(shè)置邊框的范圍讓其只顯示中間部分,每個(gè)獨(dú)立的組件都可以單獨(dú)操作:選中,移動(dòng),染色,tooltip…都可以呈現(xiàn)。

  1. for(int i=0; i<24; i++){  
  2.  JComponent component = null;  
  3.  int index = i % 8;  
  4.  if(index == 0){  
  5.         component = new Router1();  
  6.  }  
  7.  ... ...  
  8.  if(component != null){  
  9.          component.setPreferredSize(CARDSIZE);  
  10.          component.setMaximumSize(CARDSIZE);  
  11.          component.setMinimumSize(CARDSIZE);  
  12.          component.setBounds(XGAP, i*CARDSIZE.height+YGAP, CARDSIZE.width-XGAP*2, CARDSIZE.height-YGAP*2);  
  13.          this.add(component);  
  14.      }  
  15.    
  16.  } 

 

 

2.相框?qū)?/p>

這是一個(gè)給每個(gè)組件設(shè)置相框的一個(gè)圖層,首先我們需要相框圖片

  1. Rectangle rect = new Rectangle(0, i*CARDSIZE.height, CARDSIZE.width, CARDSIZE.height);  
  2.              if(i != (this.currentIndex + 8)){  
  3.                  g2.drawImage(CARDIMAGE, rect.x, rect.y, rect.width, rect.height, null);  
  4.              }else{  
  5.                  rect.grow(-XGAP+4, -YGAP+4);  
  6.                  g2.setColor(Color.white);  
  7.                  g2.setStroke(TWaverConst.BASIC_STROKE);  
  8.    
  9.                  int d = 8;  
  10.                  g2.drawLine(rect.x, rect.y, rect.x+d*2, rect.y);  
  11.                  g2.drawLine(rect.x, rect.y, rect.x, rect.y+d);  
  12.    
  13.                  g2.drawLine(rect.x+rect.width, rect.y+rect.height, rect.x+rect.width-d*2, rect.y+rect.height);  
  14.                  g2.drawLine(rect.x+rect.width, rect.y+rect.height, rect.x+rect.width, rect.y+rect.height-d);  
  15.              } 

 

3.蒙版層

這是最上邊的類似于蒙版的一層,通過兩張上下透明的圖片將其放置在scrollerPane的最上邊一層

  1. if(top){  
  2.      image = TWaverUtil.getImageIcon("/demo/layout/images/mist1.png");  
  3.  }else{  
  4.      image = TWaverUtil.getImageIcon("/demo/layout/images/mist2.png");  
  5.  }  
  6.  JComponent canvas = new JComponent(){  
  7.      public void paintComponent(Graphics g) {  
  8.      super.paintComponent(g);  
  9.          g.drawImage(image.getImage(), 00, image.getIconWidth(), image.getIconHeight(), null);  
  10.      }  
  11.  }; 

蒙版層上也是有動(dòng)畫效果的,當(dāng)鼠標(biāo)點(diǎn)擊上或下的蒙版,組件面板會(huì)自動(dòng)上移或下移一個(gè)

4.最終疊加效果

這樣兩張圖片一疊加就可以得到我們最開始提供的那種圖了。

是不是有點(diǎn)像用PS軟件在畫圖,呵呵,對(duì)了,這就是twaver swing中圖層的作用。

PS:附上源Demo代碼供大家學(xué)習(xí)分享:TopoDemo

 

原文鏈接:http://www.cnblogs.com/twaver/archive/2011/07/29/2120877.html

  1. 簡(jiǎn)述Java圖形用戶界面設(shè)計(jì)(Swing)
  2. Effective Java 創(chuàng)建和銷毀對(duì)象
  3. Java編碼及網(wǎng)絡(luò)傳輸中的編碼問題
  4. 探討:Java中刪除數(shù)組中重復(fù)元素
  5. 用Java GUI編寫的畫板程序
責(zé)任編輯:林師授 來源: twaver的博客
相關(guān)推薦

2010-04-20 13:45:00

WPS表格

2012-09-25 09:09:51

JSjQuery素材

2012-09-19 14:18:32

素材Web開發(fā)

2012-02-24 15:28:36

ibmdw

2009-11-27 14:25:57

VS2003模板

2020-05-19 17:26:21

Python俄羅斯方塊游戲開發(fā)

2017-02-13 16:47:13

iOSDSL開發(fā)

2011-04-07 08:55:23

WebUI素材

2025-04-07 00:00:00

OpenAIGPT-4o圖像

2009-07-15 09:06:07

BeanTableMoSwing

2009-07-16 11:02:33

Swing文件選擇器

2011-06-28 09:03:14

MeeGoN9諾基亞

2009-12-02 11:37:36

思科無線路由器

2011-11-03 10:11:37

HTML 5

2022-03-09 12:45:15

時(shí)間線

2009-07-14 18:28:58

Swing入門

2018-03-21 18:00:15

NestJS

2009-06-29 15:28:00

TableModelSwing開發(fā)

2023-08-02 20:04:47

2020-12-31 10:29:05

數(shù)據(jù)可視化可視化工具編碼
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)