代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
Java
】
在Graphics上绘制BufferedImage的一个简单示例
作者:
Dezai.CN
/ 发布于
2011/12/1
/
722
主要功能是将一个图片切割成为4份(2行 * 2列),然后再将切分后的4个子图像进行乱序,换句话说,就是让切割后的子图像不在显示在原有的位置上,最后渲染到Swing的组件上
<div> <pre><span style="color: #008080"> 1</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> MyTest <span style="color: #0000ff">extends</span> JPanel {<span style="color: #008080"> 2</span> <span style="color: #0000ff">private</span> <span style="color: #0000ff">int</span> numlocs = 2;<span style="color: #008080"> 3</span> <span style="color: #0000ff">private</span> <span style="color: #0000ff">int</span> numcells = numlocs * numlocs;<span style="color: #008080"> 4</span> <span style="color: #0000ff">private</span> <span style="color: #0000ff">int</span>[] cells;<span style="color: #008080"> 5</span> <span style="color: #0000ff">private</span> BufferedImage bi;<span style="color: #008080"> 6</span> <span style="color: #0000ff">private</span> <span style="color: #0000ff">int</span> w, h, cw, ch;<span style="color: #008080"> 7</span> <span style="color: #0000ff">public</span> MyTest() {<span style="color: #008080"> 8</span> <span style="color: #0000ff">try</span> {<span style="color: #008080"> 9</span> bi = ImageIO.read(<span style="color: #0000ff">new</span> File("D:/desktop.png"));<span style="color: #008080">10</span> w = bi.getWidth();<span style="color: #008080">11</span> h = bi.getHeight();<span style="color: #008080">12</span> } <span style="color: #0000ff">catch</span> (IOException e) {<span style="color: #008080">13</span> e.printStackTrace();<span style="color: #008080">14</span> }<span style="color: #008080">15</span> <span style="color: #008000">//</span><span style="color: #008000">将整个图片分隔成为4分,2行 * 2列,这里cx和cy是每个子图片的宽和高</span><span style="color: #008000"></span><span style="color: #008080">16</span> cw = w / numlocs;<span style="color: #008080">17</span> ch = h / numlocs;<span style="color: #008080">18</span> cells = <span style="color: #0000ff">new</span> <span style="color: #0000ff">int</span>[numcells];<span style="color: #008080">19</span> <span style="color: #008000">//</span><span style="color: #008000">初始化每个子图片的位置信息</span><span style="color: #008000"></span><span style="color: #008080">20</span> <span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> i = 0; i < numcells; i++)<span style="color: #008080">21</span> cells[i] = i;<span style="color: #008080">22</span> }<span style="color: #008080">23</span> <span style="color: #0000ff">void</span> doExchange() {<span style="color: #008080">24</span> Random rand = <span style="color: #0000ff">new</span> Random();<span style="color: #008080">25</span> <span style="color: #0000ff">int</span> ri;<span style="color: #008080">26</span> <span style="color: #008000">//</span><span style="color: #008000">将2 * 2 = 4个图片的位置通过随机数的方式打乱。</span><span style="color: #008000"></span><span style="color: #008080">27</span> <span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> i = 0; i < numcells; i++) {<span style="color: #008080">28</span> <span style="color: #0000ff">while</span> ((ri = rand.nextInt(numlocs)) == i) {<span style="color: #008080">29</span> }<span style="color: #008080">30</span> <span style="color: #0000ff">int</span> tmp = cells[i];<span style="color: #008080">31</span> cells[i] = cells[ri];<span style="color: #008080">32</span> cells[ri] = tmp;<span style="color: #008080">33</span> }<span style="color: #008080">34</span> }<span style="color: #008080">35</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> paintComponent(Graphics g) {<span style="color: #008080">36</span> <span style="color: #0000ff">super</span>.paintComponent(g);<span style="color: #008080">37</span> <span style="color: #0000ff">int</span> dx, dy;<span style="color: #008080">38</span> <span style="color: #008000">//</span><span style="color: #008000">逐个渲染乱序后的每个子图片</span><span style="color: #008000"></span><span style="color: #008080">39</span> <span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> x = 0; x < numlocs; x++) {<span style="color: #008080">40</span> <span style="color: #0000ff">int</span> sx = x * cw;<span style="color: #008080">41</span> <span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> y = 0; y < numlocs; y++) {<span style="color: #008080">42</span> <span style="color: #0000ff">int</span> sy = y * ch;<span style="color: #008080">43</span> <span style="color: #0000ff">int</span> cell = cells[x * numlocs + y];<span style="color: #008080">44</span> dx = (cell / numlocs) * cw;<span style="color: #008080">45</span> dy = (cell % numlocs) * ch;<span style="color: #008080">46</span> <span style="color: #008000">//</span><span style="color: #008000">参数说明:</span><span style="color: #008080">47</span> <span style="color: #008000"> </span><span style="color: #008000">//</span><span style="color: #008000">BufferedImage: 目标绘制图像缓冲区</span><span style="color: #008080">48</span> <span style="color: #008000"> </span><span style="color: #008000">//</span><span style="color: #008000">dx1,dy1: 绘制目标的左上角x,y坐标</span><span style="color: #008080">49</span> <span style="color: #008000"> </span><span style="color: #008000">//</span><span style="color: #008000">dx2,dy2: 绘制目标的右下角x,y坐标</span><span style="color: #008080">50</span> <span style="color: #008000"> </span><span style="color: #008000">//</span><span style="color: #008000">sx1,sy1: 源图像(第一个参数)的左上角x,y坐标</span><span style="color: #008080">51</span> <span style="color: #008000"> </span><span style="color: #008000">//</span><span style="color: #008000">sx2,sy2: 源图像(第一个参数)的右下角x,y坐标</span><span style="color: #008000"></span><span style="color: #008080">52</span> g.drawImage(bi, dx, dy, dx + cw, dy + ch, sx, sy, sx + cw, sy + ch, <span style="color: #0000ff">null</span>);<span style="color: #008080">53</span> }<span style="color: #008080">54</span> }<span style="color: #008080">55</span> }<span style="color: #008080">56</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">void</span> main(String[] args) {<span style="color: #008080">57</span> JFrame frame = <span style="color: #0000ff">new</span> JFrame();<span style="color: #008080">58</span> frame.setTitle("BufferedImage");<span style="color: #008080">59</span> frame.setSize(1000, 600);<span style="color: #008080">60</span> frame.addWindowListener(<span style="color: #0000ff">new</span> WindowAdapter() {<span style="color: #008080">61</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> windowClosing(WindowEvent e) {<span style="color: #008080">62</span> System.exit(0);<span style="color: #008080">63</span> }<span style="color: #008080">64</span> });<span style="color: #008080">65</span> Container contentPane = frame.getContentPane();<span style="color: #008080">66</span> MyTest p = <span style="color: #0000ff">new</span> MyTest();<span style="color: #008080">67</span> contentPane.add(p);<span style="color: #008080">68</span> p.doExchange();<span style="color: #008080">69</span> frame.show();<span style="color: #008080">70</span> }<span style="color: #008080">71</span> }</pre> </div>
试试其它关键字
BufferedImage
同语言下
.
List 切割成几份 工具类
.
一行一行读取txt的内容
.
Java PDF转换成图片并输出给前台展示
.
java 多线程框架
.
double类型如果小数点后为零则显示整数否则保留两位小
.
将图片转换为Base64字符串公共类抽取
.
sqlParser 处理SQL(增删改查) 替换schema 用于多租户
.
JAVA 月份中的第几周处理 1-7属于第一周 依次类推 29-
.
java计算两个经纬度之间的距离
.
输入时间参数计算年龄
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
Dezai.CN
贡献的其它代码
(
4037
)
.
多线程Socket服务器模块
.
生成随机密码
.
清除浮动样式
.
弹出窗口居中
.
抓取url的函数
.
使用base HTTP验证
.
div模拟iframe嵌入效果
.
通过header转向的方法
.
Session操作类
.
执行sqlite输入插入操作后获得自动编号的ID
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3