2012-11-04 26 views
0

我有一個assigment,我需要動畫一個3d立方體繪製與逐一連接形成立方體的線條。之後,立方體的每一面都需要用不同的顏色着色,一旦每個着色,暫停,然後着色下一側。java GUI三維立方體在所有側面着色

我該如何去做這件事?我提供我的代碼,我在後臺廣場上儘量填充立方體作出後,但我得到了以下錯誤:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Coordinate out of bounds! 
    at sun.awt.image.IntegerInterleavedRaster.getDataElements(IntegerInterleavedRaster.java:203) 
    at java.awt.image.BufferedImage.getRGB(BufferedImage.java:881) 
    at drawCube.floodFill(drawCube.java:50) 
    at drawCube.main(drawCube.java:181) 

這裏是我的代碼,讓我知道什麼是最好的方式做,這將是。

import java.awt.image.BufferedImage; 
import javax.swing.JLabel; 
import javax.swing.ImageIcon; 
import java.awt.Point; 

class drawCube 
{ 
    static void fillImage(BufferedImage image, int red, int green, int blue) 
{ 
int packedRGB = packRgb(255,255,255);//white 

for (int y=0;y<image.getHeight(null);y++) 
{ 
    for (int x=0;x<image.getWidth(null);x++) 
    image.setRGB(x,y,packedRGB); 
} 
} 

public static void floodFill(BufferedImage image, int x,int y, int fillColor) 
{ 
java.util.ArrayList<Point> examList=new java.util.ArrayList<Point>(); 

int initialColor=image.getRGB(x,y); 
examList.add(new Point(x,y)); 

while (examList.size()>0) 
{ 
    Point p = examList.remove(0); // get and remove the first point in the list 
    if (image.getRGB(p.x,p.y)==initialColor) 
    { 
    x = p.x; y = p.y; 
    image.setRGB(x, y, fillColor); // fill current pixel 

    examList.add(new Point(x-1,y));  // check west neighbor 
    examList.add(new Point(x+1,y));  // check east neighbor 
    examList.add(new Point(x,y-1));  // check north neighbor 
    examList.add(new Point(x,y+1));  // check south neighbor 

    // waitNS(1); // delay to see floodFill() work 
    // repaintImage(image); 

    } 
} 
} 

private static void repaintImage(BufferedImage image) 
{ 
_imageLabel.setIcon(new ImageIcon(image)); 
_imageLabel.repaint(); 
} 

public static void waitNS(long ns) 
{ 
try { Thread.sleep(ns); } // Pause ns 
    catch (Exception ignore) { ; } 
} 

public static int packRgb(int r,int g,int b) 
{ 
return (r*256+g)*256+b; 
} 

static JLabel _imageLabel; 
public static void main(String[] args) throws Exception 
{ 
// create an 300x300 RGB image 
BufferedImage image=new BufferedImage(300,300,BufferedImage.TYPE_INT_RGB); 

// fill the image with green color 
fillImage(image,0,255,0);   

JLabel imageLabel=new JLabel(); 
_imageLabel = imageLabel; // make it global 
imageLabel.setIcon(new ImageIcon(image)); 
imageLabel.setText("Filling the box with yellow color ..."); 

javax.swing.JFrame window=new javax.swing.JFrame(); 
window.setTitle("Cube Experiment"); 
window.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE); 

window.add(imageLabel); 

window.pack(); 
window.setVisible(true); 

java.awt.Graphics2D gr=(java.awt.Graphics2D) image.getGraphics(); 

int x1 = 50; int y1 = 150; 
int x2 = 150; int y2 = 150; 
int x3 = 150; int y3 = 250; 
int x4 = 50; int y4 = 250; 

gr.setColor(new java.awt.Color(0,0,0)); // blue 
gr.setStroke(new java.awt.BasicStroke(2)); // set pen width to 2 pixels 

gr.drawLine(50, 150, 150, 150); 
repaintImage(image); 
waitNS(500); 

gr.drawLine(150, 150, 150, 250); 
repaintImage(image); 
waitNS(500); 

gr.drawLine(150, 250, 50, 250); 
repaintImage(image); 
waitNS(500); 

gr.drawLine(50, 250, 50, 150); 
repaintImage(image); 
waitNS(500); 

gr.drawLine(0, 300, 100, 300); 
repaintImage(image); 
waitNS(500); 

gr.drawLine(0, 300, 50, 250); 
repaintImage(image); 
waitNS(500); 

gr.drawLine(100, 300, 150, 250); 
repaintImage(image); 
waitNS(500); 


gr.drawLine(100, 300, 100, 200); 
repaintImage(image); 
waitNS(500); 

gr.drawLine(0, 285, 0, 200); 
repaintImage(image); 
waitNS(500); 

gr.drawLine(0, 200, 100, 200); 
repaintImage(image); 
waitNS(500); 

gr.drawLine(0, 200, 50, 150); 
repaintImage(image); 
waitNS(500); 

gr.drawLine(100, 200, 150, 150); 
repaintImage(image); 
waitNS(500); 

// fill the square with yellow color 
int yellow = packRgb(255,255,0); 
int black = packRgb(0,0,0); 
//floodFill(image,(x1+x2)/2, (y1+y4)/2, yellow);//flood fill at center 

imageLabel.setIcon(new ImageIcon(image)); 
imageLabel.setText("Completed !"); 

} 
} 

回答

2



有兩個問題在這裏:在ArrayIndexOutOfBoundsException異常和不幸的巧合。

您的泛洪方法包括一個循環,它可以從前一個測試點的每個方向生成點,但不會說何時停止。其結果是可以在300x300區域外生成一個點,當調用image.getRGB(int,int)時拋出一個異常。
在對元素進行循環操作時,記住數組的限制通常是個好主意。例如:

if(x-1>0) examList.add(new Point(x-1,y)); // check west neighbour 
if(x+1<300) examList.add(new Point(x+1,y)); // check east neighbour 
if(y-1>0) examList.add(new Point(x,y-1)); // check north neighbour 
if(y+1<300) examList.add(new Point(x,y+1)); // check south neighbour 

或者更少的正是:

if (p.x>0 && p.y>0 && p.x<300 && p.y<300 && image.getRGB(p.x,p.y)==initialColor) 

另外,點開始的洪水從發生到趴在立方體的結構線之一,所以線條都充滿代替空間。選擇起點時要小心。

floodFill(image, 60, 160, java.awt.Color.yellow.hashCode()); 

應該工作。

希望這會有所幫助。

+0

謝謝你的幫助。現在我得到了一切工作。 – user1729967