Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8904035
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T02:01:13+00:00 2026-06-15T02:01:13+00:00

I have an assigment where I need to animate a 3d cube being drawn

  • 0

I have an assigment where I need to animate a 3d cube being drawn with lines connecting one by one to form the cube. after that, each side of the cube needs to be colored in a different color, once each is colored in, pause, then color the next side.

How do I go about doing this? I have provided my code, I try filling in the background square right after the cube is made but I get the following error:

 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)

here is my code, let me know what the best way to do this would be.

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 !");

}
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T02:01:14+00:00Added an answer on June 15, 2026 at 2:01 am

    There are two problems here: the ArrayIndexOutOfBoundsException and an unfortunate coincidence.

    Your flood method includes a loop that generates points in every direction from the previous point tested, but doesn’t say when to stop. The result is that a point can be generated outside of the 300×300 area, throwing an exception when image.getRGB(int,int) is called.
    It’s generally a good idea to remember the limits of your array when looping operations on its elements. For example:

    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
    

    Or less precisely:

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

    Also, the point you start the flood from happens to lie on one of the construction lines of the cube, so the lines are filled instead of the spaces. Be more careful when choosing the points to start at.

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

    should work.

    Hope this helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a detail view that includes three UIButtons, each of which pushes a
I have an assignment in my java class that i need to recursively print
I have an assignment in which I need to create a function that tells
I have an assignment that I need to create a custom C type string
I have a homework assignment where I need to take input from a file
Hi I have a homework assignment where I need to implement an intersection of
Hello is have a question for a school assignment i need to : Read
I have an assignment (i think a pretty common one) where the goal is
For my Operating Systems class I have an assignment due that is built onto
So I am being taught assembly and we have an assignment which is to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.