線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:5457
推到 Plurk!
推到 Facebook!

Java小畫家

尚未結案
vespa
一般會員


發表:14
回覆:22
積分:7
註冊:2004-02-13

發送簡訊給我
#1 引用回覆 回覆 發表時間:2004-08-19 13:27:25 IP:163.13.xxx.xxx 未訂閱
請教大家 我想用java寫一個在封閉區塊中填色的功能 類似小畫家的填入色彩 我用google搜尋了好久 找不到適合的程式碼 麻煩大家給我些指引或參考資料 謝謝....
RaynorPao
版主


發表:139
回覆:3622
積分:7025
註冊:2002-08-12

發送簡訊給我
#2 引用回覆 回覆 發表時間:2004-08-19 15:07:05 IP:203.73.xxx.xxx 未訂閱
引言: 請教大家 我想用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

發送簡訊給我
#3 引用回覆 回覆 發表時間:2004-08-19 15:48:00 IP:163.13.xxx.xxx 未訂閱
引言: vespa 你好: 在這裡有範例,先參考看看 http://www.1javastreet.com/vb/scripts/ShowCode.asp?txtCodeId=4126&lngWId=2
謝謝你!RaynorPao 那個範例我有找過,不過它只能畫圖,不能針對封閉區塊著色 跟我們的需求不同 我要做的類似 http://christiananswers.net/kids/coloring-nicdemus.html 我找到很多這種填色的軟體 不過都沒參考原始碼
neoart
版主


發表:22
回覆:582
積分:425
註冊:2003-05-09

發送簡訊給我
#4 引用回覆 回覆 發表時間:2004-08-19 16:04:48 IP:61.64.xxx.xxx 未訂閱
http://ptolemy.eecs.berkeley.edu/diva/demo/index.html,也許有你要的. 這有必要由C的flood fill演算法改寫.
vespa
一般會員


發表:14
回覆:22
積分:7
註冊:2004-02-13

發送簡訊給我
#5 引用回覆 回覆 發表時間:2004-08-20 07:15:15 IP:163.13.xxx.xxx 未訂閱
謝謝! 我再研究看看...
neoart
版主


發表:22
回覆:582
積分:425
註冊:2003-05-09

發送簡訊給我
#6 引用回覆 回覆 發表時間:2004-08-20 07:45:14 IP:61.56.xxx.xxx 未訂閱
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

發送簡訊給我
#7 引用回覆 回覆 發表時間:2004-08-24 09:12:17 IP:163.13.xxx.xxx 未訂閱
謝謝! 我研究完之後再把結果放上來..
vespa
一般會員


發表:14
回覆:22
積分:7
註冊:2004-02-13

發送簡訊給我
#8 引用回覆 回覆 發表時間:2004-09-01 08:46:29 IP:163.13.xxx.xxx 未訂閱
我試出來了,填滿的效果如下圖所示 另外,我想請問,有沒有方法可以將方型的Frame視窗自訂成自己想要的圖案 比如改成調色盤的方框, 我們目前做的是簡單的影像處理軟體 希望能讓介面"可愛"一點,謝謝..
neoart
版主


發表:22
回覆:582
積分:425
註冊:2003-05-09

發送簡訊給我
#9 引用回覆 回覆 發表時間:2004-09-01 09:48:04 IP:61.64.xxx.xxx 未訂閱
引言: 我試出來了,填滿的效果如下圖所示 .....
強強強,水水水!!!真的給你太恭喜了.請問你的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

發送簡訊給我
#10 引用回覆 回覆 發表時間:2004-09-01 10:52:46 IP:211.76.xxx.xxx 未訂閱
vespa 您好:    您的美工能力真是不錯!程式也很棒! 真是羨慕,這能力可以教嗎?
vespa
一般會員


發表:14
回覆:22
積分:7
註冊:2004-02-13

發送簡訊給我
#11 引用回覆 回覆 發表時間:2004-09-01 16:27:58 IP:163.13.xxx.xxx 未訂閱
引言: 強強強,水水水!!!真的給你太恭喜了.請問你的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 您的熱心回覆總能解決我們的心頭大患,少頭痛個好幾天 嗯,我也想分享我們所寫的東西, 就像我們在寫程式的過程中參考別人在網路上放的資料一樣 我在年底我們這份作業完成之後,再把作品放上來給大家瞧瞧..
系統時間:2024-04-18 21:23:32
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!