Tenho um JFrame com dois JPanel
Um tem só botoes e o outro tem um botão e o desenho de um grafo
O problema é q onde era pra desenhar o grafo (o JPanel), aparece só um quadrado branco mostrando só um pedaço do grafo
Eu tentei usar o setBounds pra esse JPanel, mas não fez diferença.
Tentei usar setSize e tb não adiantou
[code]
package Main;
import Arvore.;
import Grafo.;
import grafo.;
import java.awt.;
import java.awt.event.;
import javax.swing.;
public class Principal extends JFrame implements ActionListener {
private JPanel main;
private JPanel panel;
private JPanel panel2;
private JFrame frame;
private JSeparator separador;
private JLabel image;
private JLabel vert;
private JLabel ori;
private JLabel dest;
private JLabel p;
private JMenuItem sobre;
private JMenuItem sair;
private JTextField campo;
private JTextField campo2;
private JComboBox combo1;
private JComboBox combo2;
private JButton ok1;
private JButton aleat;
private JButton graf;
private JButton arv;
private Grafo grafoComp;
int numV;
boolean preenche = false;
public Principal() {
super("..");
main = new JPanel();
main.setLayout(new BorderLayout());
image = new JLabel(new ImageIcon("rot1.jpg"));
createMenu();
main.add(image);
this.add(main);
this.setSize(800, 535);
this.setLocationRelativeTo(null);
this.setResizable(false);
this.setVisible(true);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
private void createMenu() {
...
// botao grafo completo
graf = new JButton("Grafo Completo");
graf.setBounds(400, 263, 130, 20);
graf.setFont(new java.awt.Font("Comic Sans MS", 4, 13));
graf.addActionListener(this);
main.add(graf);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == graf) {
// cria os botoes do primeiro JPanel
// botao do segundo JPanel
arv = new JButton("Árvore");
arv.addActionListener(this);
frame = new JFrame();
frame.setLayout(new java.awt.BorderLayout());
panel = new JPanel();
panel2 = new JPanel();
// adiciona os botoes no primeiro JPanel
// segundo JPanel
grafoComp.grafoCompleto();
panel2 = new DesenhaGrafo(grafoComp).Desenha();
panel2.setBounds(100, 100, 100, 100);
panel2.add(arv);
frame.add(panel, java.awt.BorderLayout.PAGE_START);
frame.add(panel2, java.awt.BorderLayout.CENTER);
frame.setSize(650, 600);
frame.setLocationRelativeTo(null);
frame.setResizable(false);
frame.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
frame.setVisible(true);
}
if (e.getSource() == ok1) {
// ...
panel2 = new DesenhaGrafo(grafoComp).Desenha(); // atualiza grafo
}
if (e.getSource() == aleat) {
// ...
panel2 = new DesenhaGrafo(grafoComp).Desenha(); // atualiza grafo
}
if (e.getSource() == arv) {
new GeraArvore(grafoComp);
}
}
public static void main(String args[]) {
new Principal();
}
}[/code]
[code]
package Grafo;
import grafo.Grafo;
import java.awt.;
import javax.swing.;
public class DesenhaGrafo {
private JPanel panel;
public DesenhaGrafo(Grafo grafo) {
panel = new JPanel(); // cria nova janela pro grafo
Grafico painelGrafo = new Grafico(grafo); // chama a classe pra desenhar o grafo
panel.add(painelGrafo);
panel.setSize(607, 526);
painelGrafo.setBackground(new java.awt.Color(255, 255, 255));
}
public JPanel Desenha() {
return panel;
}
class Grafico extends JPanel {
private Grafo grafo;
Color Vertice;
Color Aresta;
Color Texto;
public Grafico(Grafo grafo) {
this.grafo = grafo;
this.Vertice = new Color(100, 149, 237);
this.Aresta = new Color(25, 25, 112);
this.Texto = new Color(0, 0, 0);
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g); // painel
Graphics2D g2d = (Graphics2D) g.create();
// com coordenadas polares
int margem = 40;
int r;
if (this.getWidth() < this.getHeight()) {
r = this.getWidth() / 2 - margem;
} else {
r = this.getHeight() / 2 - margem;
}
int xc = this.getWidth() / 2 - 15;
int yc = this.getHeight() / 2 - 15;
double rad = 1.5 * Math.PI;
double incRad = 2 * Math.PI / grafo.getNumV();
int x;
int y;
Point[] ponto = new Point[grafo.getNumV()];
for (int cont = 0; cont < grafo.getNumV(); cont++) {
x = (int) (r * Math.cos(rad) + xc);
y = (int) (r * Math.sin(rad) + yc);
ponto[cont] = new Point(x, y);
rad += incRad;
}
for (int i = 1; i < grafo.getNumV(); i++) {
for (int j = 0; j < i; j++) {
if (grafo.matrizAdj[i][j] != 0) {
g2d.setColor(Aresta);
g2d.setFont(new java.awt.Font("Comic Sans MS", 4, 14));
g2d.setStroke(new BasicStroke(3f)); //3 pixels de largura
g2d.drawLine(ponto[i].x + 30, ponto[i].y + 20, ponto[j].x + 30, ponto[j].y + 20);
int peso = grafo.matrizAdj[i][j];
g2d.drawString(""+peso,
(ponto[i].x + ponto[j].x)/2 + ((int)(Math.random()*20)),
(ponto[i].y + ponto[j].y)/2 + ((int)(Math.random()*20)));
}
}
}
for (int cont = 0; cont < grafo.getNumV(); cont++) {
g2d.setColor(Vertice);
g2d.fillOval(ponto[cont].x, ponto[cont].y, 40, 40);
g2d.setColor(Texto);
g2d.drawString("v[" + (cont) + "]", ponto[cont].x + 10, ponto[cont].y + 25);
}
g2d.dispose();
}
}
}[/code]