Need Java Programming Help Quick!
Apr 5, 2006 at 12:17 AM Thread Starter Post #1 of 14

Aman

Headphoneus Supremus
Joined
May 12, 2004
Posts
4,475
Likes
14
I know that a few people here are pretty good at Java. I, on the other hand, am not. I am okay at C++, but this language is relatively new to me and I'm not as experienced at it. This may be a very strange thing to ask of the forums, especially since the program is so elementary, but can anybody please help me out in finding out what is wrong with my code?

The code is simply a fill image program. It draws a triangle and a square, and it simply changes the "*" symbols to "." symbols within the image's array. It's really just an 'example' of how it could be done. It's a long story.

Code:

Code:
[left]public class image{ public static int i; //y public static int k; //x public static String array[][] = new String[10][20]; public static void drawImage(){ for (i = 0; i < 20; i++){ for(k = 0; k < 10; k++){ array[k][i] = "*"; } } //^Makes entire image filled with "*" array[0][0] = "*"; array[1][0] = "-"; array[2][0] = "|"; array[3][0] = "|"; array[4][0] = "|"; array[5][0] = "|"; array[6][0] = "|"; array[7][0] = "|"; array[8][0] = "-"; array[9][0] = "*"; //^One collumn of square for(k = 0; k < 10; k++){ array[k][19] = array[k][0]; } //^Makes second collumn of square array[1][0] = "*"; array[1][2] = "-"; array[1][3] = "-"; array[1][4] = "-"; array[1][5] = "-"; array[1][6] = "-"; array[1][7] = "-"; array[1][8] = "-"; array[1][9] = "-"; array[1][10] = "-"; array[1][11] = "-"; array[1][12] = "-"; array[1][13] = "-"; array[1][14] = "-"; array[1][15] = "-"; array[1][16] = "-"; array[1][17] = "-"; array[1][18] = "-"; array[1][19] = "*"; //^Makes first line in square for(i = 0; i < 20; i++){ array[9][i] = array[1][i]; } //^Makes second line in square array[4][8] = "/"; array[4][9] = "\\"; array[5][7] = "/"; array[5][10] = "\\"; array[6][6] = "/"; array[6][11] = "\\"; array[7][5] = "/"; array[7][12] = "\\"; array[8][4] = "-"; array[8][5] = "-"; array[8][6] = "-"; array[8][7] = "-"; array[8][8] = "-"; array[8][9] = "-"; array[8][10] = "-"; array[8][11] = "-"; array[8][12] = "-"; //^Triangle for(k = 0; k < 10; k++){ for(i = 0; i < 20; i++){ System.out.print(array[k][i]); if(i == 19){ System.out.println(" "); } } } //^Draws square and rectangle fillImage(k, i); } public static void fillImage(int k, int i){ if(array[k][i] == "*"){ array[k][i] = "."; } //^Makes each asterix (*) a period (.) k++; i++; if(k > 9 || i > 19){ drawImage(); fillImage(k, i); } //^Call itself using recursion until maximum array values are hit public static void main (String [] args){ drawImage(); } }[/left]

When I go to compile this code, it works just fine. When I run it, it draws the square and circle, but it gives me an "ArrayBoundsException error (10)". I tried a wide assortment of fixes but I get furthest into the program by using the code above. If anybody could help me in finding out what exactly I could do to fix this error, it would mean the world to me. It's very important that I get this done for tomorrow.

Thanks in advance for any help!!
 
Apr 5, 2006 at 12:43 AM Post #2 of 14

mjg

Headphoneus Supremus
Joined
Jun 20, 2003
Posts
4,755
Likes
10
ok.. careful how your using k and I.

look at the fillImage method in the case if k=9 and i=19.

if (k > 9 || i > 19) {
drawImage();

isn't true, so you you call fillImage recursively. In the next call k=10 and i=20 and you attempt to address array[10][20] which will case the array index out of bounds exception.

This recursion is very chaotic and hard to follow. You should try a more linear type of style, you don't need nested recursive calls. Regardless after you fix this bug your program will have an infinite recursion. If you want to use a recursion, you need a proper base case case to stop the process.

I added a fake base case...

public static void fillImage(int k, int i){

if(k> 9 || i >19)
return;

if (array[k] == "*") {
array[k] = ".";
} //^Makes each asterix (*) a period (.)
k++;
i++;

if (k > 9 || i > 19) {
drawImage();

fillImage(k, i);
}
}


your program doesnt crash anymoer, but the output looks messed up:

********************

**-----------------*

|******************|

|******************|

|*******/\*********|

|******/**\********|

|*****/****\*******|

|****/******\******|

-***---------******-

**-----------------*
 
Apr 5, 2006 at 12:58 AM Post #3 of 14

Aman

Headphoneus Supremus
Joined
May 12, 2004
Posts
4,475
Likes
14
Yeah, a mess is what I'd call it too. I started working on this an hour ago and am very rushed right now. Stressful.
eek.gif


But hey, you're definitely right!

If you think I made a separate method for actually DRAWING the shapes in the array that would make it better? So, for example, the bottom code you referenced to, would that be better in its own separate method I would call from main or drawImage?

Or does my logic need complete re-thinking? I have a tendency to over-think things, so this would not be surprising to me.

Thanks so much for the quick response. I know this is a tricky task so I really appreciate the help.

And PS: No need to edit out your 'insults' to my code - no need to do so if the statements were true!
 
Apr 5, 2006 at 1:17 AM Post #5 of 14

mjg

Headphoneus Supremus
Joined
Jun 20, 2003
Posts
4,755
Likes
10
I'd get rid of all this recursion for starters, it would make the task at hand alot clearer and easier to work with!

Anything that can be done in a recursion can be done in a loop, so long as it's tail recursive... All of your recursive calls are done at the bottom of the of the functions so this is no problem... recursions can be fun and sometimes useful, but forcing something to be recursive that doesn't make sense can be really disgusting.

Also if your making recursions, don't use global variables like that... I'll look at your code for a bit longer, I guess I'm bored enough to give it a shot.
 
Apr 5, 2006 at 1:22 AM Post #6 of 14

imported_Echo_

500+ Head-Fier
Joined
Mar 22, 2005
Posts
559
Likes
10
document man makes it a ton easier

also why dont you try using a JPanel

its probably easier and you can do so much more than the terminal window


Quote:

*
* Chamal
* 0.1
*/
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class Draw extends JFrame implements ActionListener
{
private Container cp; //container
private Panel panel; //panel
private JMenuBar bar; //menu bar
private JMenuItem quit, erase, circle, rectangle, square, white, red, yellow, blue, green, black, orange, purple; //jmenu items for user to pick
private JMenu fileMenu, edit, shape, background, shapeC; //Jmenu for user to pick
private JMenuItem menuSelected; //holder variable for actionListener
private Graphics g; //grpahics g
private Color shapeColor; //sets default color for shape color to white
private Color backgroundColor=Color.BLUE; //sets defualt color for background to blue
private int x,y,size,size2; //variables for x and y location of shapes and size of shapes

//default contructor
public Draw()
{
setSize(1024,768);//sets window size to 1024x768
setLocation(0,0); //sets it to 0,0
setTitle("Draw"); //sets title to draw
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE); //lets the program exit on close
cp = getContentPane();
cp.setLayout(new BorderLayout());
cp.setBackground (backgroundColor); //sets background color
setVisible(true);

}
public void showGUI()
{
createMenu(); //cals create menu method
show();
}
public void main (String [] args)
{
Draw md = new Draw(); //Calls constructor draw
g=cp.getGraphics();
showGUI(); //calls showgGui
}
private class menuListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
//if statements for which jmenuitem is selected
menuSelected = (JMenuItem) event.getSource();
if(menuSelected.equals(quit))
{
System.exit(0);
}
if(menuSelected.equals(circle) || menuSelected.equals(square) || menuSelected.equals(rectangle))
{
randomIntegers();
createShape(false);
}
if(menuSelected.equals(white) || menuSelected.equals(red) || menuSelected.equals(yellow) || menuSelected.equals(blue))
{
changeColors();
}
if(menuSelected.equals(green) || menuSelected.equals(black) || menuSelected.equals(orange) || menuSelected.equals(purple))
{
changeShapeColor();
}
if(menuSelected.equals(erase)) //if erase is hit changes the setcolor to background color and calls create shape
{


createShape(true);

}
}

}
public void changeShapeColor() //method that changes the color of shapes
{
if(menuSelected.equals(green))
shapeColor=(Color.GREEN);
if(menuSelected.equals(black))
shapeColor=(Color.BLACK);
if(menuSelected.equals(orange))
shapeColor=(Color.ORANGE);
if(menuSelected.equals(purple))
shapeColor=(new Color(171,88,173));
g.setColor(shapeColor);
}
public void createShape(boolean b)
{
if(b==true) //if user selects erase turns all possible shapes to background color
{
g.setColor(backgroundColor);
g.fillOval(x,y,size,size);
g.fillRect(x,y,size,size2);
g.fillRect(x,y,size,size);
g.setColor(shapeColor);
}
if(b==false)
{
if(menuSelected.equals(circle))
g.fillOval(x,y,size,size);
if(menuSelected.equals(rectangle))
g.fillRect(x,y,size,size2);
if(menuSelected.equals(square))
g.fillRect(x,y,size,size);
}
}
public void randomIntegers()//gives random value for x y position of shape and size of shape
{
size=(int)(Math.random()*(51))+50;
size2=(int)(Math.random()*(51))+50;
x=(int)(Math.random()*(925));
y=(int)(Math.random()*(569))+100;
}

public void changeColors() //changes the background colors
{
if(menuSelected.equals(white))
backgroundColor=Color.WHITE;
if(menuSelected.equals(yellow))
backgroundColor=Color.YELLOW;
if(menuSelected.equals(red))
backgroundColor=Color.RED;
if(menuSelected.equals(blue))
backgroundColor=Color.BLUE;
cp.setBackground(backgroundColor);

}
public void actionPerformed(ActionEvent event)
{
System.exit( 0 );
}

public void createMenu()
{
bar = new JMenuBar(); //creates a new jmenubar
cp.add(bar,BorderLayout.NORTH); //sets to border layout
createjMenu(); //calls createJmenu
createjMenuItems(); //calls createJmenuitems
addToBar(bar); //calls addToBar
addToJMenu(); //cals addToJMenu
addActListeners(); //calls addActListners
show(); //cals show
}

private void addToJMenu()
{
fileMenu.add(quit);
edit.add(erase);
shape.add(circle);
shape.add(rectangle);
shape.add(square);
background.add(white);
background.add(red);
background.add(yellow);
background.add(blue);
shapeC.add(green);
shapeC.add(black);
shapeC.add(orange);
shapeC.add(purple);
}
private void addActListeners()
{
quit.addActionListener(new menuListener());
erase.addActionListener(new menuListener());
circle.addActionListener(new menuListener());
rectangle.addActionListener(new menuListener());
square.addActionListener(new menuListener());
white.addActionListener(new menuListener());
red.addActionListener(new menuListener());
yellow.addActionListener(new menuListener());
blue.addActionListener(new menuListener());
green.addActionListener(new menuListener());
black.addActionListener(new menuListener());
orange.addActionListener(new menuListener());
purple.addActionListener(new menuListener());
}

private void addToBar(JMenuBar bar)
{
bar.add( fileMenu ); //adds the file into the menu bar
bar.add( edit ); //adds edit to the menu bar
bar.add( shape ); //adds shape to the menu bar
bar.add( background ); //adds background ot the menu bar
bar.add( shapeC ); //adds it to the menu bar
}
//------creates jmenu--------///
private void createjMenu()
{
fileMenu = new JMenu( "File" );
edit = new JMenu( "Edit" );
shape = new JMenu( "Shape" );
background = new JMenu( "Background" );
shapeC = new JMenu( "ShapeColor" );
}
//--------Creates JMenuItems------------------//
private void createjMenuItems()
{
quit = new JMenuItem("Quit");
erase = new JMenuItem("Erase");
circle = new JMenuItem("Circle");
rectangle = new JMenuItem("Rectangle");
square = new JMenuItem("Square");
white = new JMenuItem("White");
red = new JMenuItem("Red");
yellow = new JMenuItem("Yellow");
blue = new JMenuItem("Blue");
green = new JMenuItem("Green");
black = new JMenuItem("Black");
orange = new JMenuItem("Orange");
purple = new JMenuItem("Purple");
}
}


 
Apr 5, 2006 at 1:24 AM Post #7 of 14

mjg

Headphoneus Supremus
Joined
Jun 20, 2003
Posts
4,755
Likes
10
Quote:

Originally Posted by Echo_
document man makes it a ton easier

also why dont you try using a JPanel

its probably easier and you can do so much more than the terminal window



If he could this in swing, it would be trivial. Can you do this in swing? You'd be in luck, i TA for a gui design course ; )
 
Apr 5, 2006 at 1:32 AM Post #10 of 14

mjg

Headphoneus Supremus
Joined
Jun 20, 2003
Posts
4,755
Likes
10
ok here is my idea.... do this totally flat, no recursion, only one nested loop.. some variables to consider:

a variable for the distance of the edges of the triangle radius from the center, starts at 0, then grows by 2 each time until the edges are on the ends of thje box which means left edge of the box +1 right end of the box -1. Call it edge radius i guess.

a left edge constant, a right edge constant. it'll make the code easy to read if you use constants like that.

You can use some logic to determine what sort of character to draw. At the base index + edge radius draw / ... if your at the end - edge radius, draw a \.

If your at the left side draw a | or the right side |...
If your in the box, but not in the triangle, draw a *, if your in the triangle, draw a -...

y Do this all in one large method body, use good names for the variables. and use a 2d array like you were using to test the indices against your logic to determine which character.
 
Apr 5, 2006 at 1:39 AM Post #11 of 14

wallstreet

100+ Head-Fier
Joined
Feb 14, 2002
Posts
236
Likes
0
I don't know if this is the only problem, because I just skimmed the code, but this recursive function isn't working like you think.

Code:

Code:
[left]public static void fillImage(int k, int i){ if(array[k][i] == "*"){ array[k][i] = "."; } //^Makes each asterix (*) a period (.) k++; i++; if(k > 9 ||i > 19){ drawImage(); } fillImage(k, i); }[/left]

This runs infinitely, as fillImage keeps getting called. Even if it exits the function when drawImage() is called, it returns to fillImage and fillImage gets called again.

*Edit: whoops, looks like several got to this before I did when I stepped away from my computer
smily_headphones1.gif
*
 
Apr 5, 2006 at 2:39 AM Post #12 of 14

Aman

Headphoneus Supremus
Joined
May 12, 2004
Posts
4,475
Likes
14
Hi everyone:

I have taken all of your wonderful suggestions in heart. I have written them all down and will bring them with me tomorrow for when I discuss this ridiculous code with the person who wanted me to do this in the first place.

It must be done with recursion, unfortunately, but I do whole-heartedly appreciate all of the efforts you guys have put in. It just comes to show what only a year of absence from programming will do to your skill level! It shouldn't matter how inexperienced I am with java - the amount of mistakes and lack of logic found in my code, as revealed by you guys, was overwhelming.

Again, I thank you all. I'll be reporting on what exactly I decided to go with tomorrow.

Until then, thank you again, and good night.

-Andrew
 
Apr 5, 2006 at 6:06 PM Post #13 of 14

wallstreet

100+ Head-Fier
Joined
Feb 14, 2002
Posts
236
Likes
0
Quote:

Originally Posted by Aman
Hi everyone:

I have taken all of your wonderful suggestions in heart. I have written them all down and will bring them with me tomorrow for when I discuss this ridiculous code with the person who wanted me to do this in the first place.

It must be done with recursion, unfortunately, but I do whole-heartedly appreciate all of the efforts you guys have put in. It just comes to show what only a year of absence from programming will do to your skill level! It shouldn't matter how inexperienced I am with java - the amount of mistakes and lack of logic found in my code, as revealed by you guys, was overwhelming.

Again, I thank you all. I'll be reporting on what exactly I decided to go with tomorrow.

Until then, thank you again, and good night.

-Andrew



Hey, it happens to all of us, especially when trying to do something at the last minute
wink.gif
.
 
Apr 5, 2006 at 10:43 PM Post #14 of 14

mjg

Headphoneus Supremus
Joined
Jun 20, 2003
Posts
4,755
Likes
10
Quote:

Originally Posted by Aman
Hi everyone:

I have taken all of your wonderful suggestions in heart. I have written them all down and will bring them with me tomorrow for when I discuss this ridiculous code with the person who wanted me to do this in the first place.

It must be done with recursion, unfortunately, but I do whole-heartedly appreciate all of the efforts you guys have put in. It just comes to show what only a year of absence from programming will do to your skill level! It shouldn't matter how inexperienced I am with java - the amount of mistakes and lack of logic found in my code, as revealed by you guys, was overwhelming.

Again, I thank you all. I'll be reporting on what exactly I decided to go with tomorrow.

Until then, thank you again, and good night.

-Andrew



Andrew,

I bust on you alot, but I probablly should ease off. Sorry about that.I think you are doing the right thing by seeking help with your work. Even if we gave you nothing to help, it can help you re-evaluate what parts you assume are correct.

Good luck.
 

Users who are viewing this thread

Top