Montar um grafo

0 respostas
A

Boa Tarde !

Sei que hoje e domingão e todos devem estar assistindo seus jogos, mas preciso de uma orientação.

Tenho que criar um Grafo, porém na unha e facil fazer esse grafo, porém meu professor quer que eu crie um função onde peça
para eu inserir as posições para o grafo ser gerado automaticamente, e particulamente não tenho ideia de como fazer.

Alguém pode ajudar ?

Exemplo da entrada do arquivo em txt pelo sistema.

Exemplo:

[color=red]Arquivo entrada.txt:[/color]

8
13
50 30
100 30
25 80
75 90
125 80
75 130
25 130
75 20
0 1 1
0 2 3
0 7 3
1 2 4
1 7 3
1 3 2
1 4 3
2 4 2
3 4 1
3 6 3
3 5 5
4 5 2
5 6 3

[size=18][color=blue]CÓDIGO - PINTANDO JANELA[/color][/size]

import java.awt.Color;

import java.awt.Graphics;

import javax.swing.JFrame;

//classe que utiliza herança

class PintandoJanela extends JFrame{

<a href="//construtor.Definindo">//construtor.Definindo</a> a posição e o tamanho da janela.

PintandoJanela(){

this.setBounds(600,500,500,500);

this.setVisible(true);

}

public void paint(Graphics g){

g.setColor(Color.green);

<a href="//g.fillRect">//g.fillRect</a>(40, 40, 50, 200);

<a href="//g.drawRect">//g.drawRect</a>(200, 300, 50, 50);

g.setColor(Color.pink);

//criando 02 vetores com posições x e y.

// int [] ListaPontosx = {200,210,220,230,220,210};

//int [] ListaPontosy = {50,40,40,50,60,60};

//pintando poligono preenchido com os pontos acima

// g.fillPolygon(ListaPontosx, ListaPontosy, 6);

//mudando a cor

g.setColor(Color.green);		

//Linhas

g.drawLine(65,65,200,65);

g.drawLine(65,65,65,200);

g.drawLine(70,190,200,190);

g.drawLine(215,190,215,65);

g.drawLine(360,190,200,190);

g.drawLine(365,300,365,190);

g.drawLine(215,315,350,315);

g.drawLine(215,300,215,190);

g.setColor(Color.blue);

//Bolinhas

g.fillOval(50,50,30,30);

g.fillOval(200,50,30,30);

g.fillOval(50,180,30,30);

g.fillOval(200,180,30,30);

g.fillOval(350,180,30,30);

g.fillOval(200,300,30,30);

g.fillOval(350,300,30,30);

g.setColor(Color.yellow);

//Números

g.drawString(1,60,70);

g.drawString(2,210,70);

g.drawString(3,60,200);

g.drawString(4,210,200);

g.drawString(5, 360, 200);

g.drawString(6, 210, 320);

g.drawString(7, 360, 320);

//Números no meio

g.setColor(Color.red);

g.drawString(1,130,60);

g.drawString(2,50,130);

g.drawString(3,220,130);

g.drawString(4,130,210);

g.drawString(5,290,185);

g.drawString(6,205,255);

g.drawString(7,370,255);

g.drawString(8,290,335);
}
 public static void main(String [] args){
	 PintandoJanela p = new PintandoJanela();
 }

}

[size=12][color=red]CÓDIGO - LENDO ARQUIVO E GRAVANDO[/color][/size]

import java.awt.<em>;

import java.awt.event.</em>;

import javax.swing.<em>;

import <a href="http://java.io">java.io</a>.</em>;
class Exemplo1102 extends JFrame implements ActionListener

{

JLabel L1,L2;

JButton B1,B2,B3;

JTextField T1;

TextArea TA1;

JPanel P1;

FileDialog Fabrir,Fsalvar;
public static void main(String[] args)

{

JFrame Janela = new Exemplo1102();

WindowListener x = new WindowAdapter()

{

public void windowClosing(WindowEvent e)

{

System.exit(0);

}

};

Janela.addWindowListener(x);

Janela.show();

}
Exemplo1102()

{

setTitle(Manipulação de arquivo Texto);

setSize (500,300);

setResizable(false);

getContentPane().setBackground (new Color (150,150,150));

getContentPane().setLayout (new FlowLayout());

L1 = new JLabel(Texto a ser editado:);

L1.setForeground(Color.black);

L2 = new JLabel(Status:);

L2.setForeground(Color.black);

B1 = new JButton(Gravar);

B2 = new JButton(Abrir);

B3 = new JButton(Limpar);

B1.addActionListener(this);

B2.addActionListener(this);

B3.addActionListener(this);

T1 = new JTextField(35);

T1.setForeground(Color.red);

T1.setEditable(false);

P1 = new JPanel();

P1.setLayout (new FlowLayout());

P1.add(L2); P1.add(T1);

TA1 = new TextArea(8,60);

Fabrir = new FileDialog(this,Abrir arquivo,FileDialog.LOAD);

Fsalvar = new FileDialog(this,Salvar arquivo,FileDialog.SAVE);

getContentPane().add(L1);

getContentPane().add(TA1);

getContentPane().add(B1);

getContentPane().add(B2);

getContentPane().add(B3);

getContentPane().add(P1);

}
public void actionPerformed(ActionEvent e)

{

String nome_do_arquivo;

if (e.getSource() == B3) //limpar

{

TA1.setText("");

T1.setText("");

}

if (e.getSource() == B1) //gravar

{

try

{

Fsalvar.show();

if (Fsalvar.getFile()==null) return;

nome_do_arquivo = Fsalvar.getDirectory()+Fsalvar.getFile();

FileWriter out = new FileWriter(nome_do_arquivo);

out.write(TA1.getText());

out.close();

T1.setText(Arquivo gravado com sucesso !);

}

catch(java.io.IOException exc)

{

T1.setText(Erro ao gravar no arquivo !);

}

}

if (e.getSource() == B2) //ler

{

try

{

Fabrir.show();

if (Fabrir.getFile()==null) return;

nome_do_arquivo = Fabrir.getDirectory()+Fabrir.getFile();

FileReader in = new FileReader(nome_do_arquivo);

String S="";

int i = in.read();

while (i!=-1)

{

S = S +(char)i;

i = in.read();

}

TA1.setText(S);

in.close();

T1.setText(Arquivo aberto com sucesso !);

}

catch(java.io.IOException exc)

{

T1.setText(Erro ao abrir o arquivo !);

}

}

}

}
Criado 7 de outubro de 2007
Respostas 0
Participantes 1