我正在尝试模拟生产系统。为了简要解释我打算做什么,我将创建一个面板,其中我将有一些表格来保存值(用于几个工作站的属性和工作类型(见下图))。当我运行它时,应该存储这些值以供进一步处理。
在上一个问题上,我被推荐使用 TreeMaps 来存储这些值,所以我创建了类似的东西:
Station[num][type][avg_time][posx][posy][state]
Part[num][type][state]
到目前为止,这是我的代码:
爪哇
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");
}
});
}
}
绘制矩形.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 个 TreeMap,并且只使用那些需要的,但我很确定必须有一种更简单、更有效的方式来存储数据。
在此先感谢您的帮助!