Rešitev: program Simulator prebivalstva (celotna)

Iz MaFiRaWiki

Naloga: simulator prebivalstva

  1. /****************************************************************/
  2. /* Simulacija5.1 */
  3. /* */
  4. /****************************************************************/
  5.  
  6. import java.io.*;
  7. import java.awt.*;
  8. import java.awt.event.*;
  9. import javax.swing.*;
  10. import javax.swing.filechooser.*;
  11. import java.util.*;
  12. import java.net.URL;
  13.  
  14.  
  15. public class Simulacija5 extends JFrame
  16. {
  17. // Deklaracija globalnih spremenljivk
  18. private JLabel jLabel1;
  19. private JLabel jLabel2;
  20. private JLabel jLabel3;
  21. private JLabel jLabel4;
  22. private JLabel jLabel6;
  23. private JLabel jLabel7;
  24. private JLabel jLabel8;
  25. private JLabel jLabel9;
  26. private JLabel jLabel10;
  27. private JTextField jTextField1;
  28. private JComboBox jComboBox1;
  29. private JComboBox jComboBox2;
  30. private JComboBox jComboBox3;
  31. private JComboBox jComboBox4;
  32. private JComboBox jComboBox5;
  33. private JSlider jSlider1;
  34. private JButton jButton1;
  35. private JButton jButton2;
  36. private JPanel contentPane;
  37. private Plosca drawFrame;
  38. private double prebivalstvoStat;
  39. private int stikalo = 0;
  40. private String trenutnoIme;
  41. private File file;
  42. private boolean Filebool;
  43. private String record;
  44. int prebivalstvo;
  45. int[] tabela;
  46. int prebivalstvoStaro = 0;
  47. int export;
  48. int izvedba;
  49.  
  50.  
  51.  
  52. // konec deklaracije
  53.  
  54. // deklaracija objekta, ki bo risal graf
  55. public class Plosca extends JPanel {
  56.  
  57. int x;
  58. int y;
  59. int x2;
  60. int y2;
  61. int[] gr;
  62. int krat;
  63. int skala;
  64. int skalax;
  65.  
  66. public Plosca(int xx, int yy, int xx2, int yyy) {
  67. x = xx; y = yy;
  68. x2 = xx2; y2 = yyy;
  69.  
  70. }
  71.  
  72. public void nastavi(int[] tab, int kratnik) {
  73. krat = kratnik;
  74. gr = new int[krat];
  75. for (int i = 0; i < kratnik; i++){
  76. gr[i] = tab[i];
  77. }
  78. skala = gr[0]/122;
  79. skalax = 350/krat;
  80.  
  81. }
  82.  
  83. protected void paintComponent(Graphics g) {
  84.  
  85. super.paintComponent(g);
  86. setBackground(Color.WHITE);
  87. g.setColor(Color.BLACK);
  88. int j = 0;
  89. double k;
  90. for(int i = 0; i < krat - 1; i++) {
  91. g.drawLine(j, (((245 - gr[i]/skala) - 122) * 2) + 122, j + 5, (((245 - gr[i+1]/skala) - 122) * 2) + 122);
  92. if (krat == 50) k = 7.1;
  93. else k = 3.5;
  94. j = (int)(j + k);
  95. //if (skalax * i > 350) break;
  96. }
  97.  
  98. g.drawLine(0,122,350,122);
  99. g.drawLine(0,61,350,61);
  100. g.drawLine(0,183,350,183);
  101. }
  102. }
  103.  
  104. // inicializacija okna
  105. public Simulacija5()
  106. {
  107. super();
  108. initializeComponent();
  109.  
  110.  
  111. this.setVisible(true);
  112. }
  113.  
  114.  
  115. // Dolocitev komponent
  116. private void initializeComponent()
  117. {
  118. jLabel1 = new JLabel();
  119. jLabel2 = new JLabel();
  120. jLabel3 = new JLabel();
  121. jLabel4 = new JLabel();
  122. jLabel6 = new JLabel();
  123. jLabel7 = new JLabel();
  124. jLabel8 = new JLabel();
  125. jLabel9 = new JLabel();
  126. jLabel10 = new JLabel();
  127. jTextField1 = new JTextField();
  128. jComboBox1 = new JComboBox();
  129. jComboBox2 = new JComboBox();
  130. jComboBox3 = new JComboBox();
  131. jComboBox4 = new JComboBox();
  132. jComboBox5 = new JComboBox();
  133. jSlider1 = new JSlider();
  134. jButton1 = new JButton();
  135. jButton2 = new JButton();
  136. contentPane = (JPanel)this.getContentPane();
  137. drawFrame = new Plosca(0,0,0,0);
  138. drawFrame.setLayout(null);
  139. drawFrame.setPreferredSize(new java.awt.Dimension(350, 245));
  140. tabela = new int[300];
  141.  
  142. drawFrame.setBackground(Color.RED);
  143. fc = new JFileChooser();
  144.  
  145. jLabel1.setText("Stopnja kriminala");
  146. jLabel2.setText("Tehnoloska razvitost");
  147. jLabel3.setText("Ugodnost okolja");
  148. jLabel4.setText("Naravne katastrofe");
  149. jLabel6.setText("Gostota (0-100)");
  150. jLabel7.setText("Prebivalstvo");
  151. jLabel8.setText("Pritisnite Izracunaj");
  152. //
  153. // Prebivalstvo
  154. //
  155. jTextField1.setText("2000000");
  156. jTextField1.addActionListener(new ActionListener() {
  157. public void actionPerformed(ActionEvent e)
  158. {
  159. jButton1_actionPerformed(e);
  160. }
  161.  
  162. });
  163. //
  164. // Kriminal
  165. //
  166. jComboBox1.addItem("Visoka");
  167. jComboBox1.addItem("Srednja");
  168. jComboBox1.addItem("Nizka");
  169. //
  170. // Tehnologija
  171. //
  172. jComboBox2.addItem("Visoka");
  173. jComboBox2.addItem("Srednja");
  174. jComboBox2.addItem("Nizka");
  175. //
  176. // Okolje
  177. //
  178. jComboBox3.addItem("Ugodno");
  179. jComboBox3.addItem("Nevtralno");
  180. jComboBox3.addItem("Neugodno");
  181. //
  182. // Katastrofe
  183. //
  184. jComboBox4.addItem("Redko");
  185. jComboBox4.addItem("Obicajno");
  186. jComboBox4.addItem("Pogosto");
  187.  
  188. jComboBox5.addItem("1");
  189. jComboBox5.addItem("50");
  190. jComboBox5.addItem("100");
  191. //
  192. // Gostota
  193. //
  194. jSlider1.setValue(50);
  195. //
  196. // Izvedi
  197. //
  198. jButton1.setText("Izracunaj");
  199. jButton1.addActionListener(new ActionListener() {
  200. public void actionPerformed(ActionEvent e)
  201. {
  202. jButton1_actionPerformed(e);
  203. }
  204.  
  205. });
  206.  
  207. jButton2.setText("Odpri");
  208. jButton2.addActionListener(new ActionListener() {
  209. public void actionPerformed(ActionEvent e)
  210. {
  211. jButton2_actionPerformed(e);
  212. }
  213.  
  214. });
  215. //
  216. // Sestava okna - uporabljena je addComponent metoda, ki olajša sestavo okna Swing
  217. //
  218. contentPane.setLayout(null);
  219. addComponent(contentPane, jLabel1, 125,10,110,18);
  220. addComponent(contentPane, jLabel2, 125,50,130,18);
  221. addComponent(contentPane, jLabel3, 125,90,110,18);
  222. addComponent(contentPane, jLabel4, 125,130,130,18);
  223. addComponent(contentPane, jLabel6, 125,170,110,18);
  224. addComponent(contentPane, jLabel7, 125,210,150,18);
  225. addComponent(contentPane, jLabel8, 265,90,150,18);
  226. addComponent(contentPane, jLabel9, 265,130,200,18);
  227. addComponent(contentPane, jLabel10, 265,150,200,18);
  228. addComponent(contentPane, jTextField1, 15,210,100,22);
  229. addComponent(contentPane, jComboBox1, 15,10,100,22);
  230. addComponent(contentPane, jComboBox2, 15,50,100,22);
  231. addComponent(contentPane, jComboBox3, 15,90,100,22);
  232. addComponent(contentPane, jComboBox4, 15,130,100,22);
  233. addComponent(contentPane, jComboBox5, 265,210,100,22);
  234. addComponent(contentPane, jSlider1, 15,170,100,28);
  235. addComponent(contentPane, jButton1, 265,10,110,28);
  236. addComponent(contentPane, jButton2, 265,50,110,28);
  237. addComponent(contentPane, drawFrame, 400,10,352,247);
  238. //
  239. // Simulacija4.1
  240. //
  241. this.setTitle("Simulacija5.1");
  242. this.setLocation(new Point(18, 19));
  243. this.setSize(new Dimension(800, 300));
  244. drawFrame.setBorder(BorderFactory.createCompoundBorder(
  245. BorderFactory.createLoweredBevelBorder(),
  246. BorderFactory.createEmptyBorder(5,5,5,5)));
  247.  
  248. }
  249.  
  250. //addComponent metoda, ki olajša stvaritev okna
  251. private void addComponent(Container container,Component c,int x,int y,int width,int height)
  252. {
  253. c.setBounds(x,y,width,height);
  254. container.add(c);
  255. }
  256.  
  257.  
  258. private void jTextField1_actionPerformed(ActionEvent e)
  259. {
  260. System.out.println("\njTextField1_actionPerformed(ActionEvent e) called.");
  261.  
  262.  
  263. }
  264.  
  265.  
  266. // Dogodek ob pritisku
  267.  
  268. private void jButton1_actionPerformed(ActionEvent e)
  269. {
  270.  
  271. izvedba = Integer.parseInt((String)jComboBox5.getSelectedItem());
  272. //export = izvedbe;
  273. for (int i = 0; i < izvedba; i++){
  274. System.out.println("\njButton1_actionPerformed(ActionEvent e) called.");
  275. // Izvedba ob pritisku
  276. prebivalstvo = (int)Double.parseDouble(jTextField1.getText());
  277.  
  278. String crime = (String)jComboBox1.getSelectedItem();
  279. String tech = (String)jComboBox2.getSelectedItem();
  280. String okolje = (String)jComboBox3.getSelectedItem();
  281. String katastrofe = (String)jComboBox4.getSelectedItem();
  282. int gostota = jSlider1.getValue();
  283. double tech2;
  284. double crime2;
  285. double okolje2;
  286. double katastrofe2;
  287.  
  288. if (stikalo == 0){
  289. prebivalstvoStat = prebivalstvo;
  290. }
  291. if (katastrofe.equalsIgnoreCase( "pogosto" )) katastrofe2 = 1;
  292. else if (katastrofe.equalsIgnoreCase( "obicajno" )) katastrofe2 = 0;
  293. else katastrofe2 = -1;
  294.  
  295. if (okolje.equalsIgnoreCase( "ugodno" )) okolje2 = 1;
  296. else if (katastrofe.equalsIgnoreCase( "nevtralno" )) okolje2 = 0;
  297. else okolje2 = -1;
  298.  
  299. if (crime.equalsIgnoreCase( "visoka" )) crime2 = 1;
  300. else if (crime.equalsIgnoreCase( "srednjao" )) crime2 = 0;
  301. else crime2 = -1;
  302.  
  303. if (tech.equalsIgnoreCase( "visoka" )) tech2 = 1;
  304. else if (tech.equalsIgnoreCase( "srednja" )) tech2 = 0;
  305. else tech2 = -1;
  306.  
  307. double rodnost = 5.00/1000 * (100.00 + okolje2)/100.00 * (100 + tech2)/100.00 + 5.00/1000.00 * (50 - gostota);
  308. double smrtnost = 5.00/1000 * (100.00 + katastrofe2)/100.00 * (100.00 + crime2)/100.00 + 3.00/1000.00 * (-50 + gostota);
  309.  
  310. prebivalstvo = (int)(prebivalstvo * (100 + rodnost - smrtnost)/100.00);
  311. jLabel8.setText("Prebivalstvo: " + prebivalstvo);
  312. gostota = (int)(gostota * prebivalstvo/prebivalstvoStat);
  313. jSlider1.setValue(gostota);
  314. jLabel9.setText("Naravni prirastek je: ");
  315. jLabel10.setText(String.format("%.2f", ((rodnost - smrtnost)*1000)) + " promila");
  316. jTextField1.setText(String.valueOf(prebivalstvo));
  317. tabela[stikalo] = (int)(prebivalstvo);
  318. stikalo ++;
  319.  
  320. }
  321. drawFrame.nastavi(tabela, izvedba);
  322. drawFrame.validate();
  323. drawFrame.removeAll();
  324. drawFrame.repaint();
  325. stikalo = 0;
  326.  
  327.  
  328.  
  329. }
  330.  
  331. // File manager (branje iz datoteke)
  332.  
  333. private void jButton2_actionPerformed(ActionEvent e)
  334. {
  335.  
  336. int returnVal = fc.showOpenDialog(Simulacija5.this);
  337.  
  338. if (returnVal == JFileChooser.APPROVE_OPTION) {
  339. try {
  340. file = fc.getSelectedFile();
  341. BufferedReader inputStream = null;
  342. inputStream = new BufferedReader(new FileReader(file));
  343. String l;
  344. String zapis = "";
  345. while ((l = inputStream.readLine()) != null) {
  346. zapis = zapis + l;
  347. zapis = zapis + "break";
  348. }
  349. String [] temp = null;
  350. temp = zapis.split("break");
  351.  
  352. int gostota = Integer.parseInt(temp[4]);
  353. double tech2 = Double.parseDouble(temp[1]);
  354. double crime2 = Double.parseDouble(temp[0]);
  355. double okolje2 = Double.parseDouble(temp[2]);
  356. double katastrofe2 = Double.parseDouble(temp[3]);
  357. int prebivalstvo = Integer.parseInt(temp[5]);
  358.  
  359.  
  360. double rodnost = 5.00/1000 * (100 + okolje2)/100 * (100 + tech2)/100 + 5.00/1000 * (50 - gostota);
  361. double smrtnost = 5.00/1000 * (100 + katastrofe2)/100 * (100 + crime2)/100 + 3.00/1000 * (-50 + gostota);
  362.  
  363. prebivalstvo = (int)(prebivalstvo * (100 + rodnost - smrtnost)/100);
  364. jLabel8.setText("Prebivalstvo: " + prebivalstvo);
  365. gostota = (int)(gostota * prebivalstvo/prebivalstvoStat);
  366. jSlider1.setValue(gostota);
  367. jLabel9.setText("Naravni prirastek je:");
  368. jLabel10.setText(String.format("%.2f", ((rodnost - smrtnost)*1000)) + " promila");
  369. jTextField1.setText(String.valueOf(prebivalstvo));
  370.  
  371. } catch (IOException f) {
  372. f.printStackTrace();
  373. }
  374. }
  375. }
  376.  
  377.  
  378. public static void main(String[] args)
  379. {
  380. JFrame.setDefaultLookAndFeelDecorated(true);
  381. JDialog.setDefaultLookAndFeelDecorated(true);
  382. try
  383. {
  384. UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
  385. }
  386. catch (Exception ex)
  387. {
  388. System.out.println("Failed loading L&F: ");
  389. System.out.println(ex);
  390. }
  391. new Simulacija5();
  392. }
  393. }

Glej tudi

Osebna orodja