2012-11-04 1 views
0

큐브를 형성하기 위해 하나씩 연결하는 선으로 그려지는 3D 큐브를 애니메이션화해야하는 부분이 있습니다. 그 후에 큐브의 각면이 다른 색으로 채워질 필요가 있습니다. 한 번씩 색이 들어가고 잠시 멈추고 다음면을 색칠합니다.자바 GUI 모든면에서 3D 큐브 색상 지정

어떻게해야합니까? 큐브가 만들어진 후 난 배경 광장 오른쪽 작성하려고 내 코드를 제공하고 있지만 다음과 같은 오류 얻을 : 나에게 가장 좋은 방법이 될 것이다 이렇게 알려, 여기

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) 

내 코드입니다 . 는, ArrayIndexOutOfBoundsException과 불행한 우연의 일치 : 여기에 두 가지 문제가

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



있습니다.

홍수 메서드에는 테스트 된 이전 점에서 모든 방향으로 점을 생성하지만 중지 할 시점은 표시하지 않는 루프가 포함되어 있습니다. 결과적으로 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