Skip to content

Commit

Permalink
Merge branch 'feature/simulador' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
fjorgemota committed Apr 12, 2015
2 parents bc287c0 + 1144e4f commit 517ca5e
Show file tree
Hide file tree
Showing 44 changed files with 1,180 additions and 465 deletions.
3 changes: 3 additions & 0 deletions src/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Manifest-Version: 1.0
Main-Class: Main

264 changes: 184 additions & 80 deletions src/Main.java
Original file line number Diff line number Diff line change
@@ -1,106 +1,209 @@
import editores.Editor;
import editores.EditorLinha;
import models.Botao;
import views.Modo;
import views.Paleta;
import views.Quadro;
import views.Reproduzivel;

import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.CompoundBorder;
import javax.swing.border.LineBorder;
import javax.swing.filechooser.*;
import javax.swing.filechooser.FileFilter;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.ArrayList;

public class Main implements MouseListener, MouseMotionListener, ActionListener {
static Quadro quadro;
static Paleta paleta;
static Modo modo;
static JFileChooser fc;
static JFrame f;
static JToolBar toolbar;
static Reproduzivel figuraSelecionada;
static final String NEW = "NEW";
static final String SAVE = "SAVE";
static final String OPEN = "OPEN";
static final String EXIT = "EXIT";


public static void main(String[] argv) {
JFrame f = new JFrame();
f = new JFrame();
fc = new JFileChooser();
fc.setMultiSelectionEnabled(false);
fc.setFileFilter(new FileFilter() {
@Override
public boolean accept(File f) {
return f.getName().endsWith(".sim1");
}

@Override
public String getDescription() {
return "Simulador de Circuitor (.sim1)";
}
});
fc.setAcceptAllFileFilterUsed(false);
f.setLayout(new BorderLayout());
quadro = new Quadro();
modo = new Modo();
paleta = new Paleta(quadro);
f.setContentPane(quadro);
paleta = new Paleta(quadro, modo);
JMenuBar barra = new JMenuBar();
f.setJMenuBar(barra);

Main main = new Main();

JMenu guia;
guia = new JMenu("Arquivo");
barra.add(guia);

JMenuItem item;
item = new JMenuItem("Sair");
item.addActionListener(new Main());
guia.add(item);

guia = new JMenu("Paleta");
barra.add(guia);

item = new JMenuItem("Porta AND");
item.setActionCommand(Paleta.AND);
item.addActionListener(paleta);
item = new JMenuItem("Novo");
item.setActionCommand(NEW);
item.addActionListener(main);
guia.add(item);

item = new JMenuItem("Porta OR");
item.setActionCommand(Paleta.OR);
item.addActionListener(paleta);
item = new JMenuItem("Abrir");
item.setActionCommand(OPEN);
item.addActionListener(main);
guia.add(item);

item = new JMenuItem("Porta XOR");
item.setActionCommand(Paleta.XOR);
item.addActionListener(paleta);
item = new JMenuItem("Salvar");
item.setActionCommand(SAVE);
item.addActionListener(main);
guia.add(item);

item = new JMenuItem("Porta NOT");
item.setActionCommand(Paleta.NOT);
item.addActionListener(paleta);
item = new JMenuItem("Sair");
item.setActionCommand(EXIT);
item.addActionListener(main);
guia.add(item);

item = new JMenuItem("Porta NAND");
item.setActionCommand(Paleta.NAND);
item.addActionListener(paleta);
guia.add(item);
toolbar = new JToolBar();
toolbar.setOrientation(JToolBar.VERTICAL);
JButton botao;
f.add(toolbar, BorderLayout.WEST);
f.add(new JScrollPane(
quadro,
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED
), BorderLayout.CENTER, 1);

item = new JMenuItem("Porta NOR");
item.setActionCommand(Paleta.NOR);
item.addActionListener(paleta);
guia.add(item);

item = new JMenuItem("Porta NXOR");
item.setActionCommand(Paleta.NXOR);
item.addActionListener(paleta);
guia.add(item);
botao = new JButton("Porta AND");
botao.setActionCommand(Paleta.AND);
botao.addActionListener(paleta);
toolbar.add(botao);

item = new JMenuItem("Conector");
item.setActionCommand(Paleta.LINHA);
item.addActionListener(paleta);
guia.add(item);
botao = new JButton("Porta OR");
botao.setActionCommand(Paleta.OR);
botao.addActionListener(paleta);
toolbar.add(botao);

guia = new JMenu("Modo");
barra.add(guia);
botao = new JButton("Porta XOR");
botao.setActionCommand(Paleta.XOR);
botao.addActionListener(paleta);
toolbar.add(botao);

item = new JMenuItem("Criar");
item.setActionCommand(Modo.CRIAR);
item.addActionListener(modo);
guia.add(item);
botao = new JButton("Porta NOT");
botao.setActionCommand(Paleta.NOT);
botao.addActionListener(paleta);
toolbar.add(botao);

item = new JMenuItem("Editar");
item.setActionCommand(Modo.EDITAR);
item.addActionListener(modo);
guia.add(item);
botao = new JButton("Porta NAND");
botao.setActionCommand(Paleta.NAND);
botao.addActionListener(paleta);
toolbar.add(botao);

item = new JMenuItem("Apagar");
item.setActionCommand(Modo.APAGAR);
item.addActionListener(modo);
guia.add(item);
botao = new JButton("Porta NOR");
botao.setActionCommand(Paleta.NOR);
botao.addActionListener(paleta);
toolbar.add(botao);

botao = new JButton("Porta NXOR");
botao.setActionCommand(Paleta.NXOR);
botao.addActionListener(paleta);
toolbar.add(botao);

botao = new JButton("Conector");
botao.setActionCommand(Paleta.LINHA);
botao.addActionListener(paleta);
toolbar.add(botao);

botao = new JButton("Botão");
botao.setActionCommand(Paleta.BOTAO);
botao.addActionListener(paleta);
toolbar.add(botao);

f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(800, 600);
f.setTitle("Simulador de Circuitos");
desenhar();
for(Component c: toolbar.getComponents()) {
botao = (JButton) c;
botao.setBackground(null);
}
}

public void actionPerformed(ActionEvent e){
System.exit(0); //sai da aplicação com o código de status zero
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
if (command.equals(EXIT)) {
System.exit(0); //sai da aplicação com o código de status zero
}
else if (command.equals(OPEN)) {
int returnVal = fc.showOpenDialog(f);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
FileInputStream fi;
ObjectInputStream is;
ArrayList<Reproduzivel> q;
try {
fi = new FileInputStream(file);
is = new ObjectInputStream(fi);
q = (ArrayList<Reproduzivel>) is.readObject();
fi.close();
} catch (IOException e1) {
JOptionPane.showMessageDialog(null, "Arquivo inválido!");
return;
} catch(ClassNotFoundException e2) {
e2.printStackTrace();
return;
}
quadro.setFigs(q);
}
} else if(command.equals(SAVE)) {
int returnVal = fc.showSaveDialog(f);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
if(!file.getAbsolutePath().endsWith(".sim1")) {
file = new File(file.getAbsolutePath()+".sim1");
}
FileOutputStream fo;
ObjectOutputStream os;
try {
fo = new FileOutputStream(file);
os = new ObjectOutputStream(fo);
os.writeObject(quadro.getFigs());
os.flush();
fo.close();
} catch(FileNotFoundException e1) {
e1.printStackTrace();
return;
} catch (IOException e2) {
e2.printStackTrace();
return;
}
}

} else if(command.equals(NEW)) {
quadro.getFigs().clear();
quadro.repaint();
}
}

static void desenhar() {
Expand All @@ -110,49 +213,50 @@ static void desenhar() {
}

public void mouseClicked(MouseEvent e) {
int x = e.getX();
int y = e.getY();
if (modo.estaCriando()) {
int x = e.getX();
int y = e.getY();
Editor editor = Main.paleta.editor();
if(editor == null){
JOptionPane.showMessageDialog(null, "Selecione um componente no menu 'Paleta'!");

} else {
editor.clique(x, y);
if (editor != null) {
if (editor.clique(x, y)) {
modo.setaEditando();
for(Component c: toolbar.getComponents()) {
JButton botao = (JButton) c;
botao.setBackground(null);
}
}
}
} else if (modo.estaEditando()) {
figuraSelecionada = Main.quadro.pegaObjetoEm(x, y);
if (figuraSelecionada instanceof Botao) {
figuraSelecionada.calcula();
}
}
}

public void mousePressed(MouseEvent e) {
if (modo.estaEditando()) {
int x = e.getX();
int y = e.getY();
figuraSelecionada = Main.quadro.pegaObjetoEm(x, y);
if(figuraSelecionada == null) {
JOptionPane.showMessageDialog(null, "Selecione um componente e arraste!");
}
else {
figuraSelecionada.selecionaPonto(x, y);
}
if (modo.estaCriando()) {
return;
}
else if(modo.estaApagando()){
int x = e.getX();
int y = e.getY();
figuraSelecionada = Main.quadro.pegaObjetoEm(x, y);
if(figuraSelecionada == null) {
JOptionPane.showMessageDialog(null, "Selecione um componente para apagar");
}
else {
int x = e.getX();
int y = e.getY();
figuraSelecionada = Main.quadro.pegaObjetoEm(x, y);
if (figuraSelecionada != null) {
if(e.getButton() == MouseEvent.BUTTON1) {
figuraSelecionada.selecionaPonto(x, y);
} else {
quadro.removeFig(figuraSelecionada);
quadro.repaint();
}

}
}

public void mouseReleased(MouseEvent e) {
if (modo.estaEditando() && figuraSelecionada != null) {
figuraSelecionada = null;
if (modo.estaCriando()) {
return;
}
quadro.repaint();
}

public void mouseEntered(MouseEvent e) {
Expand Down
2 changes: 1 addition & 1 deletion src/editores/Editor.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package editores;

public interface Editor {
void clique(int x, int y);
boolean clique(int x, int y);

}
20 changes: 11 additions & 9 deletions src/editores/EditorAnd.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
package editores;

import views.*;
import models.*;

import javax.swing.*;
import models.Ponto;
import views.FigAND;
import views.Quadro;

public class EditorAnd extends EditorPortaLogica {
public EditorAnd(Quadro quadro){
public EditorAnd(Quadro quadro) {
super(quadro);
}
public void clique(int x, int y){
}

public boolean clique(int x, int y) {
FigAND and = new FigAND(new Ponto(x, y));
if(this.continuaInsercao(and)){
if (this.continuaInsercao(and)) {
this.quadro.addFig(and);
this.quadro.repaint();
return true;
}
}
return false;
}
}
Loading

0 comments on commit 517ca5e

Please sign in to comment.