BFS and DFS Navigation in Java Part 2

BFS and DFS navigation part 2

Save Time On Research and Writing
Hire a Pro to Write You a 100% Plagiarism-Free Paper.
Get My Paper

  

1 .- the user select the number of Nodes he wants <=15

2 .- with 0 and 1 he builds the adjacent matriz

3 .- Display it ( the adjacent matriz )

Save Time On Research and Writing
Hire a Pro to Write You a 100% Plagiarism-Free Paper.
Get My Paper

4 .- Display the adjacent list 

5 .- Build the GUI with 2 buttons 

one for BFS Navigation

one for DFS Navigation

6 .-  Display Navigation with the color

7. – When a parent node is being visited paint it black and in a queue display it part (call the queue by exemple visiting)

 

and their children paint gray

8. – After being visited send the node to another queue (call it visited for example)

9. – And a node visited paint it red

  

NB I provide an example with files i uploaded check it ! only missing the queues and the color changes !

inteligencia1/Arbol.java
inteligencia1/Arbol.java
package
 inteligencia1
;

import
 java
.
awt
.
Color
;

import
 java
.
awt
.
Graphics
;

import
 java
.
awt
.
image
.
BufferedImage
;

import
 java
.
util
.
LinkedList
;

import
 javax
.
swing
.
JComponent
;

import
 javax
.
swing
.
SwingUtilities
;

public
 
class
 
Arbol
 
extends
 
JComponent
{

    
BufferedImage
 imagen
;

    
LinkedList
 
< Nodo >
 lista
;

    
    
    
int
 x
,
y
;

    
int
 ancho
,
alto
;

    
boolean
 bfs
;

    
boolean
 inicio
=
false
;

    
LinkedList
< Nodo >
 cola 
=
 
new
 
LinkedList
();

    
LinkedList
< Nodo >
 pila 
=
 
new
 
LinkedList
();

    
Graphics
 g
;

    
    
public
 
Arbol
(){

        
this
.
setBounds
(
450
,
70
,
440
,
500
);

        
this
.
setBackground
(
Color
.
DARK_GRAY
);

        imagen 
=
 
new
 
BufferedImage
(
getWidth
(),
getHeight
(),
BufferedImage
.
TYPE_3BYTE_BGR
);

        imagen
.
getGraphics
().
setColor
(
 
Color
.
LIGHT_GRAY 
);

        imagen
.
getGraphics
().
fillRect
(
0
,
0
,
440
,
500
);

    
}

    @
Override

    
public
 
void
 paint
(
Graphics
 gr
)

    
{
   
        gr
.
drawImage
(
imagen
,
0
,
0
,
this
);

    
    
}
   
    
public
 
void
 addListPaint
(
LinkedList
 
< Nodo >
 lista
){

        
this
.
lista
=
null
;

        
this
.
lista
=
lista
;

        x
=
y
=
0
;

        x
=
17
;

        y
=
14
;

        
int
 xfle
,
yfle
;

        g
=
imagen
.
getGraphics
();

        g
.
setColor
(
 
Color
.
LIGHT_GRAY 
);

        g
.
fillRect
(
0
,
0
,
450
,
500
);

                
        
if
(
lista
!=
null
){

            
//Dibujar los nodos
            
for
 
(
Nodo
 nn 
:
 lista
){

                g
.
setColor
(
 
Color
.
WHITE 
);

                g
.
fillOval
(
nn
.
getXpos
(),
 nn
.
getYpos
(),
 nn
.
getAncho
(),
 nn
.
getAlto
());

                g
.
setColor
(
 
Color
.
BLACK
);

                g
.
drawString
(
nn
.
getName
(),
nn
.
getXpos
()
+
10
,
 nn
.
getYpos
()
+
15
);

            
}

            
//Lineas
            
for
 
(
Nodo
 raiz 
:
  lista
){

                
if
 
(
raiz
.
getNodoListCount
()
>
0
){

                    
for
 
(
Nodo
 hijo 
:
 raiz
.
getListNodo
()){

                        g
.
setColor
(
 
Color
.
WHITE 
);

                        
if
 
(
raiz
.
getXpos
()
==
hijo
.
getXpos
()
 
&&
 raiz
.
getYpos
()
==
hijo
.
getYpos
())

                        
{

                            g
.
drawOval
(
raiz
.
getXpos
()

10
,
 raiz
.
getYpos
()

10
,
 
30
,
 
30
);

                        
}
else
{

                            g
.
setColor
(
 
Color
.
blue
);

                            
if
 
(
raiz
.
isExists
(
hijo
)
 
&&
 hijo
.
isExists
(
raiz
)){

                                g
.
setColor
(
 
Color
.
RED 
);

                                g
.
drawLine
(
raiz
.
getXpos
()
+
x
,
 raiz
.
getYpos
()
+
y
,
 hijo
.
getXpos
()
+
x
,
hijo
.
getYpos
()
+
y
);

                            
}
else
 
{

                                xfle
=
((
int
)(
hijo
.
getXpos
()
+
raiz
.
getXpos
())
/
2
);

                                yfle
=
((
int
)
 
(
hijo
.
getYpos
()
 
+
 raiz
.
getYpos
())
 
/
 
2
);

                                xfle
=
(
int
)(
raiz
.
getXpos
()
+
xfle
)
/
2
;

                                yfle
=
(
int
)(
raiz
.
getYpos
()
+
yfle
)
/
2
;

                                g
.
drawLine
(
raiz
.
getXpos
()
+
x
,
 raiz
.
getYpos
()
+
y
,
 hijo
.
getXpos
()
+
x
,
hijo
.
getYpos
()
+
y
);

                                g
.
drawString
(
“♦”
,
x
+
xfle
,
y
+
yfle
);

                            
}

                            
                        
}

                        
                    
}

                
}

            
}

        
}

        repaint
();

    
}

   
    
    
private
 
void
 
MarcarNegro
(
Nodo
 nodoactual
){

         g
=
imagen
.
getGraphics
();

          g
.
setColor
(
 
Color
.
BLACK 
);

          g
.
fillOval
(
nodoactual
.
getXpos
(),
 nodoactual
.
getYpos
(),
 nodoactual
.
getAncho
(),
 nodoactual
.
getAlto
());

          g
.
setColor
(
 
Color
.
WHITE
);

          g
.
drawString
(
nodoactual
.
getName
(),
nodoactual
.
getXpos
()
+
10
,
 nodoactual
.
getYpos
()
+
15
);

        repaint
();

            
try
 
{

                
Thread
.
sleep
(
1000
);

            
}
 
catch
 
(
InterruptedException
 ex
)
 
{

            
}

            g
.
setColor
(
 
Color
.
DARK_GRAY 
);

        
}

    
    
    
private
 
void
 
MarchaVista
(
Nodo
 nodoactual
){

         g
=
imagen
.
getGraphics
();

         
            g
.
setColor
(
 
Color
.
DARK_GRAY 
);

            g
.
fillOval
(
nodoactual
.
getXpos
(),
 nodoactual
.
getYpos
(),
 nodoactual
.
getAncho
(),
 nodoactual
.
getAlto
());

            g
.
setColor
(
 
Color
.
BLACK
);

            g
.
drawString
(
nodoactual
.
getName
(),
nodoactual
.
getXpos
()
+
10
,
 nodoactual
.
getYpos
()
+
15
);

            nodoactual
.
visitado
=
true
;

        
        repaint
();

        
try
 
{

            
Thread
.
sleep
(
1000
);

        
}
 
catch
 
(
InterruptedException
 ex
)
 
{

        
}

    
}

   
    
public
 
void
 BFS
()

    
{

       addListPaint
(
lista
);

        
MarcarNoVisitado
();

       inicio
=
true
;

       g
=
imagen
.
getGraphics
();

            
//Recorrido BFS
       
Thread
 hilo
;

                hilo 
=
 
new
 
Thread
 
()
 
{
 
              @
Override

              
public
 
void
 run
()
 
{
 
                 bfs
=
true
;

                 cola
.
add
(
lista
.
getFirst
());

                  
while
 
(
!
cola
.
isEmpty
()){

                      
Nodo
 nodoactual
=
cola
.
poll
();

                      
//Marcado visitado
                      
if
 
(
!
nodoactual
.
visitado
){

                            
MarchaVista
(
nodoactual
);

                      
}

                      
try
 
{

                          
Thread
.
sleep
(
1000
);

                      
}
 
catch
 
(
InterruptedException
 ex
)
 
{}

                      
for
 
(
Nodo
 hijonodo 
:
 nodoactual
.
getListNodo
()){

                          
if
 
(
 
!
hijonodo
.
visitado
){

                             
//Visitado(hijonodo);
                             
MarchaVista
(
hijonodo
);

                             cola
.
add
(
hijonodo
);

                          
}

                      
}

                      
try
 
{

                          
Thread
.
sleep
(
1000
);
     
                      
}
 
catch
 
(
InterruptedException
 ex
)
 
{

                      
}
 
                      
if
 
(
!
cola
.
contains
(
nodoactual
)){

                         
MarcarNegro
(
nodoactual
);
 
                      
}

                  
}

              
}

         
};

        hilo
.
start
();

        
    
}
   
    
   
    
private
 
void
 
MarcarNoVisitado
()

    
{

        
for
 
(
Nodo
 nodo 
:
 lista 
){

            nodo
.
visitado
=
false
;

        
}

           
    
}

    
public
 
void
 fondo
()

    
{
   
        g
=
imagen
.
getGraphics
();

        g
.
setColor
(
Color
.
DARK_GRAY
);

        g
.
fillRect
(
0
,
0
,
450
,
500
);

    
}

    
    
//esto es pra que el grafico se actuslize
    
private
 
void
 dibuja
()
 
throws
 
Exception
 
{

        
SwingUtilities
.
invokeAndWait
(
new
 
Runnable
()
 
{

            @
Override

                
public
 
void
 run
()
 
{

                    
                    paintImmediately
(
0
,
 
0
,
500
,
 
300
);

                
}

            
});

    
}

            
}

inteligencia1/MyDefaultTableModel.java
inteligencia1/MyDefaultTableModel.java
package
 inteligencia1
;

import
 java
.
util
.
LinkedList
;

import
 javax
.
swing
.
table
.
DefaultTableModel
;

 
class
 
MyDefaultTableModel
 
extends
 
DefaultTableModel
 
{
  
     
int
 
Row
,
 
Column
;
  
     
     
LinkedList
< Nodo >
 listas 
=
 
new
 
LinkedList
();

     
public
 
MyDefaultTableModel
()
 
{
  
            
super
();
  
        
}

        
public
 
MyDefaultTableModel
(
Object
[]
 columnNames
,
 
int
 rowCount
)
 
{
  
            
super
(
columnNames
,
rowCount
);
 
        
}
 
            @
Override

        
public
 
boolean
 isCellEditable
(
int
 row
,
 
int
 col
)
 
{
  
            
return
 
true
;
  
        
}
 
       
        
        @
Override

        
public
 
void
 setValueAt
(
Object
 aValue
,
 
int
 row
,
int
 column
)

        
{
   
boolean
 vacio
;

            
if
(
aValue
.
toString
().
equals
(
“0”
)
 
||
 aValue
.
toString
().
equals
(
“1”
))

            
{
   vacio
=
(
getValueAt
(
row
,
column
)
==
null
)
?
true
:
false
;

                
if
 
(
 aValue
!=
null
){

                    
super
.
setValueAt
(
aValue
.
toString
(),
 row
,
 column
);

                    
if
 
(
aValue
.
toString
().
equals
(
“1”
))
 
{

                       
if
(
!
Programa
.
lista
.
get
(
row
).
isExists
(
Programa
.
lista
.
get
(
column
)))
 
{

                            
Programa
.
lista
.
get
(
row
).
addNodoList
(
Programa
.
lista
.
get
(
column
));

                        
}

                       
Programa
.
panel
.
addListPaint
(
Programa
.
lista
);

                       
Programa
.
panelarbol
.
addListPaint
(
Programa
.
lista
);

                    
}
else
  
if
 
(
aValue
.
toString
().
equals
(
“0”
)
&&
 
Programa
.
lista
.
get
(
row
).
getNodoListCount
()
>
0
 
&&
 
Programa
.
lista
.
get
(
row
)
!=
null
){

                                
Programa
.
lista
.
get
(
row
).
remNodoList
(
Programa
.
lista
.
get
(
column
));

                                
Programa
.
panel
.
addListPaint
(
Programa
.
lista
);

                                
Programa
.
panelarbol
.
addListPaint
(
Programa
.
lista
);

                           
}
      
                
}
           
            
}
else
{

               
super
.
setValueAt
(
“0”
,
 row
,
 column
);

                
Programa
.
lista
.
get
(
row
).
remNodoList
(
Programa
.
lista
.
get
(
column
));

                
Programa
.
panel
.
addListPaint
(
Programa
.
lista
);

                
Programa
.
panelarbol
.
addListPaint
(
Programa
.
lista
);

            
}
  
        
}

    
}

inteligencia1/Nodo.java
inteligencia1/Nodo.java
package
 inteligencia1
;

import
 java
.
util
.
LinkedList
;

import
 javax
.
swing
.
JOptionPane
;

public
 
class
 
Nodo
 
{

    
private
 
String
 name
;

    
private
 
int
 xPos
;

    
private
 
int
 yPos
;

    
private
 
int
 ancho
;

    
private
 
int
 alto
;

    
private
 
double
 valor
;

   
    
private
 
LinkedList
< Nodo >
 
NLista
 
=
new
 
LinkedList
();

   
    
public
 
boolean
 visitado
=
false
;

    
    
public
 
Nodo
()

    
{

        name
=
“”
;

        ancho
=
40
;

        alto
=
40
;

        valor 
=
0
;

        
    
}

    
    
public
 
Nodo
(
String
 name
){

        
this
.
name
=
name
;

    
}

    
public
 
Nodo
(
String
 name
,
 
int
 xPos
,
 
int
  yPos
){

        
this
.
name
=
name
;

        
this
.
xPos
=
xPos
;

        
this
.
yPos
=
yPos
;

    
}

    
     
public
 
Nodo
(
String
 name
,
 
int
 xPos
,
 
int
  yPos
,
 
int
 ancho
,
 
int
 alto
){

        
this
.
name
=
name
;

        
this
.
xPos
=
xPos
;

        
this
.
yPos
=
yPos
;

        
this
.
ancho
=
ancho
;

        
this
.
alto
=
alto
;

    
}

    
    
public
 
void
 setName
(
String
 name
){

        
this
.
name
=
name
;

    
}
 
    
public
 
String
 getName
(){

        
return
 
this
.
name
;

    
}

    
    
public
 
void
 setXpos
(
int
 xPos
){

        
this
.
xPos
=
xPos
;

    
}

    
public
 
int
 getXpos
(){

        
return
 
this
.
xPos
;

    
}

    
    
public
 
void
 setYpos
(
int
 yPos
){

        
this
.
yPos
=
yPos
;

    
}

    
public
 
int
 getYpos
(){

        
return
 
this
.
yPos
;

    
}

    
    
public
 
void
 setAncho
(
int
 ancho
){

        
this
.
ancho
=
ancho
;

    
}

    
public
 
int
 getAncho
(){

        
return
 
this
.
ancho
;

    
}

    
    
public
 
void
 setAlto
(
int
 alto
){

        
this
.
alto
=
alto
;

    
}

    
public
 
int
 getAlto
(){

        
return
 
this
.
alto
;

    
}

    
    
public
 
void
 setValor
(
double
 valor
){

        
this
.
valor
=
valor
;

    
}

    
public
 
double
  getValor
(){

        
return
 
this
.
valor
;

    
}

    
    
    
//———–Nodo agregar a la lista
    
public
 
void
 addNodoList
(
Nodo
 nodo
){

       
        
try
{

        
//NLista.push(nodo);
            
NLista
.
add
(
nodo
);

       
}
catch
(
java
.
lang
.
NullPointerException
 e
){

           
JOptionPane
.
showMessageDialog
(
null
,
“Error 1 : ”
+
e
.
toString
());

       
}
catch
(
java
.
lang
.
Exception
 e
){

             
JOptionPane
.
showMessageDialog
(
null
,
 
“Error 2 : ”
+
e
.
toString
());

           
       
}

       
    
}

    
/// Remover un Nodo de la lista 
    
public
 
void
 remNodoList
(
Nodo
 row
){

        
NLista
.
remove
(
row
);

    
}

    
// Optener un nodo de la lista  por un index 
    
public
 
Nodo
 getNodoList
(
int
 index
){

        
return
 
NLista
.
get
(
index
);

    
}

    
// Retorna la cantidad de elemento de la lista
    
public
 
int
 getNodoListCount
(){

        
return
 
NLista
.
size
();

    
}

    
// Esistencia del nodo en la lista
    
public
 
boolean
 isExists
(
Nodo
 e
){

        
return
 
NLista
.
contains
(
e
);

    
}

    
// Retorna lista del nodo
    
public
 
LinkedList
< Nodo >
 getListNodo
(){

        
return
 
NLista
;

    
}

}

inteligencia1/PanelArbol.java
inteligencia1/PanelArbol.java
package
 inteligencia1
;

import
 java
.
awt
.
Color
;

import
 java
.
awt
.
Graphics
;

import
 java
.
awt
.
image
.
BufferedImage
;

import
 java
.
util
.
LinkedList
;

import
 java
.
util
.
logging
.
Level
;

import
 java
.
util
.
logging
.
Logger
;

import
 javax
.
swing
.
JComponent
;

public
 
class
 
PanelArbol
 
extends
 
JComponent
 
{

    
BufferedImage
 imagen
;

    
LinkedList
 
< Nodo >
 lista
;

    
int
 x
,
y
;

    
int
 ancho
,
alto
;

    
boolean
 bfs
,
dfs
;

    
boolean
 inicio
=
false
;

    
LinkedList
< Nodo >
 cola 
=
 
new
 
LinkedList
();

    
public
 
PanelArbol
(
int
 x
,
 
int
 y
,
 
int
 ancho
,
int
 alto
){

        
this
.
setBounds
(
x
,
 y
,
 ancho
,
 alto
);

        
this
.
ancho
=
ancho
;

        
this
.
alto
=
alto
;

        imagen 
=
 
new
 
BufferedImage
(
getWidth
(),
getHeight
(),
BufferedImage
.
TYPE_3BYTE_BGR
);

        imagen
.
getGraphics
().
setColor
(
Color
.
blue
);

        imagen
.
getGraphics
().
fillRect
(
0
,
 
0
,
 ancho
,
 alto
);

        bfs
=
false
;

        dfs
=
false
;

        
    
}

  
    
public
 
void
 BFS
(){

        bfs
=
true
;

    
}

    
public
 
void
 DFS
(){

        dfs
=
true
;

        repaint
();

    
}

    
public
 
void
 addListPaint
(
LinkedList
 
< Nodo >
 lista
){

        
this
.
lista
=
null
;

        
this
.
lista
=
lista
;

        repaint
();

    
}

   
    
    
    
   @
Override

   
public
 
void
 paint 
(
Graphics
 g
){
 
        x
=
y
=
0
;

        x
=
17
;

        y
=
14
;

        
int
 xfle
,
yfle
;

        g
.
drawImage
(
imagen
,
0
,
0
,
this
);

        g
.
setColor
(
 
Color
.
LIGHT_GRAY 
);

        g
.
fillRect
(
0
,
 
0
,
  ancho
,
alto 
);
  
        
        
if
(
lista
!=
null
){

            
//Dibujar los nodos
            
for
 
(
Nodo
 nn 
:
 lista
){

                g
.
setColor
(
 
Color
.
WHITE 
);

                g
.
fillOval
(
nn
.
getXpos
(),
 nn
.
getYpos
(),
 nn
.
getAncho
(),
 nn
.
getAlto
());

                g
.
setColor
(
 
Color
.
BLACK
);

                g
.
drawString
(
nn
.
getName
(),
nn
.
getXpos
()
+
10
,
 nn
.
getYpos
()
+
15
);

            
}

            
//Lineas
            
for
 
(
Nodo
 raiz 
:
  lista
){

                
if
 
(
raiz
.
getNodoListCount
()
>
0
){

                    
for
 
(
Nodo
 hijo 
:
 raiz
.
getListNodo
()){

                        g
.
setColor
(
 
Color
.
WHITE 
);

                        
if
 
(
raiz
.
getXpos
()
==
hijo
.
getXpos
()
 
&&
 raiz
.
getYpos
()
==
hijo
.
getYpos
())

                        
{

                            g
.
drawOval
(
raiz
.
getXpos
()

10
,
 raiz
.
getYpos
()

10
,
 
30
,
 
30
);

                        
}
else
{

                            g
.
setColor
(
 
Color
.
blue
);

                            
if
 
(
raiz
.
isExists
(
hijo
)
 
&&
 hijo
.
isExists
(
raiz
)){

                                g
.
setColor
(
 
Color
.
RED 
);

                                g
.
drawLine
(
raiz
.
getXpos
()
+
x
,
 raiz
.
getYpos
()
+
y
,
 hijo
.
getXpos
()
+
x
,
hijo
.
getYpos
()
+
y
);

                            
}
else
 
{

                                xfle
=
((
int
)(
hijo
.
getXpos
()
+
raiz
.
getXpos
())
/
2
);

                                yfle
=
((
int
)
 
(
hijo
.
getYpos
()
 
+
 raiz
.
getYpos
())
 
/
 
2
);

                                xfle
=
(
int
)(
raiz
.
getXpos
()
+
xfle
)
/
2
;

                                yfle
=
(
int
)(
raiz
.
getYpos
()
+
yfle
)
/
2
;

                                g
.
drawLine
(
raiz
.
getXpos
()
+
x
,
 raiz
.
getYpos
()
+
y
,
 hijo
.
getXpos
()
+
x
,
hijo
.
getYpos
()
+
y
);

                                g
.
drawString
(
“♦”
,
x
+
xfle
,
y
+
yfle
);

                            
}

                            
                        
}

                        
                    
}

                
}

            
}

            inicio
=
true
;

            cola
.
add
(
lista
.
getFirst
());

            
//Recorrido BFS
            
if
 
(
bfs
)
 
{

                
while
 
(
!
cola
.
isEmpty
()){

                    
Nodo
 nodoactual
=
cola
.
poll
();

                    
//Marcado visitado
                    
if
 
(
!
nodoactual
.
visitado
){

                        g
.
setColor
(
 
Color
.
DARK_GRAY 
);

                        g
.
fillOval
(
nodoactual
.
getXpos
(),
 nodoactual
.
getYpos
(),
 nodoactual
.
getAncho
(),
 nodoactual
.
getAlto
());

                        g
.
setColor
(
 
Color
.
BLACK
);

                        g
.
drawString
(
nodoactual
.
getName
(),
nodoactual
.
getXpos
()
+
10
,
 nodoactual
.
getYpos
()
+
15
);

                        nodoactual
.
visitado
=
true
;

                    
}

                    
try
 
{

                        
Thread
.
sleep
(
1000
);

                    
}
 
catch
 
(
InterruptedException
 ex
)
 
{

                        
Logger
.
getLogger
(
PanelArbol
.
class
.
getName
()).
log
(
Level
.
SEVERE
,
 
null
,
 ex
);

                    
}

                    
for
 
(
Nodo
 hijonodo 
:
 nodoactual
.
getListNodo
()){

                        
if
 
(
!
hijonodo
.
equals
(
nodoactual
.
getNodoList
(
nodoactual
.
getNodoListCount
()

1
))
&&
 
!
hijonodo
.
visitado
){

                            g
.
setColor
(
 
Color
.
DARK_GRAY
);

                            g
.
fillOval
(
hijonodo
.
getXpos
(),
 hijonodo
.
getYpos
(),
 hijonodo
.
getAncho
(),
 hijonodo
.
getAlto
());

                            g
.
setColor
(
 
Color
.
BLACK
);

                            g
.
drawString
(
hijonodo
.
getName
(),
hijonodo
.
getXpos
()
+
10
,
 hijonodo
.
getYpos
()
+
15
);

                            cola
.
add
(
hijonodo
);

                            hijonodo
.
visitado
=
true
;

                        
}

                        
try
 
{

                            
Thread
.
sleep
(
1000
);

                        
}
 
catch
 
(
InterruptedException
 ex
)
 
{

                          
Logger
.
getLogger
(
PanelArbol
.
class
.
getName
()).
log
(
Level
.
SEVERE
,
 
null
,
 ex
);

                        
}

                    
}

                    g
.
setColor
(
 
Color
.
BLACK 
);

                    g
.
fillOval
(
nodoactual
.
getXpos
(),
 nodoactual
.
getYpos
(),
 nodoactual
.
getAncho
(),
 nodoactual
.
getAlto
());

                    g
.
setColor
(
 
Color
.
WHITE
);

                    g
.
drawString
(
nodoactual
.
getName
(),
nodoactual
.
getXpos
()
+
10
,
 nodoactual
.
getYpos
()
+
15
);

                
}

                bfs
=
false
;

            
}

            
//Recorrido DFS
            
       
}

   
}

}

inteligencia1/PanelLista.java
inteligencia1/PanelLista.java
package
 inteligencia1
;

import
 java
.
awt
.
Color
;

import
 java
.
awt
.
Graphics
;

import
 java
.
awt
.
image
.
BufferedImage
;

import
 java
.
util
.
LinkedList
;

import
 javax
.
swing
.
JComponent
;

public
 
class
 
PanelLista
 
extends
 
JComponent
 
{

    
BufferedImage
 imagen
;

    
LinkedList
 
< Nodo >
 lista
;

    
int
 x
,
y
;

    
public
 
PanelLista
(
int
 x
,
 
int
 y
,
 
int
 ancho
,
int
 alto
){

        
this
.
setBounds
(
x
,
 y
,
 ancho
,
 alto
);

        
        imagen 
=
 
new
 
BufferedImage
(
getWidth
(),
getHeight
(),
BufferedImage
.
TYPE_3BYTE_BGR
);

        imagen
.
getGraphics
().
setColor
(
Color
.
blue
);

        imagen
.
getGraphics
().
fillRect
(
0
,
 
0
,
 ancho
,
 alto
);

    
}

    
public
 
void
 addListPaint
(
LinkedList
 
< Nodo >
 lista
){

        
this
.
lista
=
null
;

        
this
.
lista
=
lista
;

        repaint
();

    
}

    @
Override

    
public
 
void
 paint
(
Graphics
 g
){

        
//this.paint(g);
        
int
 i
=
0
,
f
;

        x
=
y
=
0
;

        g
.
setColor
(
 
Color
.
WHITE 
);

        g
.
fillRect
(
0
,
 
0
,
  
400
,
300
 
);
        
        
if
 
(
lista
!=
null
){

           g
.
drawImage
(
imagen
,
0
,
0
,
this
);

           
for
(
Nodo
 nodo
:
 lista
){

               f
=
40
;

               g
.
setColor
(
 
Color
.
BLACK 
);

               g
.
drawRect
(
 x
,
y
,
 
25
,
25
 
);

               g
.
drawString
(
nodo
.
getName
(),
 x
+
12
,
 y
+
12
);

               
if
 
(
nodo
.
getNodoListCount
()
>
0
){

                   
for
 
(
Nodo
 nodolista
:
 nodo
.
getListNodo
()){

                        g
.
setColor
(
 
Color
.
BLUE 
);

                        g
.
drawRect
(
 x
+
f
,
y
,
 
25
,
25
 
);

                        g
.
drawString
(
nodolista
.
getName
(),
 x
+
f
+
12
,
 y
+
12
);

                        g
.
setColor
(
 
Color
.
BLACK
);

                        g
.
drawLine
(
x
+
f

15
,
 y
+
12
,
 x
+
f
,
 y
+
12
);

                        g
.
setColor
(
 
Color
.
RED 
);

                        g
.
drawString
(
“|>”
,
 x
+
f

9
,
 y
+
17
);

                        f
+=
40
;

                    
}

               
}

               y
+=
30
;

           
}

        
}

        
    
}

    
public
 
void
 
Repaint
(){

        repaint
();

    
}

}

inteligencia1/Programa.form

inteligencia1/Programa.java
inteligencia1/Programa.java
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package
 inteligencia1
;

import
 java
.
util
.
LinkedList
;

import
 javax
.
swing
.
DefaultListModel
;

import
 javax
.
swing
.
JOptionPane
;

/**
 *
 * 
@author
 Ismael
 */

public
 
class
 
Programa
 
extends
 javax
.
swing
.
JFrame
 
{

  
    
MyDefaultTableModel
 model
;

    
DefaultListModel
     modellista
=
 
new
 
DefaultListModel
();

    
String
 
[]
 vector
;

    
int
 ini
=
65
;

    
int
 fin
=
90
;

    
int
 limiteSpinner
=
10
;

    
public
 
static
   
LinkedList
< Nodo >
 lista 
=
 
new
 
LinkedList
();

    
public
 
static
 
PanelLista
 panel
;

   
// public static PanelArbol panelarbol;
    
public
 
static
 
Arbol
 panelarbol
;

    
    
public
 
Programa
()
 
{

        initComponents
();

        
Spinner
.
setValue
(
1
);

        panel
=
new
 
PanelLista
(
11
,
 
370
,
 
400
,
300
);

        
//panelarbol= new PanelArbol(450,70,450,500);
        panelarbol
=
new
 
Arbol
();

        panelarbol
.
setDoubleBuffered
(
true
);

        panel
.
setDoubleBuffered
(
true
);

        
this
.
add
(
panel
);

        
this
.
add
(
panelarbol
);

        
    
}

    
    
/**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */

    @
SuppressWarnings
(
“unchecked”
)

    
// //GEN-BEGIN:initComponents
    
private
 
void
 initComponents
()
 
{

        jLabel1 
=
 
new
 javax
.
swing
.
JLabel
();

        
Spinner
 
=
 
new
 javax
.
swing
.
JSpinner
();

        jLabel2 
=
 
new
 javax
.
swing
.
JLabel
();

        jScrollPane1 
=
 
new
 javax
.
swing
.
JScrollPane
();

        tabla 
=
 
new
 javax
.
swing
.
JTable
();

        jScrollPane2 
=
 
new
 javax
.
swing
.
JScrollPane
();

        fila 
=
 
new
 javax
.
swing
.
JList
();

        btnMatriz 
=
 
new
 javax
.
swing
.
JButton
();

        jLabel3 
=
 
new
 javax
.
swing
.
JLabel
();

        jLabel4 
=
 
new
 javax
.
swing
.
JLabel
();

        btnAleatorio 
=
 
new
 javax
.
swing
.
JButton
();

        btnBFS 
=
 
new
 javax
.
swing
.
JButton
();

        setDefaultCloseOperation
(
javax
.
swing
.
WindowConstants
.
EXIT_ON_CLOSE
);

        jLabel1
.
setFont
(
new
 java
.
awt
.
Font
(
“Tahoma”
,
 
1
,
 
12
));
 
// NOI18N
        jLabel1
.
setText
(
“Cantidad de Nodo ”
);

        
Spinner
.
addChangeListener
(
new
 javax
.
swing
.
event
.
ChangeListener
()
 
{

            
public
 
void
 stateChanged
(
javax
.
swing
.
event
.
ChangeEvent
 evt
)
 
{

                
SpinnerStateChanged
(
evt
);

            
}

        
});

        jLabel2
.
setFont
(
new
 java
.
awt
.
Font
(
“Tahoma”
,
 
1
,
 
12
));
 
// NOI18N
        jLabel2
.
setText
(
“Matriz Adyacente”
);

        tabla
.
setModel
(
new
 javax
.
swing
.
table
.
DefaultTableModel
(

            
new
 
Object
 
[][]
 
{

            
},

            
new
 
String
 
[]
 
{

            
}

        
));

        tabla
.
setSelectionMode
(
javax
.
swing
.
ListSelectionModel
.
SINGLE_SELECTION
);

        jScrollPane1
.
setViewportView
(
tabla
);

        tabla
.
getAccessibleContext
().
setAccessibleDescription
(
“”
);

        fila
.
setBackground
(
new
 java
.
awt
.
Color
(
240
,
 
240
,
 
240
));

        jScrollPane2
.
setViewportView
(
fila
);

        btnMatriz
.
setFont
(
new
 java
.
awt
.
Font
(
“Tahoma”
,
 
1
,
 
12
));
 
// NOI18N
        btnMatriz
.
setText
(
“Desplegar Matriz ”
);

        btnMatriz
.
addActionListener
(
new
 java
.
awt
.
event
.
ActionListener
()
 
{

            
public
 
void
 actionPerformed
(
java
.
awt
.
event
.
ActionEvent
 evt
)
 
{

                btnMatrizActionPerformed
(
evt
);

            
}

        
});

        jLabel3
.
setFont
(
new
 java
.
awt
.
Font
(
“Tahoma”
,
 
1
,
 
12
));
 
// NOI18N
        jLabel3
.
setText
(
“Lista Adyacente”
);

        jLabel4
.
setFont
(
new
 java
.
awt
.
Font
(
“Tahoma”
,
 
1
,
 
12
));
 
// NOI18N
        jLabel4
.
setText
(
“Dibujo Nodo”
);

        btnAleatorio
.
setFont
(
new
 java
.
awt
.
Font
(
“Tahoma”
,
 
1
,
 
12
));
 
// NOI18N
        btnAleatorio
.
setText
(
“LLenar Aleatoriamente”
);

        btnAleatorio
.
addActionListener
(
new
 java
.
awt
.
event
.
ActionListener
()
 
{

            
public
 
void
 actionPerformed
(
java
.
awt
.
event
.
ActionEvent
 evt
)
 
{

                btnAleatorioActionPerformed
(
evt
);

            
}

        
});

        btnBFS
.
setFont
(
new
 java
.
awt
.
Font
(
“Tahoma”
,
 
1
,
 
12
));
 
// NOI18N
        btnBFS
.
setText
(
“Navigacion BFS”
);

        btnBFS
.
addActionListener
(
new
 java
.
awt
.
event
.
ActionListener
()
 
{

            
public
 
void
 actionPerformed
(
java
.
awt
.
event
.
ActionEvent
 evt
)
 
{

                btnBFSActionPerformed
(
evt
);

            
}

        
});

        javax
.
swing
.
GroupLayout
 layout 
=
 
new
 javax
.
swing
.
GroupLayout
(
getContentPane
());

        getContentPane
().
setLayout
(
layout
);

        layout
.
setHorizontalGroup
(

            layout
.
createParallelGroup
(
javax
.
swing
.
GroupLayout
.
Alignment
.
LEADING
)

            
.
addGroup
(
layout
.
createSequentialGroup
()

                
.
addContainerGap
()

                
.
addGroup
(
layout
.
createParallelGroup
(
javax
.
swing
.
GroupLayout
.
Alignment
.
LEADING
,
 
false
)

                    
.
addGroup
(
layout
.
createSequentialGroup
()

                        
.
addComponent
(
jScrollPane2
,
 javax
.
swing
.
GroupLayout
.
PREFERRED_SIZE
,
 
40
,
 javax
.
swing
.
GroupLayout
.
PREFERRED_SIZE
)

                        
.
addPreferredGap
(
javax
.
swing
.
LayoutStyle
.
ComponentPlacement
.
RELATED
)

                        
.
addComponent
(
jScrollPane1
,
 javax
.
swing
.
GroupLayout
.
PREFERRED_SIZE
,
 
0
,
 
Short
.
MAX_VALUE
))

                    
.
addComponent
(
jLabel3
)

                    
.
addGroup
(
layout
.
createSequentialGroup
()

                        
.
addGroup
(
layout
.
createParallelGroup
(
javax
.
swing
.
GroupLayout
.
Alignment
.
LEADING
)

                            
.
addGroup
(
layout
.
createSequentialGroup
()

                                
.
addComponent
(
jLabel1
)

                                
.
addPreferredGap
(
javax
.
swing
.
LayoutStyle
.
ComponentPlacement
.
RELATED
)

                                
.
addComponent
(
Spinner
,
 javax
.
swing
.
GroupLayout
.
PREFERRED_SIZE
,
 
48
,
 javax
.
swing
.
GroupLayout
.
PREFERRED_SIZE
))

                            
.
addComponent
(
jLabel2
))

                        
.
addGap
(
18
,
 
18
,
 
18
)

                        
.
addGroup
(
layout
.
createParallelGroup
(
javax
.
swing
.
GroupLayout
.
Alignment
.
LEADING
,
 
false
)

                            
.
addComponent
(
btnAleatorio
,
 javax
.
swing
.
GroupLayout
.
DEFAULT_SIZE
,
 javax
.
swing
.
GroupLayout
.
DEFAULT_SIZE
,
 
Short
.
MAX_VALUE
)

                            
.
addComponent
(
btnMatriz
,
 javax
.
swing
.
GroupLayout
.
DEFAULT_SIZE
,
 javax
.
swing
.
GroupLayout
.
DEFAULT_SIZE
,
 
Short
.
MAX_VALUE
))))

                
.
addGap
(
99
,
 
99
,
 
99
)

                
.
addComponent
(
jLabel4
)

                
.
addGap
(
31
,
 
31
,
 
31
)

                
.
addComponent
(
btnBFS
,
 javax
.
swing
.
GroupLayout
.
PREFERRED_SIZE
,
 
151
,
 javax
.
swing
.
GroupLayout
.
PREFERRED_SIZE
)

                
.
addContainerGap
(
186
,
 
Short
.
MAX_VALUE
))

        
);

        layout
.
setVerticalGroup
(

            layout
.
createParallelGroup
(
javax
.
swing
.
GroupLayout
.
Alignment
.
LEADING
)

            
.
addGroup
(
javax
.
swing
.
GroupLayout
.
Alignment
.
TRAILING
,
 layout
.
createSequentialGroup
()

                
.
addContainerGap
()

                
.
addGroup
(
layout
.
createParallelGroup
(
javax
.
swing
.
GroupLayout
.
Alignment
.
BASELINE
)

                    
.
addComponent
(
jLabel1
)

                    
.
addComponent
(
Spinner
,
 javax
.
swing
.
GroupLayout
.
PREFERRED_SIZE
,
 
23
,
 javax
.
swing
.
GroupLayout
.
PREFERRED_SIZE
)

                    
.
addComponent
(
btnMatriz
)

                    
.
addComponent
(
jLabel4
)

                    
.
addComponent
(
btnBFS
))

                
.
addPreferredGap
(
javax
.
swing
.
LayoutStyle
.
ComponentPlacement
.
UNRELATED
)

                
.
addGroup
(
layout
.
createParallelGroup
(
javax
.
swing
.
GroupLayout
.
Alignment
.
BASELINE
)

                    
.
addComponent
(
jLabel2
)

                    
.
addComponent
(
btnAleatorio
))

                
.
addGap
(
20
,
 
20
,
 
20
)

                
.
addGroup
(
layout
.
createParallelGroup
(
javax
.
swing
.
GroupLayout
.
Alignment
.
LEADING
)

                    
.
addComponent
(
jScrollPane1
,
 javax
.
swing
.
GroupLayout
.
PREFERRED_SIZE
,
 
205
,
 javax
.
swing
.
GroupLayout
.
PREFERRED_SIZE
)

                    
.
addComponent
(
jScrollPane2
,
 javax
.
swing
.
GroupLayout
.
PREFERRED_SIZE
,
 
208
,
 javax
.
swing
.
GroupLayout
.
PREFERRED_SIZE
))

                
.
addPreferredGap
(
javax
.
swing
.
LayoutStyle
.
ComponentPlacement
.
UNRELATED
)

                
.
addComponent
(
jLabel3
)

                
.
addContainerGap
(
351
,
 
Short
.
MAX_VALUE
))

        
);

        pack
();

    
}
// //GEN-END:initComponents
//
    
private
 
void
 btnMatrizActionPerformed
(
java
.
awt
.
event
.
ActionEvent
 evt
)
 
{
//GEN-FIRST:event_btnMatrizActionPerformed
        
// TODO add your handling code here
        
int
 spin
=
Integer
.
parseInt
(
 
String
.
valueOf
(
Spinner
.
getValue
()));

        modellista
=
 
new
 
DefaultListModel
();

        lista 
=
 
new
 
LinkedList
();

        
if
 
(
 spin 
!=
 
0
 
){

            ini
=
65
;

            
int
 cantidad
=
 
(
spin
<= limiteSpinner ) ? ( int ) spin - 1 : 10 ;             vector = new   String   [ cantidad + 1 ];             modellista . clear ();              for ( int  i = 0 ; i < cantidad ; i ++ ){                  vector [ i ] = "" + ( char ) ini ;                  lista . add ( new   Nodo ( vector [ i ]));                  modellista . addElement ( vector [ i ]);                  ini ++ ;              }             lista . add ( new   Nodo ());              if   ( ini == 75 ){                   modellista . addElement ( "" + ( char ) ini );                   lista . getLast (). setName ( "" + ( char ) ini );              }              if   ( tabla . getColumnCount () <= 10 ){                modellista . addElement ( "" + ( char ) ini );                lista . getLast (). setName ( "" + ( char ) ini );              }                          fila . setModel ( modellista );             model = new   MyDefaultTableModel ( vector , vector . length );             tabla . setModel ( model );              AddXYpos ();                       } else {              JOptionPane . showConfirmDialog ( this ,   "La cantidad de nodo no puede ser igual a 0" );          }              } //GEN-LAST:event_btnMatrizActionPerformed                private   void   AddXYpos (){                   int   [][]  posicion = {              { 22 , 240 }, //1              { 43 , 157 }, //2              { 110 , 89 }, //3              { 223 , 61 }, //4              { 326 , 110 }, //5              { 381 , 212 }, //6              { 364 , 317 }, //8              { 282 , 399 }, //7              { 168 , 412 }, //9              { 60 , 350 }    //10          };                   for ( int  i = 0 ; i < lista . size (); i ++ ){             lista . get ( i ). setXpos ( posicion [ i ][ 0 ]);             lista . get ( i ). setYpos ( posicion [ i ][ 1 ]);          }                 for   ( Nodo  nn  :  lista ){           nn . setAlto ( 35 );           nn . setAncho ( 35 );          }      }           private   void   SpinnerStateChanged ( javax . swing . event . ChangeEvent  evt )   { //GEN-FIRST:event_SpinnerStateChanged          // TODO add your handling code here:          int  sio = Integer . parseInt ( String . valueOf ( Spinner . getValue ()));          if   ( sio < 1 )   {              Spinner . setValue ( 1 );          }          else   if   ( sio >
this
.
limiteSpinner
)
 
{

            
Spinner
.
setValue
(
this
.
limiteSpinner
);

        
}

    
}
//GEN-LAST:event_SpinnerStateChanged

    
private
 
void
 btnAleatorioActionPerformed
(
java
.
awt
.
event
.
ActionEvent
 evt
)
 
{
//GEN-FIRST:event_btnAleatorioActionPerformed
        
// TODO add your handling code here:
            
for
 
(
int
 c
=
0
;
c
< tabla . getColumnCount (); c ++ )   {                  for ( int  f = 0 ; f < tabla . getRowCount (); f ++ ){                    tabla . setValueAt ( String . valueOf (( Math . random () < 0.5 ) ? 0 : 1 ),  f ,  c );                  }              }      } //GEN-LAST:event_btnAleatorioActionPerformed      private   void  btnBFSActionPerformed ( java . awt . event . ActionEvent  evt )   { //GEN-FIRST:event_btnBFSActionPerformed          // TODO add your handling code here:         panelarbol . BFS ();      } //GEN-LAST:event_btnBFSActionPerformed      //      /**      *  @param  args the command line arguments      */      public   static   void  main ( String  args [])   {          /* Set the Nimbus look and feel */          //
        
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */

        
try
 
{

            
for
 
(
javax
.
swing
.
UIManager
.
LookAndFeelInfo
 info 
:
 javax
.
swing
.
UIManager
.
getInstalledLookAndFeels
())
 
{

                
if
 
(
“Nimbus”
.
equals
(
info
.
getName
()))
 
{

                    javax
.
swing
.
UIManager
.
setLookAndFeel
(
info
.
getClassName
());

                    
break
;

                
}

            
}

        
}
 
catch
 
(
ClassNotFoundException
 ex
)
 
{

            java
.
util
.
logging
.
Logger
.
getLogger
(
Programa
.
class
.
getName
()).
log
(
java
.
util
.
logging
.
Level
.
SEVERE
,
 
null
,
 ex
);

        
}
 
catch
 
(
InstantiationException
 ex
)
 
{

            java
.
util
.
logging
.
Logger
.
getLogger
(
Programa
.
class
.
getName
()).
log
(
java
.
util
.
logging
.
Level
.
SEVERE
,
 
null
,
 ex
);

        
}
 
catch
 
(
IllegalAccessException
 ex
)
 
{

            java
.
util
.
logging
.
Logger
.
getLogger
(
Programa
.
class
.
getName
()).
log
(
java
.
util
.
logging
.
Level
.
SEVERE
,
 
null
,
 ex
);

        
}
 
catch
 
(
javax
.
swing
.
UnsupportedLookAndFeelException
 ex
)
 
{

            java
.
util
.
logging
.
Logger
.
getLogger
(
Programa
.
class
.
getName
()).
log
(
java
.
util
.
logging
.
Level
.
SEVERE
,
 
null
,
 ex
);

        
}

        
//

        
/* Create and display the form */

        java
.
awt
.
EventQueue
.
invokeLater
(
new
 
Runnable
()
 
{

            @
Override

            
public
 
void
 run
()
 
{

                
new
 
Programa
().
setVisible
(
true
);

            
}

        
});

    
}

    
// Variables declaration – do not modify//GEN-BEGIN:variables
    
private
 javax
.
swing
.
JSpinner
 
Spinner
;

    
private
 javax
.
swing
.
JButton
 btnAleatorio
;

    
private
 javax
.
swing
.
JButton
 btnBFS
;

    
private
 javax
.
swing
.
JButton
 btnMatriz
;

    
private
 javax
.
swing
.
JList
 fila
;

    
private
 javax
.
swing
.
JLabel
 jLabel1
;

    
private
 javax
.
swing
.
JLabel
 jLabel2
;

    
private
 javax
.
swing
.
JLabel
 jLabel3
;

    
private
 javax
.
swing
.
JLabel
 jLabel4
;

    
private
 javax
.
swing
.
JScrollPane
 jScrollPane1
;

    
private
 javax
.
swing
.
JScrollPane
 jScrollPane2
;

    
private
 javax
.
swing
.
JTable
 tabla
;

    
// End of variables declaration//GEN-END:variables
}

Still stressed with your coursework?
Get quality coursework help from an expert!