2012-07-13 93 views
0

我想模擬一個生產系統。爲了解釋我打算簡要地做些什麼,我將創建一個小組,在這個小組中我將有一些表格來保存數據(針對幾個工作站和工作類型的特性(見下圖))。當我運行它時,這些值應該被存儲以供進一步處理。如何處理TreeMaps?我可以只使用一個TreeMap來存儲多個項目的值嗎?

關於前一個問題,我用樹狀圖來存儲這些值推薦,所以我創建類似:

Station[num][type][avg_time][posx][posy][state] 
Part[num][type][state] 

繼承人是到目前爲止我的代碼:

L.java

import java.awt.*; 
import javax.swing.*; 


public class L extends JFrame { 

    public static final int ww = 1000; 
    public static final int wh = 600; 
    public static final int bgw = (ww - 30); 
    public static final int bgh = (wh - 80); 
    public static final String wt = "Teste"; 
    Color new_piece = new Color(255,0,0); 
    Color progress_piece = new Color(255,215,0); 
    Color ready_piece = new Color(173,255,47); 
    Container pane = getContentPane(); 
    Dimension appletSize = pane.getSize(); 
    int wHeight = appletSize.height; 
    int wWidth = appletSize.width; 
    DrawRectangle rectangle = new DrawRectangle(); 
    public TMap t; 

    public L() { 
     setSize(ww,wh); 
     this.setTitle(wt); 
//  Sim(int nparts); 
     JButton startButton = new JButton("Start");   
     JButton stopButton = new JButton("Stop"); 

     //... Add Listeners 
     // startButton.addActionListener(new StartAction()); 
     //stopButton.addActionListener(new StopAction()); 

     //... Layout inner panel with two buttons horizontally 
     JPanel buttonPanel = new JPanel(); 
     buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT,10,10)); 
     buttonPanel.add(startButton); 
     buttonPanel.add(stopButton); 
     this.setLayout(new BoxLayout(pane,BoxLayout.Y_AXIS)); 
     this.add(rectangle); 
     this.add(buttonPanel); 

     //Sim(); 
     t = new TMap(); 
     test(); 
     //pane.add(rectangle); 

     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setResizable(false); 
     setLocationRelativeTo(null); 
     setVisible(true); 
    } 

    public void addRectangle(int px, int py, int pw, int ph, Color pc, String state) { 
     this.rectangle.addRectangle(px, py, pw, ph, pc, state); 
    } 

    public void Sim() { 
     addRectangle(20,20,10,10,Color.green,"new"); 
     /*for (int i=0;i<=nparts;i++) { 
      addRectangle(200,200,50,Color.green); 
     }*/ 
    } 

    public void test() { 
     // First Station Proprieties 
     t.put("num",1); 
     t.put("type",1); 
     t.put("avg_time",5); 
     t.put("posx",100); 
     t.put("posy",20); 
     t.put("state",0); 
     // Second Station Proprieties 
     t.put("num",2); 
     t.put("type",2); 
     t.put("avg_time",7); 
     t.put("posx",200); 
     t.put("posy",20); 
     t.put("state",0); 
     /*System.out.println("Now the tree map Keys: " + t.St.keySet()); 
     System.out.println("Now the tree map contain: " + t.St.values());*/ 
    } 

    public static void main(String[] args) { 

     try { 
       UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); 
     } catch (UnsupportedLookAndFeelException ex) { 
      ex.printStackTrace(); 
     } catch (IllegalAccessException ex) { 
      ex.printStackTrace(); 
     } catch (InstantiationException ex) { 
      ex.printStackTrace(); 
     } catch (ClassNotFoundException ex) { 
      ex.printStackTrace(); 
     } 

     SwingUtilities.invokeLater(new Runnable() { 
      public void run() { 
       L l = new L(); 
       //System.out.println("Entryset: " + t.keySet()); 
       //System.out.println("Entryset: " + t.Station() + "\n"); 
      } 
     }); 

    } 

} 

DrawRectangle.java

import java.awt.*; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.geom.*; 
import java.util.ArrayList; 
import java.util.concurrent.TimeUnit; 
import java.util.logging.Level; 
import java.util.logging.Logger; 
import javax.swing.*; 

public class DrawRectangle extends JPanel implements ActionListener { 

private java.util.List<Rectangle2D> squares; 
private java.util.List<Color> colors; 
private long seconds = 1; 
private int anim_interval = (int) TimeUnit.SECONDS.toMillis(seconds); 
private Timer sim_timer; 
private Timer idle_timer; 
int px = 10, velx = 2; 
String state; 
Color pc; 


public DrawRectangle(){ 
    //this.setBounds(10, 10, 10, 10); 
    this.setMinimumSize(new Dimension(100,100)); 
    this.setPreferredSize(new Dimension(100,L.bgh)); 
    this.setMinimumSize(new Dimension(500,L.bgh)); 
    setBackground(Color.gray); 
    setDoubleBuffered(true); 
    setBorder(BorderFactory.createLineBorder(Color.black, 1)); 
    squares = new ArrayList<Rectangle2D>(); 
    colors = new ArrayList<Color>(); 
    sim_timer = new Timer(anim_interval,this);   
    sim_timer.start(); 
} 

public void addRectangle(int px, int py, int pw, int ph, Color pc, String state) { // square 
    squares.add(new Rectangle2D.Double(px, py, pw, ph)) ; 
    pc = pc; 
    //System.out.println(state); 
    //this.a = a; 
    //this.startX = startX; 
    //this.startY = startY; 
} 

public void actionPerformed(ActionEvent e) { 
    /*if (px < 0 || px > 990) { 
     velx = -velx; 
    }*/ 
    //System.out.println(px); 
    if (px == 20) { 
     sim_timer.stop(); 
     state = "idle"; 
     try { 
      Thread.sleep(5000); 
     } catch (InterruptedException ex) { 
      Logger.getLogger(DrawRectangle.class.getName()).log(Level.SEVERE, null, ex); 
     } 
     sim_timer.start(); 
     state = "going"; 
    } 
    else if (px == 50) { 
     sim_timer.stop(); 
     state = "done"; 
     seconds = 2; 
    } 
    //if (state != "idle") { 
     px = px + velx; 
     repaint(); 
    //} 
} 

private void idlestate() throws InterruptedException { 
    Thread.sleep(5000); 
    state = "idle"; 
} 
private void goingstate() { 
    state = "going"; 
} 
private void donestate() { 
    state = "done"; 
} 

@Override 
public void paintComponent(Graphics g) { 
    super.paintComponent(g); 
    Graphics2D g1 = (Graphics2D) g.create(); 
    Graphics2D g2 = (Graphics2D) g.create(); 
    //System.out.println("D1"); 
    /*for(Rectangle2D rect : squares) { 
     System.out.println(colors); 
     //g1.setPaint(colors); 
     g1.fill(rect); 
    }*/ 
    //for(int i=0;i<squares.size();i++) { 
     //System.out.println("D2"); 
     //g1.setColor(colors.get(i)); 
     if (state == "going") { g1.setColor(Color.orange); } 
     else if (state == "idle") { g1.setColor(Color.yellow); } 
     else if (state == "done") { g1.setColor(Color.green); } 
     else { g1.setColor(Color.red); } 
     //g1.fill(squares.get(i)); 
     g1.fillRect(px, 10, 10, 10); 
     g2.setColor(Color.blue); 
     g2.fillRect(px,40,10,10); 
     //g1.dispose(); 
    //} 
} 
public void setColor(Color newColor) { 
    pc = newColor; 
     } 

} 

TMap.java

import java.util.*; 

public class TMap { 
public TreeMap <String, Integer> St; 
public int num_atrib = 6; 

public TMap() { 
    St = new TreeMap <>(); 
} 
public Set<String> getKeySet() { 
    return St.keySet(); 
} 

public Integer get(String s) { 
    return St.get(s); 
} 

public void put (String s, int i) { 
    St.put(s,i); 
    System.out.println("Now the tree map Keys: " + St.keySet()); 
    System.out.println("Now the tree map contain: " + St.values()); 
} 

public TreeMap<String, Integer> Station(String s,int i) { 
} 
} 

的DrawRectangle.java代碼是在這裏只是爲那些誰可能要編譯的代碼,但沒有任何實際問題爲止。 TMap.java是我創建地圖的地方,並且有處理數據的方法。一切工作正常,我的問題是這樣的:

最有可能的,模擬的時候,我將有一個以上的站,所以我需要存儲的信息在這樣一個問題:

Station[num][type][avg_time][posx][posy][state] 
Station[1][1][5][100][20][0] 
Station[2][2][7][200][20][0] 
Station[3][3][4][300][20][0] 

事情是,當我把新的數據到樹形圖,它會覆蓋原有的數據存儲,所以如果我添加信息的兩倍,輸出會到來:

Now the tree map Keys: [avg_time, num, posx, posy, type] 
Now the tree map contain: [5, 1, 100, 20, 1] 
Now the tree map Keys: [avg_time, num, posx, posy, state, type] 
Now the tree map contain: [7, 2, 200, 20, 0, 2] 

模擬將被限制在6個工作站點,所以我的問題是,處理這個問題的最佳做法是什麼?我唯一能想出的就是創建6個TreeMaps,並只使用那些需要的,但我確信必須有一種更簡單,更高效的方式來存儲數據。

在此先感謝您的幫助!

回答

1

我不知道我明白你想達到什麼,但這是地圖工作的方式。它存儲了成對的鍵/值,確保鍵是唯一的。

所以,當你寫:

map.put("abc",100); 
map.put("abc",200); 

地圖只有第二行之後的一個條目,這是關鍵=「ABC」和值= 200

你可能要存儲電臺在一個列表,而不是,每個站地圖鍵/值對持有相關信息:

List<TMap> stations = new ArrayList<TMap>(); 

TMap station1 = new TMap(); 
station1.put("num",1); 
station1.put("type",1); 
... 
stations.add(station1); 

TMap station2 = new TMap(); 
station2.put("num",2); 
station2.put("type",2); 
... 
stations.add(station2); 

PS:我不知道我的理解具有點210類,它真的是一個TreeMap。所以你也可以使用一張地圖清單:

List<Map<String, Integer>> stations = new ArrayList<Map<String, Integer>>(); 
Map<String, Integer> station1 = new TreeMap<String, Integer>(); 
相關問題