Kara, naum deu certo… vou postar o codigo inteiro…
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.geom.Line2D;
import java.util.LinkedList;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import Arv_Binaria.Pr_Arvore.no;
public class Pr_LinePanel extends JComponent {
private JFrame t=new JFrame();
private int x1=-100;
private int x2=-100;
private int y1=-100;
private int y2=-100;
private int in=-100;
boolean apaga=false;
public JTextField jtInsere,jtExclui;
public no noImp;
int cont=0;
public Pr_LinePanel() {
super();
jtInsere=new JTextField();
jtInsere.setBounds(800,600,50,20);
this.add(jtInsere);
JLabel jbInsere=new JLabel("Insere");
jbInsere.setBounds(750,600,50,20);
this.add(jbInsere);
jtExclui=new JTextField();
jtExclui.setBounds(890,600,50,20);
this.add(jtExclui);
JLabel jlExclui=new JLabel("Exclui");
jlExclui.setBounds(850,600,50,20);
this.add(jlExclui);
jtInsere.addKeyListener(new KeyListener(){
public void keyPressed(KeyEvent arg0) {
// TODO Auto-generated method stub
}
public void keyReleased(KeyEvent arg0) {
if (arg0.getKeyCode() == KeyEvent.VK_ENTER)
{
Pr_Arvore.Insere(Integer.parseInt(jtInsere.getText())) ;
jtInsere.setText("");
}
}
public void keyTyped(KeyEvent arg0) {
// TODO Auto-generated method stub
}
});
jtExclui.addKeyListener(new KeyListener(){
public void keyPressed(KeyEvent arg0) {
// TODO Auto-generated method stub
}
public void keyReleased(KeyEvent arg0) {
if (arg0.getKeyCode() == KeyEvent.VK_ENTER)
{
limpar();
Pr_Arvore.Remover(Integer.parseInt(jtExclui.getText())) ;
jtExclui.setText("");
}
}
public void keyTyped(KeyEvent arg0) {
// TODO Auto-generated method stub
}
});
}
public void setLinePos(int x1, int y1, int x2, int y2, int in, JFrame j) {
this.x1 = x1;
this.x2 = x2;
this.y1 = y1;
this.y2 = y2;
this.in = in;
this.t=j;
this.setOpaque(true);
apaga=false;
repaint();
}
public void limpar(){
apaga=true;
repaint();
}
protected void paintComponent( Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
if (apaga==true){
g2d.clearRect(0, 0,970,680);
}
Line2D line = new Line2D.Double(x1, y1, x2, y2);
g2d.setStroke(new BasicStroke(3.0f));
g2d.setColor(Color.black);
g2d.fillOval(x2 - 14, y2 - 14, 28, 28);
g2d.draw(line);
g2d.dispose();
JLabel b = null;
b = new JLabel("" + in);
b.setForeground(Color.white);
b.setBounds(x2 - 10, y2 - 14, 28, 28);
this.add(b);
}
}
classe da arvore…
Ta um bagunçada… mas ela faz a inserção no paint no momento q insere algum nó novo… a exclusão faço pela pré-ordem, ignorando o nó a ser excluido…
import javax.swing.JFrame;
public class Pr_Arvore extends JFrame {
private static Pr_LinePanel linePanel = null;
public static JFrame j = null;
public static int x;
public static int y;
public static int xx;
public static int yy;
public static int in;
public static int[] vx = { 240, 120, 60, 30, 15 };
public static int[] vy = { 80, 80, 80, 80, 80 };
static no A = null;
static no NoExc=null;
static int cont=0;
public static class no {
int info;
no esq;
no dir;
int px;
int py;
int pax;
int pay;
int altura;
public no(int x, no e, no d, int xp, int yp, int pax, int pay, int alt) {
this.info = x;
this.esq = e;
this.dir = d;
this.px = xp;
this.py = yp;
this.pax = pax;
this.pay = pay;
this.altura = alt;
}
}
public void Pre_Ordem(no A) {
if (A != null) {
System.out.print(A.info+" - ");
Pre_Ordem(A.esq);
Pre_Ordem(A.dir);
if ((A.dir==null)&&(A.esq==null))
System.out.println(A.info+" F ");
}
}
public void Ordem_central(no A, int cont) {
if (A != null)
{
Ordem_central(A.esq, cont);
cont++;
System.out.print(A.info+" - ");
Ordem_central(A.dir, cont);
}
}
public void Pos_Ordem(no A)
{
if (A != null) {
Pos_Ordem(A.esq);
Pos_Ordem(A.dir);
System.out.print(A.info+" - ");
}
}
public void localiza(){
if (A==null)
System.out.println("Vazio");
else
{
no aux=A;
while ((aux!=null)&&(x!=aux.info))
{
if (x<aux.info)
aux=aux.esq;
else
aux=aux.dir;
}
if (aux==null)
System.out.println("Dados naum Localizado");
else
System.out.println("Dado encontrado, altura "+aux.altura+" contando a partir do 0");
}
}
public void folha(no A) {
if (A != null) {
Pre_Ordem(A.esq);
Pre_Ordem(A.dir);
if ((A.dir==null)&&(A.esq==null))
System.out.println(A.info+" F ");
}
}
public void maior(no A)
{
if (A!=null){
if (A.dir!=null)
maior(A.dir);
else
System.out.println("Maior e:" +A.info);
}
}
public void menor(no A)
{
if (A!=null)
if(A.esq!=null)
menor(A.esq);
else
System.out.println(A.info);
}
public static void Insere(int valor) {
int x = valor;
no temp = new no(x, null, null, 0, 0,0,0,0);
if (A == null) {
A = temp;
A.altura = 0;
A.px = 480;
A.py = 100;
A.pax = 480;
A.pay = 100;
setLinePos(temp);
} else {
no ant = A;
no aux = A;
while ((aux != null) && (x != aux.info) && (aux.altura < 5)) {
if (x < aux.info) {
ant = aux;
aux = aux.esq;
} else if (x > aux.info) {
ant = aux;
aux = aux.dir;
}
}
if (aux == null) {
if (x < ant.info) {
temp.px = ant.px - vx[ant.altura];
temp.py = ant.py + vy[ant.altura];
temp.pax = ant.px;
temp.pay = ant.py;
temp.altura = ant.altura + 1;
ant.esq = temp;
setLinePos(temp);
} else {
temp.px = ant.px + vx[ant.altura];
temp.py = ant.py + vy[ant.altura];
temp.pax = ant.px;
temp.pay = ant.py;
temp.altura = ant.altura + 1;
ant.dir = temp;
setLinePos(temp);
}
} else if (x == aux.info)
System.out.println("Já existe");
else
System.out.println("Ultrapassa o limite da Árvore \n"
+ "Árvore definida para altura 6 camadas");
}
}
public static void inseRemovendo(no NoExc,int valor) {
if (NoExc != null) {
if (NoExc.info!=valor)
Insere(NoExc.info);
inseRemovendo(NoExc.esq, valor);
inseRemovendo(NoExc.dir, valor);
}
}
public static void Remover(int valor) {
NoExc = A;
A=null;
inseRemovendo(NoExc, valor);
}
/* public static void setLinePos(no A) {
linePanel.setLinePos(A);
} */
public static void setLinePos(no A) {
x = A.pax;
y = A.pay;
xx = A.px;
yy = A.py;
in = A.info;
linePanel.setLinePos(x, y, xx, yy, in,j);
}
private static Pr_LinePanel getLinePanel() {
if (linePanel == null)
{
linePanel = new Pr_LinePanel();
}
return linePanel;
}
public Pr_Arvore()
{
j = new JFrame("Árvore Binária"); // chamou uma frame
j.setResizable(false);
j.add(new Pr_LinePanel()); // gerou um container dentro da frame
j.setSize(970, 680);
j.add(getLinePanel());
j.setVisible(true); // exibe a frame
}
public static void main(String[] args) {
Pr_Arvore xxs = new Pr_Arvore();
}
}