BOA TARDE . ALGUÉM SABE ME DIZER O PORQUE AS BARRAS DO GRÁFICO NÃO APARECEM?
CLASSE do Gráfico :
import java.io.File;
import java.io.IOException;
import static java.lang.Double.max;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultListModel;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;
/**
*
-
@author efer_
*/
public class exercicio_forca_iso_bilateral extends javax.swing.JFrame {// Variáveis globais DefaultListModel lm = new DefaultListModel(); serial s = new serial(); ArrayList valores = new ArrayList(); Double maior_valor; Double media=0.5;
//Inserindo os valores de força máxima e média no gráfio
ArrayList<Teste_isometria> DadosGrafico= new ArrayList<>(); /* DadosGrafico.add(new Teste_isometria(“Força Máxima”,maior_valor)); DadosGrafico.add(new Teste_isometria(“Força Média”,media));*/
// -------------------------------------------
/**
- Creates new form principal
*/
public exercicio_forca_iso_bilateral() {
initComponents();
}
/**
-
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”) // private void initComponents() {
jScrollPane1 = new javax.swing.JScrollPane(); jList1 = new javax.swing.JList<>(); btnIniciar = new javax.swing.JButton(); jButton3 = new javax.swing.JButton(); btnParar = new javax.swing.JButton(); jLabel1 = new javax.swing.JLabel(); jLabel2 = new javax.swing.JLabel(); jLabel3 = new javax.swing.JLabel(); jLabel4 = new javax.swing.JLabel(); jLabel5 = new javax.swing.JLabel(); jLabel6 = new javax.swing.JLabel(); jLabel7 = new javax.swing.JLabel(); jLabel8 = new javax.swing.JLabel(); jLabel9 = new javax.swing.JLabel(); jLabel11 = new javax.swing.JLabel(); jLabel12 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setResizable(false);jScrollPane1.setViewportView(jList1);btnIniciar.setText(“Iniciar”); btnIniciar.addMouseListener(new java.awt.event.MouseAdapter() { public void mouseClicked(java.awt.event.MouseEvent evt) { btnIniciarMouseClicked(evt); } });
btnParar.setText(“Parar”); btnParar.addMouseListener(new java.awt.event.MouseAdapter() { public void mouseClicked(java.awt.event.MouseEvent evt) { btnPararMouseClicked(evt); } });
jLabel1.setFont(new java.awt.Font(“Tahoma”, 1, 14)); // NOI18N
jLabel1.setText(“Máximo:”);jLabel2.setText(“0”);jLabel3.setText(“Mínimo:”);jLabel4.setText(“0”);jLabel5.setFont(new java.awt.Font(“Tahoma”, 1, 14)); // NOI18N
jLabel5.setText(“Média:”);jLabel6.setText("-");jLabel7.setFont(new java.awt.Font(“Tahoma”, 1, 14)); // NOI18N
jLabel7.setText(“Tempo:”);jLabel8.setText(“00:00”);jLabel9.setFont(new java.awt.Font(“Tahoma”, 0, 14)); // NOI18N jLabel9.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); jLabel9.setText(“Dados:”);
jLabel11.setFont(new java.awt.Font(“Tahoma”, 0, 14)); // NOI18N
jLabel11.setText(“DATA:”);jLabel12.setFont(new java.awt.Font(“Tahoma”, 1, 14)); // NOI18N
jLabel12.setText(“Teste de força Isométrica Máxima - Bilateral”);javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 118, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(30, 30, 30) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(btnIniciar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(btnParar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jLabel1) .addComponent(jLabel3) .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jLabel5) .addComponent(jLabel6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jLabel7) .addComponent(jLabel8, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE))) .addComponent(jLabel11) .addComponent(jLabel9, javax.swing.GroupLayout.PREFERRED_SIZE, 118, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(132, 132, 132) .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 615, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(layout.createSequentialGroup() .addGap(263, 263, 263) .addComponent(jLabel12))) .addContainerGap(51, Short.MAX_VALUE)) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addGap(38, 38, 38) .addComponent(jLabel12) .addGap(51, 51, 51) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(jLabel11) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(jLabel9) .addGap(13, 13, 13) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(btnIniciar) .addGap(8, 8, 8) .addComponent(btnParar) .addGap(31, 31, 31) .addComponent(jLabel7) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jLabel8) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jLabel1) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jLabel2) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jLabel3) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jLabel4) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jLabel5) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jLabel6) .addGap(0, 0, Short.MAX_VALUE)) .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 371, Short.MAX_VALUE))) .addComponent(jButton3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addContainerGap()) );
pack();
}//
Thread t = new Thread(new Runnable() { <a class="mention" href="/u/override">@Override</a> public void run() { char ch = s.lerCOM(); int escolha = JOptionPane.showConfirmDialog(null, “Pronto para começar?”, “Confirmar”, JOptionPane.YES_NO_OPTION); if (escolha != JOptionPane.NO_OPTION) { long inicio = System.currentTimeMillis();
while (true) { // loop infinito !!!! // Cronometro long atual = System.currentTimeMillis(); long tempo = (atual - inicio); jLabel8.setText(new SimpleDateFormat("mm:ss").format(new Date(atual - inicio))); jLabel8.updateUI(); // ---------- boolean continuar = true; String palavra = ""; while (continuar) { ch = s.lerCOM(); System.out.print(ch); if (ch != '\n') { palavra += ch; } else { // Para limpar e só mostrar os valores vamos usar o try ... catch ... try { Double.parseDouble(palavra); // Tenta converter para double, se não der vai para o catch e ignora o valor lm.addElement(palavra); } catch (Exception e) {} continuar = false; } jList1.setModel(lm); jList1.updateUI(); } // Manter o valor máximo encontrado atualizado try { if (Double.parseDouble(palavra) > Double.parseDouble(jLabel2.getText())) { jLabel2.setText(palavra); jLabel2.updateUI(); } } catch (Exception e) { } // Manter o valor mínimo encontrado atualizado try { if (Double.parseDouble(palavra) < Double.parseDouble(jLabel4.getText())) { jLabel4.setText(palavra); jLabel4.updateUI(); } } catch (Exception e) { } } } }
});
private void btnIniciarMouseClicked(java.awt.event.MouseEvent evt) { btnIniciar.setEnabled(false); // Desabilita o botão s.selecionaCOM(“COM3”); lm.removeAllElements(); t.start(); // Inicia a Thread
}
/*public void calculos(){
int j = 0; for (j = 0; j < valores.size(); j++) { if ((double) valores.get(j) > max) { max = (double) valores.get(j); maior_valor = max; // colocando a força maior na váriavel maior_valor } if ((double) valores.get(j) < min) { min = (double) valores.get(j); } soma += (double) valores.get(j); } jLabel2.setText(Double.toString(max)); jLabel4.setText(Double.toString(min)); String num = String.format("%.2f", soma / j); jLabel6.setText(num); media = Double.parseDouble(num);// colocando a média na variável média // -------------------------------------
} */
DefaultCategoryDataset pegaDados() {
DefaultCategoryDataset dataset = new DefaultCategoryDataset(); String t = "0"; for (int i = 0; i < lm.getSize(); i++) { try { dataset.addValue(media, "Media",""); dataset.addValue(maior_valor, "Maior Valor", ""); int temp = Integer.parseInt(t); temp++; t = Integer.toString(temp); valores.add(Double.parseDouble(lm.getElementAt(i).toString())); } catch (Exception e) { } } return dataset;
}
private void btnPararMouseClicked(java.awt.event.MouseEvent evt) {
t.stop(); // Para a Thread
btnIniciar.setEnabled(true); // Habilita o botão
s.fecharCOM(); // Fechar porta COMJFreeChart grafico_Barra = ChartFactory.createBarChart("Isometria", "Forças", "Valor Força", pegaDados(), PlotOrientation.VERTICAL, true,true,false); File fl = new File("C:\\Users\\user\\Documents\\NetBeansProjects\\novoForcaIsometrica\\teste.jpg"); try { ChartUtilities.saveChartAsJPEG(fl, grafico_Barra, jButton3.getWidth(), jButton3.getHeight()); // fl é o arquivo onde vai ser salvo } catch (IOException ex) { Logger.getLogger(exercicio_forca_iso_bilateral.class.getName()).log(Level.SEVERE, null, ex); } // grafico_barras é o gráfico // valor, valor é o tamanho // Coloca o arquivo como imagem no botão Icon i = new ImageIcon("C:\\Users\\user\\Documents\\NetBeansProjects\\novoForcaIsometrica\\teste.jpg"); jButton3.setIcon(i); // ------------------------------------- // Achar maior e menor valor double min = 0, max = 0, soma = 0; int j = 0; for (j = 0; j < valores.size(); j++) { if ((double) valores.get(j) > max) { max = (double) valores.get(j); maior_valor = max; // colocando a força maior na váriavel maior_valor } if ((double) valores.get(j) < min) { min = (double) valores.get(j); } soma += (double) valores.get(j); } jLabel2.setText(Double.toString(max)); jLabel4.setText(Double.toString(min)); String num = String.format("%.2f", soma / j); jLabel6.setText(num); media = Double.parseDouble(num);// colocando a média na variável média // -------------------------------------
}
/**
-
@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(exercicio_forca_iso_bilateral.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(exercicio_forca_iso_bilateral.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(exercicio_forca_iso_bilateral.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(exercicio_forca_iso_bilateral.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//
//
//
//
/* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new exercicio_forca_iso_bilateral().setVisible(true); } }); }
- For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
// Variables declaration - do not modify private javax.swing.JButton btnIniciar; private javax.swing.JButton btnParar; private javax.swing.JButton jButton3; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel11; private javax.swing.JLabel jLabel12; private javax.swing.JLabel jLabel2; private javax.swing.JLabel jLabel3; private javax.swing.JLabel jLabel4; private javax.swing.JLabel jLabel5; private javax.swing.JLabel jLabel6; private javax.swing.JLabel jLabel7; private javax.swing.JLabel jLabel8; private javax.swing.JLabel jLabel9; private javax.swing.JList jList1; private javax.swing.JScrollPane jScrollPane1; // End of variables declaration }
- Creates new form principal