我試圖用掃描儀讀取文件,我認爲我在下面寫的代碼是我需要的代碼,以做我想做的。由於掃描儀或while循環,JFrame沒有顯示?
該puprpose:我可以讀水平,然後我可以讀取邊緣(從kochfractal)和顏色。
當我試圖運行JFrame
時,它運行但未顯示,當我註釋下面的代碼時顯示了該幀。
正在讀取的文件包含(int只發生在文件的頂部1次,然後是3 double和顏色不固定,它取決於int的數量,它們會多少次發生在文件中)
int
double
double
double
color
double
double
double
color
double
double
double
color
double
double
double
color
try {
Scanner fileScanner = new Scanner(new File(filepath+"wwob.txt"));
while(fileScanner.hasNextInt()){
levelInFile = fileScanner.nextInt();
lblLvl.setText("Level: "+levelInFile);
}
while(fileScanner.hasNextLine()){
while(fileScanner.hasNextDouble()){
str1 = str1+fileScanner.nextDouble()+"--";
str2 = str2+fileScanner.nextDouble()+"--";
str3 = str3+fileScanner.nextDouble()+"--";
String color= fileScanner.next().toString();
}
}
lblEdgesBottom.setText(str1);
lblEdgesLeft.setText(str2);
lblEdgesRight.setText(str3);
上請求的額外的代碼:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/*
* KochPanel.java
*
* Created on 2-jul-2011, 10:08:42
*/
package gui;
import java.awt.Graphics;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
import timeutil.TimeStamp;
/**
*
* @author Peter Boots
*/
public class KochPanel extends javax.swing.JPanel{
private timeutil.TimeStamp ts;
private static String filepath =File.separator+"media"+File.separator+"new_disk"+File.separator;
/**
* Creates new form KochPanel
*/
@SuppressWarnings("LeakingThisInConstructor")
public KochPanel() {
initComponents();
}
public void changeLevel(int nxt) {
ts = new TimeStamp();
ts.setBegin("Generate koch edges");
int levelInFile=0;
String str1="";
String str2="";
String str3="";
try {
Scanner fileScanner = new Scanner(new File(filepath+"wwob.txt"));
while(fileScanner.hasNextInt()){
levelInFile = fileScanner.nextInt();
lblLvl.setText("Level: "+levelInFile);
}
while(fileScanner.hasNextLine()){
while(fileScanner.hasNextDouble()){
str1 = str1+fileScanner.nextDouble()+"--";
str2 = str2+fileScanner.nextDouble()+"--";
str3 = str3+fileScanner.nextDouble()+"--";
String color= fileScanner.next().toString();
}
}
lblEdgesBottom.setText(str1);
lblEdgesLeft.setText(str2);
lblEdgesRight.setText(str3);
ts.setEnd("Einde koch edges");
System.out.println(ts.toString());
} catch (FileNotFoundException ex) {
Logger.getLogger(KochPanel.class.getName()).log(Level.SEVERE, null, ex);
}
}
/**
* 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")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
lblLvl = new javax.swing.JLabel();
lblEdgesLeft = new javax.swing.JLabel();
lblEdgesRight = new javax.swing.JLabel();
lblEdgesBottom = new javax.swing.JLabel();
setBackground(new java.awt.Color(0, 0, 0));
setDoubleBuffered(false);
lblLvl.setForeground(new java.awt.Color(255, 0, 0));
lblLvl.setText("jLabel1");
lblEdgesLeft.setForeground(new java.awt.Color(255, 0, 0));
lblEdgesLeft.setText("jLabel2");
lblEdgesRight.setForeground(new java.awt.Color(255, 0, 0));
lblEdgesRight.setText("jLabel3");
lblEdgesBottom.setForeground(new java.awt.Color(255, 0, 0));
lblEdgesBottom.setText("jLabel4");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(lblLvl)
.addComponent(lblEdgesLeft)
.addComponent(lblEdgesRight)
.addComponent(lblEdgesBottom))
.addContainerGap(356, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(lblLvl)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(lblEdgesLeft)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(lblEdgesRight)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(lblEdgesBottom)
.addContainerGap(211, Short.MAX_VALUE))
);
}// </editor-fold>
// Variables declaration - do not modify
private javax.swing.JLabel lblEdgesBottom;
private javax.swing.JLabel lblEdgesLeft;
private javax.swing.JLabel lblEdgesRight;
public javax.swing.JLabel lblLvl;
// End of variables declaration
@Override
public synchronized void paintComponent(Graphics g) {
ts = new TimeStamp();
ts.setBegin("Begin paintComponent");
super.paintComponent(g);
ts.setEnd(" Einde paintComponent");
System.out.println(ts.toString());
System.out.println("Aantal hoekpunten: ");
// lblLvl.setText("Number of edges: ");
// lblEdgesLeft.setText(ts.toString());
}
}
與主CL的代碼屁股吧,這個類我shoudnt被編輯:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/*
* KochFrame.java
*
* Created on 2-jul-2011, 10:00:05
*/
package gui;
import calculate.Edge;
/**
*
* @author Peter Boots
*/
public class KochFrame extends javax.swing.JFrame {
private static int zoomTranslateX = 0;
private static int zoomTranslateY = 0;
private static double zoom = 1;
private int lastDragX = 0;
private int lastDragY = 0;
private int level = 1;
/** Creates new form KochFrame */
public KochFrame() {
initComponents();
level = 1;
resetZoom();
kochPanel.changeLevel(level);
}
/** 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")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
fitFractalBtn = new javax.swing.JButton();
levelSpinner = new javax.swing.JSpinner();
jLabel1 = new javax.swing.JLabel();
kochPanel = new gui.KochPanel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Koch Fractal");
fitFractalBtn.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
fitFractalBtn.setText("Fit Fractal");
fitFractalBtn.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
fitFractalBtnActionPerformed(evt);
}
});
levelSpinner.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
levelSpinner.setModel(new javax.swing.SpinnerNumberModel(Integer.valueOf(1), Integer.valueOf(1), null, Integer.valueOf(1)));
levelSpinner.addChangeListener(new javax.swing.event.ChangeListener() {
public void stateChanged(javax.swing.event.ChangeEvent evt) {
levelSpinnerStateChanged(evt);
}
});
jLabel1.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
jLabel1.setText("Level: ");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(levelSpinner))
.addComponent(fitFractalBtn, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 115, javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(kochPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 419, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(81, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addComponent(kochPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 25, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(levelSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel1))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(fitFractalBtn)
.addContainerGap())
);
pack();
}// </editor-fold>
private void fitFractalBtnActionPerformed(java.awt.event.ActionEvent evt) {
resetZoom();
kochPanel.repaint();
}
private void levelSpinnerStateChanged(javax.swing.event.ChangeEvent evt) {
kochPanel.changeLevel((Integer) (levelSpinner.getValue()));
}
private void kochPanelMouseClicked(java.awt.event.MouseEvent evt) {
double originalPointClickedX = (zoomTranslateX + evt.getX())/zoom;
double originalPointClickedY = (zoomTranslateY + evt.getY())/zoom;
if (evt.getButton() == java.awt.event.MouseEvent.BUTTON1) {
zoom *= 2;
} else if (evt.getButton() == java.awt.event.MouseEvent.BUTTON3) {
zoom /= 2;
}
zoomTranslateX = (int) (originalPointClickedX * zoom - evt.getX());
zoomTranslateY = (int) (originalPointClickedY * zoom - evt.getY());
kochPanel.repaint();
}
private void kochPanelMouseDragged(java.awt.event.MouseEvent evt) {
zoomTranslateX = zoomTranslateX + lastDragX - evt.getX();
zoomTranslateY = zoomTranslateY + lastDragY - evt.getY();
lastDragX = evt.getX();
lastDragY = evt.getY();
kochPanel.repaint();
}
private void kochPanelMousePressed(java.awt.event.MouseEvent evt) {
lastDragX = evt.getX();
lastDragY = evt.getY();
}
private void resetZoom() {
int kpWidth = kochPanel.getWidth();
int kpHeight = kochPanel.getHeight();
int kpSize = Math.min(kpWidth, kpHeight);
zoom = kpSize;
zoomTranslateX = -(kpWidth - kpSize)/2;
zoomTranslateY = -(kpHeight - kpSize)/2;
}
static public Edge edgeAfterZoomAndDrag(Edge e) {
return new Edge(
e.X1 * zoom - zoomTranslateX,
e.Y1 * zoom - zoomTranslateY,
e.X2 * zoom - zoomTranslateX,
e.Y2 * zoom - zoomTranslateY,
e.color);
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new KochFrame().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton fitFractalBtn;
private javax.swing.JLabel jLabel1;
private gui.KochPanel kochPanel;
private javax.swing.JSpinner levelSpinner;
// End of variables declaration
}
不要阻塞EDT(Event Dispatch Thread) - 當發生這種情況時GUI將「凍結」。而不是調用'Thread.sleep(n)'實現一個Swing'Timer'來重複執行任務,或者一個'SwingWorker'執行長時間運行的任務。有關更多詳細信息,請參見[Swing中的併發](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/)。 –
我用什麼叫sleep()呢? – cc2k
也感謝編輯,我不擅長編寫好的句子。 – cc2k