Java小畫家 |
尚未結案
|
vespa
一般會員 發表:14 回覆:22 積分:7 註冊:2004-02-13 發送簡訊給我 |
|
RaynorPao
版主 發表:139 回覆:3622 積分:7025 註冊:2002-08-12 發送簡訊給我 |
引言: 請教大家 我想用java寫一個在封閉區塊中填色的功能 類似小畫家的填入色彩 我用google搜尋了好久 找不到適合的程式碼 麻煩大家給我些指引或參考資料 謝謝....vespa 你好: 在這裡有範例,先參考看看 src="http://delphi.ktop.com.tw/loadfile.php?TOPICID=10003806&CC=223734"> -- Enjoy Researching & Developing --
------
-- 若您已經得到滿意的答覆,請適時結案!! -- -- 欲知前世因,今生受者是;欲知來世果,今生做者是 -- -- 一切有為法,如夢幻泡影,如露亦如電,應作如是觀 -- |
vespa
一般會員 發表:14 回覆:22 積分:7 註冊:2004-02-13 發送簡訊給我 |
|
neoart
版主 發表:22 回覆:582 積分:425 註冊:2003-05-09 發送簡訊給我 |
|
vespa
一般會員 發表:14 回覆:22 積分:7 註冊:2004-02-13 發送簡訊給我 |
|
neoart
版主 發表:22 回覆:582 積分:425 註冊:2003-05-09 發送簡訊給我 |
import java.applet.*; import java.awt.*; import java.awt.image.*; import java.util.*; class Segment { int y, xl, xr, dy; Segment(int y, int xl, int xr, int dy) { this.y = y; this.xl = xl; this.xr = xr; this.dy = dy; } } /* * fill: set the pixel at (x,y) and all of its 4-connected neighbors * with the same pixel value to the new pixel value nv. * A 4-connected neighbor is a pixel above, below, left, or right of a pixel. */ public class FloodFill extends Applet implements ImageConsumer { int ulx, uly, lrx, lry; /* Limited to this region */ int width, height; ImageProducer src; Image img; Stack stack; int image[]; final private void push(int y, int xl, int xr, int dy) { if (y + dy >= uly && y + dy <= lry) stack.push(new Segment(y, xl, xr, dy)); } final private int pixelread(int x, int y) { return image[y * width + x]; } final private void pixelwrite(int x, int y, int p) { image[y * width + x] = p; } private void fill(int x, int y, int nv) { int l, x1, x2, dy; int ov; Segment s; System.out.println("fill(" + x + "," + y + ")"); stack = new Stack(); ov = pixelread(x, y); /* read pv at seed point */ if (ov == nv || x < ulx || x > lrx || y < uly || y > lry) return; push(y, x, x, 1); push(y+1, x, x, -1); /* seed segment (popped 1st) */ while (!stack.empty()) { /* pop segment off stack and fill a neighboring scan line */ s = (Segment)stack.pop(); dy = s.dy; y = s.y + dy; x1 = s.xl; x2 = s.xr; /* * segment of scan line y-dy for x1<=x<=x2 was * previously filled, now explore adjacent pixels * in scan line y */ for(x = x1; x >= ulx && pixelread(x, y) == ov; x--) pixelwrite(x, y, nv); if (x >= x1) { for (;;) { for (x++; x <= x2 && pixelread(x, y) != ov; x++) ; l = x; if (x > x2) break; for (; x <= lrx && pixelread(x, y) == ov; x++) pixelwrite(x, y, nv); push(y, l, x-1, dy); if (x > x2+1) push(y, x2+1, x-1, -dy); } } else { l = x + 1; if (l < x1) push(y,l,x1-1,-dy); x = x1 + 1; do { for (; x <= lrx && pixelread(x, y) == ov; x++) pixelwrite(x, y, nv); push(y, l, x-1, dy); if (x > x2+1) push(y, x2+1, x-1, -dy); for (x++; x <= x2 && pixelread(x, y) != ov; x++) ; l = x; } while (x <= x2); } } } public void paint(Graphics g) { update(g); } public void update(Graphics g) { if (img != null) g.drawImage(img, 0, 0, this); } private int parseColor(String s) { Color c; if (s == null || s.equals("black")) c = Color.black; else if (s.equals("blue")) c = Color.blue; else if (s.equals("cyan")) c = Color.cyan; else if (s.equals("darkGray")) c = Color.darkGray; else if (s.equals("green")) c = Color.green; else if (s.equals("lightGray")) c = Color.lightGray; else if (s.equals("magenta")) c = Color.magenta; else if (s.equals("orange")) c = Color.orange; else if (s.equals("pink")) c = Color.pink; else if (s.equals("red")) c = Color.red; else if (s.equals("white")) c = Color.white; else if (s.equals("yellow")) c = Color.yellow; else if (s.charAt(0) == '#') return 0xff000000|Integer.parseInt(s.substring(1),16); else c = Color.black; return c.getRGB(); } /* ** ImageConsumer implementation */ public void imageComplete(int status) { StringTokenizer st; int color; src.removeConsumer(this); System.out.println("imageComplete: " + status); color = parseColor(getParameter("color")); st = new StringTokenizer(getParameter("fill"), ","); while (st.hasMoreTokens()) fill(Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken()), color); img = createImage (new MemoryImageSource (width, height, image, 0, width)); repaint(); } public void setColorModel(ColorModel model) { } public void setDimensions(int width, int height) { System.out.println("setDimensions: " + width + " x " + height); ulx = 0; uly = 0; lrx = width - 1; lry = height - 1; this.width = width; this.height = height; image = new int[width * height]; } public void setHints(int hintflags) { } public void setPixels(int x, int y, int w, int h, ColorModel model, byte pixels[], int off, int scansize) { int i, j; for (j = 0; j < h; ++j) for (i = 0; i < w; ++i) image[(y+j)*this.width + x + i] = model.getRGB(pixels[j*scansize+i+off]); } public void setPixels(int x, int y, int w, int h, ColorModel model, int pixels[], int off, int scansize) { int i, j; for (j = 0; j < h; ++j) for (i = 0; i < w; ++i) image[(y+j)*this.width + x + i] = model.getRGB(pixels[j*scansize+i+off]); } public void setProperties(Hashtable props) { } public void init() { System.out.println("Init src = " + src); System.out.println("img=" + getParameter("img")); System.out.println("base=" + getDocumentBase()); img = getImage(getDocumentBase(), getParameter("img")); src = img.getSource(); src.startProduction(this); } public boolean handleEvent(Event e) { if (e.id == Event.MOUSE_UP && img != null) { fill(e.x, e.y, Color.green.getRGB()); img = createImage (new MemoryImageSource (width, height, image, 0, width)); repaint(); } return false; } }資料出處: http://groups.google.com.tw/groups?hl=zh-TW&lr=&ie=UTF-8&frame=right&th=82c4473828552eaf&seekm=MSA.97Apr19123140%40anise.tte.vtt.fi#link1 我沒有試,不過應該是了 如果你試出來的話,麻煩給一下screen snapshoot,好給我聞香一下吧. 發表人 - neoart 於 2004/08/20 08:14:57 |
vespa
一般會員 發表:14 回覆:22 積分:7 註冊:2004-02-13 發送簡訊給我 |
|
vespa
一般會員 發表:14 回覆:22 積分:7 註冊:2004-02-13 發送簡訊給我 |
|
neoart
版主 發表:22 回覆:582 積分:425 註冊:2003-05-09 發送簡訊給我 |
引言: 我試出來了,填滿的效果如下圖所示 .....強強強,水水水!!!真的給你太恭喜了.請問你的flood fill是用什麼寫的? 要改變look & feel的話,要去找外掛的look & feel套件吧 http://www.incors.com/lookandfeel/ or https://sourceforge.net/projects/liquidlnf (似mac下的liquid look& feel,水噹噹的哦) 如果是要改變成類似windows 下無框的frame的話,那就要用到JNI了. |
richtop
資深會員 發表:122 回覆:646 積分:468 註冊:2003-06-10 發送簡訊給我 |
|
vespa
一般會員 發表:14 回覆:22 積分:7 註冊:2004-02-13 發送簡訊給我 |
引言: 強強強,水水水!!!真的給你太恭喜了.請問你的flood fill是用什麼寫的? ..之前您貼的那份程式碼,我研究之後發現不會用,我改參考 http://coloringbook.tripod.com/ floodfill的程式碼 修改之後部份程式碼如下 變數宣告定義: int imgArr[][], cevre[][], buf[][], copyDest[], copySrc[]; int imgw, imgh, orgColor, replaceColor, cevrepix, count; Image repaintImg, orgImg; PixelGrabber pg; MemoryImageSource MIS; imgw = orgImg.getWidth(this); imgh = orgImg.getHeight(this); imgArr = new int[imgh][imgw]; copyDest = new int[imgw * imgh]; copySrc = new int[imgw * imgh]; cevre = new int[10000][2]; buf = new int[10000][2]; pg = new PixelGrabber(orgImg.getSource(), 0, 0, imgw, imgh, copyDest, 0, imgw); try { pg.grabPixels(); } catch(InterruptedException interruptedexception) { } MIS = new MemoryImageSource(imgw, imgh, copyDest, 0, imgw); repaintImg = createImage(MIS); System.arraycopy(copyDest, 0, copySrc, 0, width * height); for(int i = 0; i < height; i ){ for(int j = 0; j < width; j ){ imgArr[i][j]= copyDest[width * i j]; } } public boolean mouseDown(Event event, int k, int l) { orgColor = imgArr[l][k]; if(orgColor != 0xff000000 && orgColor != replaceColor) { imgArr[l][k] = replaceColor; fill(l, k); while(count != 0) { for(int i1 = 0; i1 <= count - 1; i1 ) { cevre[i1][0] = buf[i1][0]; cevre[i1][1] = buf[i1][1]; } cevrepix = count - 1; count = 0; int j1 = 0; while(j1 <= cevrepix) { fill(cevre[j1][1], cevre[j1][0]); j1 ; } } cevrepix = 0; } for(i = 0; i < imgh; i ) for(j = 0; j < imgw; j ) copyDest[imgw * i j] = imgArr[i][j]; repaintImg = createImage(MIS); repaint(); return true; } public void fill(int x, int y) { if(x >= 0 && y - 1 >= 0 && y - 1 < width && x < height && imgArr[x][y - 1] == orgColor ) { imgArr[x][y - 1] = replaceColor; buf[count][0] = y - 1; buf[count][1] = x; count ; } if(x - 1 >= 0 && y >= 0 && y < width && x - 1 < height && imgArr[x - 1][y] == orgColor) { imgArr[x - 1][y] = replaceColor; buf[count][0] = y; buf[count][1] = x - 1; count ; } if(x 1 >= 0 && y >= 0 && y < width && x 1 < height && imgArr[x 1][y] == orgColor) { imgArr[x 1][y] = replaceColor; buf[count][0] = y; buf[count][1] = x 1; count ; } if(x >= 0 && y 1 >= 0 && y 1 < width && x < height && imgArr[x][y 1] == orgColor) { imgArr[x][y 1] = replaceColor; buf[count][0] = y 1; buf[count][1] = x; count ; } }謝謝你look & feel的建議 我們會再試試 引言: vespa 您好: 您的美工能力真是不錯!程式也很棒! 真是羨慕,這能力可以教嗎? RichTop 敬上沒想到我和我同學兩人寫的東西會有人覺得不錯, 這是我一開始著手寫這個小軟體時所想不到的 我們從開啟一張圖片開始寫,開啟兩張圖片比對 到轉灰階轉黑白,一直到現在的加入顏色工具列可以做塗色 遇到的問題數不清,要感謝的人也不少 尤其特地謝謝版主neoart 您的熱心回覆總能解決我們的心頭大患,少頭痛個好幾天 嗯,我也想分享我們所寫的東西, 就像我們在寫程式的過程中參考別人在網路上放的資料一樣 我在年底我們這份作業完成之後,再把作品放上來給大家瞧瞧.. |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |