Então pessoal estou com uma dúvida que desde ontem que estou tentando modificar the componentes da class Graphics
e não estou conseguindo, eu fiz um desenho de uma árvore binária com 10 circulos e em cada círculo tem um valor de ordem
do vetor, até ai tudo bem, mas o que eu quero é que mostre a árvore com os valores randômicos do vetor e depois que eu
chamar o método para ordenar fazer com que os círculos recebam o valores ordenados, e é isso que não estou conseguindo
fazer, se alguém poder me ajudar eu agradeço muito!.
segue o código:
import java.util.Random;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
/**
* @(#)HeapSort.java
*
* Class of ordination of the kind <code>Heap Sort</code>,
* where is how if is a tree that receive the values of a
* vector and order they of form more simple that the anther.
*
* @author Wenderson Campos Pereira
* @version 1.00 2009/8/27
*/
public class HeapSort extends JFrame {
private Tree tree;
private int[] array = new int[ 10 ];
private JButton display;
/**
* Default Constructor to implement the components GUI.
*/
public HeapSort() {
super( "My Heap Sort" );
Random rand = new Random();
for ( int i = 0; i < 10; i++ )
this.array[ i ] = rand.nextInt( 20 );
this.tree = new Tree( this.array );
super.add( tree, BorderLayout.CENTER );
this.display = new JButton( "Order" );
super.add( this.display, BorderLayout.SOUTH );
this.display.addActionListener(
/**
* Anonymous inner class to handler the event of the button.
*/
new ActionListener() {
/**
* Method that receive and handler the event of the button.
*
* @param event receive the event of the button to handler.
*/
@Override
public void actionPerformed( ActionEvent event ) {
tree.configurePaintComponent();
tree.repaint();
}
}
);
super.setSize( 300, 300 );
super.setVisible( true );
super.setLocationRelativeTo( null );
super.setResizable( false );
}
/**
* Class that configure the components of graphic interface
* in the positions corrents for that it have the draw if a
* <code>Tree</code>.
*/
class Tree extends JPanel {
private int[] vector = new int[ 10 ];
/**
* Default Constructor, to repaint the components of the JPanel.
*/
public Tree() {
}
/**
* Class of one parameter, that receive a vector with the
* values already inserteds, to attibute inside of the circles
* that they are in the window.
*
* @param array it write the yours values inside of ths circles.
*/
public Tree( int[] array ) {
super();
for ( int i = 0; i < 10; i++ )
this.vector[ i ] = array[ i ];
}
/**
* Configure the components of class <code>Graphics</code>.
*
* @param g is the object of the class <code>Graphics</code>
* to configure the components in the JPanel.
*/
@Override
public void paintComponent(Graphics g ) {
super.paintComponent( g );
// circle father A
g.setColor( Color.WHITE );
g.fillOval( 150, 40, 20, 20 );
// draw a "String" inside of circle
g.setColor( Color.BLACK );
g.drawString( "" + this.vector[ 0 ], 155, 55 );
// draw a line of the circle A even the B
g.setColor( Color.BLUE );
g.drawLine( 150, 55, 110, 100 );
// left son B of A
g.setColor( Color.WHITE );
g.fillOval( 100, 90, 20, 20 );
// draw a "String" inside of circle
g.setColor( Color.BLACK );
g.drawString( "" + this.vector[ 1 ], 105, 105 );
// right son C of A
g.setColor( Color.WHITE );
g.fillOval( 200, 90, 20, 20 );
// draw a "String" inside of circle
g.setColor( Color.BLACK );
g.drawString( "" + this.vector[ 2 ], 205, 105 );
// draw a line of the circle A even the C
g.setColor( Color.BLUE );
g.drawLine( 170, 55, 205, 90 );
// left son D of B
g.setColor( Color.WHITE );
g.fillOval( 70, 140, 20, 20 );
// draw a "String" inside of circle
g.setColor( Color.BLACK );
g.drawString( "" + this.vector[ 3 ], 75, 155 );
// draw a line of the circle B even the D
g.setColor( Color.BLUE );
g.drawLine( 103, 108, 82, 140 );
// right son E of B
g.setColor( Color.WHITE );
g.fillOval( 130, 140, 20, 20 );
// draw a "String" inside of circle
g.setColor( Color.BLACK );
g.drawString( "" + this.vector[ 4 ], 135, 155 );
// draw a line of the circle B even the E
g.setColor( Color.BLUE );
g.drawLine( 120, 108, 135, 140 );
// left son F of C
g.setColor( Color.WHITE );
g.fillOval( 170, 140, 20, 20 );
// draw a "String" inside of circle
g.setColor( Color.BLACK );
g.drawString( "" + this.vector[ 5 ], 175, 155 );
// draw a line of the circle C even the F
g.setColor( Color.BLUE );
g.drawLine( 205, 108, 183, 140 );
// ritht son G of C
g.setColor( Color.WHITE );
g.fillOval( 230, 140, 20, 20 );
// draw a "String" inside of circle
g.setColor( Color.BLACK );
g.drawString( "" + this.vector[ 6 ], 235, 155 );
// draw a line of the circle C even the G
g.setColor( Color.BLUE );
g.drawLine( 215, 110, 238, 140 );
// right son H of D
g.setColor( Color.WHITE );
g.fillOval( 45, 190, 20, 20 );
// draw a "String" inside of circle
g.setColor( Color.BLACK );
g.drawString( "" + this.vector[ 7 ], 50, 205 );
// draw a line of the circle D even the H
g.setColor( Color.BLUE );
g.drawLine( 73, 158, 58, 190 );
// right son I of D
g.setColor( Color.WHITE );
g.fillOval( 85, 190, 20, 20 );
// draw a "String" inside of circle
g.setColor( Color.BLACK );
g.drawString( "" + this.vector[ 8 ], 90, 205 );
// draw a line of the circle D even the I
g.setColor( Color.BLUE );
g.drawLine( 83, 160, 95, 188 );
// right son J of E
g.setColor( Color.WHITE );
g.fillOval( 113, 190, 20, 20 );
// draw a "String" inside of circle
g.setColor( Color.BLACK );
g.drawString( "" + this.vector[ 9 ], 115, 205 );
// draw a line of the circle E even the J
g.setColor( Color.BLUE );
g.drawLine( 135, 159, 122, 190 );
}
/**
* Method that it order the vector of the class, inside of this
* method is create a object of the class <code>Graphics</code>
* that will be passed for a method <code>paintComponent</code>
* of this class, and too order the vector passed after.
*
* @param array is the vector the to be inserted the yours values
* inside of the circles of the window.
*/
public void configurePaintComponent() {
this.orderArray( array, 0, array.length - 1 );
StringBuilder string = new StringBuilder();
string.append( "[ " );
for ( int i = 0; i < array.length; i++ ) {
string.append( array[ i ] );
if ( i + 1 < array.length )
string.append( "," );
}
string.append( " ]" );
JOptionPane.showMessageDialog( null, string );
}
/**
* Method that return the vector of this class.
*
* @return the vector of the class.
*/
public int[] getArray() {
return this.vector;
}
/**
* Private method to order the vector passed how parameter.
*
* @param array is the vector the to be ordered.
*/
private void orderArray( int[] array, int begin, int end ) {
int i = begin;
int j = end;
int x = array[ ( ( i + j ) / 2 ) ];
do {
while ( array[ i ] < x ) i++;
while ( array[ j ] > x ) j--;
if ( i <= j ) {
int aux = array[ i ];
array[ i ] = array[ j ];
array[ j ] = aux;
i++;
j--;
}
} while ( i <= j );
if ( begin < j )
orderArray( array, begin, j );
if ( i < end )
orderArray( array, i, end );
}
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
HeapSort apply = new HeapSort();
apply.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
}
}