d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Not Understanding Why This Is Happening
Add Reply New Topic New Poll
Member
Posts: 33,402
Joined: Feb 19 2006
Gold: 312.85
Apr 29 2014 10:15pm
So I'm working on a project where an array is being created with a random number generator containing 10-100 values, its then being painted as a bar graph on a GUI unsorted. The bars then get cycled through and highlighted/unhighlited until the end of the array.

This was the first part of that project.
Second part is I have to first Sort the array, display that array somewhat in the same manner(no highlighting) but also keep the unsorted array. This is where I'm having trouble, Im getting the array to be sorted, but when I go to paint it onto the interface it takes the place of the unsorted.
I think I explained that right but heres the code for the main class, if you guys could take a look at it and see why it would be taking the unsorted arrays place that would be great.

Code
public class ArrayViewer
{
static int[] array;
static int[] sortedArray;

public static void main(String[] args)
{

int size=0;
Scanner in=new Scanner(System.in);


do
{
System.out.print("Enter the size of the array (should be between 10 and 100): ");

size=in.nextInt();
}
while (size<10 || size>100);



JFrame frame = new JFrame("Graph");
JPanel text = new JPanel(new GridLayout(2,3));
JPanel graph = new JPanel(new GridLayout(1,1));
JPanel sortedGraph = new JPanel(new GridLayout(1,1));
GridLayout grid = new GridLayout(3, 1);
frame.setLayout(grid);


array= ArrayUtil.randomIntArray(size,200);
System.out.println(ArrayUtil.printArray(array));
sortedArray = array;


final ArrayComponent arrayGraph = new ArrayComponent(array);


frame.setSize(1025,700);


graph.add(arrayGraph);


JButton sortArray = new JButton("Click to sort and display the Array");
JButton linSearch = new JButton("Linear Search");
JButton binSearch = new JButton("Binary Search");


JTextArea searchText = new JTextArea("Search Item");
JTextArea resultText = new JTextArea("The result will be placed here");




text.add(binSearch);
text.add(searchText);
text.add(linSearch);
text.add(resultText);



graph.setVisible(true);
text.setVisible(true);

frame.add(text);
frame.add(graph);



frame.setVisible(true);


frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);




class TimerListener implements ActionListener
{

int index = 0;
public void actionPerformed(ActionEvent e)
{

if(0<=index && index<array.length)
{
arrayGraph.highlight(index);
index++;
}

else
{
arrayGraph.noHighlight();
}
}
}

TimerListener listener = new TimerListener();
final Timer timer = new Timer(500,listener);

timer.start();


class sortArrayListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
MergeSorter sorter = new MergeSorter(sortedArray);
sorter.mergeSort();
final ArrayComponent sortArray = new ArrayComponent(sortedArray);

}
}
sortArrayListener sort = new sortArrayListener();
sortArray.addActionListener(sort);

sortedGraph.add(sortArray);
frame.add(sortedGraph);
text.add(sortArray);

}
}


If you guys need to see the other 3 classes I can do that too. Theres ArrayUtil, which does the random numbers, ArrayComponent which uses the PaintComponent to draw the objects, and MergeSorter to merge sort the array.



This post was edited by pyromaniac09 on Apr 29 2014 10:21pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Apr 30 2014 04:47am
i'm not familiar with the ArrayComponent class. what package is this in? do you have a link to the javadoc/documentation?

if i understand you correctly, you want to see both graphs at the same time, one overlayed on the other. if that's the case, you have to make sure your control supports that. read the documentation or look for an example of that being done. i assume "highlight" is what's overwriting the previous one?

This post was edited by carteblanche on Apr 30 2014 05:10am
Member
Posts: 33,402
Joined: Feb 19 2006
Gold: 312.85
Apr 30 2014 02:41pm
Quote (carteblanche @ Apr 30 2014 06:47am)
i'm not familiar with the ArrayComponent class. what package is this in? do you have a link to the javadoc/documentation?

if i understand you correctly, you want to see both graphs at the same time, one overlayed on the other. if that's the case, you have to make sure your control supports that. read the documentation or look for an example of that being done. i assume "highlight" is what's overwriting the previous one?


ArrayComponent is a class I made. I want the unsorted to stay where it is, and the sorted to display below it in the empty space.

Code
public class ArrayComponent extends JComponent
{
int[] theArray;

int highlightIndex;
public ArrayComponent(int[] array)
{
theArray = array;
}

protected void paintComponent(Graphics g)
{
Graphics2D g2 = (Graphics2D) g;
int height = 0;
for(int i = 0; i<theArray.length; i++)
{
height = theArray[i];
int x = (i+1)*10;

if(i==highlightIndex)
{
g.fillRect(x, 5, 5, height);
}
else
{
g.drawRect(x, 5, 5, height);
}
}
}

public void highlight(int i)
{
highlightIndex=i;
repaint();
}
public void noHighlight()
{
highlightIndex=-1;
repaint();
}
}


I dont understand why its changing the unsorted, I've set sortedArray equal to the unsorted array and its what actually going through the mergeSort method. It should be going to its own part of the frame/panel, thats if it were actually being told to draw itself yet onto the panel, so I have no idea how its even changing the unsorted.

This post was edited by pyromaniac09 on Apr 30 2014 02:59pm
Member
Posts: 33,402
Joined: Feb 19 2006
Gold: 312.85
Apr 30 2014 03:46pm
oh nevermind i forgot i am drawing the new sorted array, so is it somehow putting itself on top of the unsorted array making the other dissapear underneath it? If thats what its doing then how do I make it so the sorted goes below it instead of on top of it? Because with what I have thats what I thought it should be doing
Member
Posts: 33,402
Joined: Feb 19 2006
Gold: 312.85
Apr 30 2014 06:13pm
alright I figure it out finally thanks to a friend of mine

as I thought it was something simple I overlooked

my
sortedArray = array; was just setting that array to the same reference so anywhere I used it it was actually changing the array array.
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll