Guest User

Untitled

a guest
Apr 22nd, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.39 KB | None | 0 0
  1.  
  2. Operacje na plikach:
  3.  
  4. File file = new File(nazwa_pliku);
  5.  
  6. try {
  7.     BufferedReader reader = new BufferedReader(new FileReader(file));
  8.     BufferedWriter writer = new BufferedWriter(new FileWriter(file));
  9.  
  10.     String line = null;
  11.     while ((line = reader.readLine()) != null) {
  12.         writer.write(line);
  13.         writer.newLine();
  14.     }
  15.  
  16.     reader.close();
  17.     writer.close();
  18. } catch (IOException e) {
  19.     System.err.println(e);
  20.     System.exit(1);
  21. }
  22.  
  23. Implementacje dla JPanel
  24. Runnable, KeyListener, MouseListener, MouseMotionListener, MouseWheelListener
  25.  
  26. Implmentacja w JFrame
  27. this.addKeyListener(JPanel);
  28. this.addMouseListener(JPanel);
  29. this.addMouseWheelListener(JPanel);
  30. this.addMouseMotionListener(JPanel);
  31.  
  32. Wątki
  33. Thread nazwaWatka = new Thread(this);
  34. nazwaWatka.start();
  35.  
  36. JFrame
  37. super("Kolokwium II");
  38. this.setSize(400, 600);
  39. this.setDefaultCloseOperation(EXIT_ON_CLOSE);
  40. this.setLocationRelativeTo(null);
  41. this.setResizable(false);
  42.  
  43. AppPanel framePanel = new AppPanel();
  44. this.setContentPane(framePanel);
  45.  
  46. this.setVisible(true);
  47.  
  48.  
  49. public class ApiFrame extends JFrame {
  50.     private JButton loadFile;
  51.     private JButton saveFile;
  52.     private JLabel labelInfo;
  53.     private String textInfo;
  54.     private ArrayList <Double> arrayOfNumbers = new ArrayList <Double>();
  55.    
  56.     public static void main(String[] args) {
  57.         ApiFrame apiFrame = new ApiFrame();
  58.     }
  59.     public ApiFrame() {
  60.         super("Kolokwium");
  61.  
  62.         this.setDefaultCloseOperation(EXIT_ON_CLOSE);
  63.         this.setLocationRelativeTo(null);
  64.         this.setResizable(false);
  65.        
  66.         final JPanel apiPanel = new JPanel();
  67.         this.setContentPane(apiPanel);
  68.         apiPanel.setLayout(new BorderLayout());
  69.        
  70.         loadFile = new JButton("Wczytaj dane");
  71.         saveFile = new JButton("Zapisz dane");
  72.        
  73.         textInfo = "Nie wczytano danych";
  74.        
  75.         labelInfo = new JLabel(textInfo,JLabel.CENTER);
  76.        
  77.         apiPanel.add(labelInfo,BorderLayout.PAGE_START);
  78.         apiPanel.add(loadFile,BorderLayout.WEST);
  79.         apiPanel.add(saveFile,BorderLayout.EAST);
  80.        
  81.         loadFile.addActionListener(new ActionListener() {
  82.             @Override
  83.             public void actionPerformed(ActionEvent arg0) {
  84.                 // TODO Auto-generated method stub
  85.                 File file = new File("numbers.dat");
  86.                 try {
  87.                     BufferedReader reader = new BufferedReader(new FileReader(file));
  88.  
  89.                     String line = null;
  90.                     while ((line = reader.readLine()) != null) {
  91.                         line = line.replace(",", ".");
  92.                         arrayOfNumbers.add(Double.parseDouble(line));
  93.                         System.out.println(line);
  94.                     }
  95.  
  96.                     reader.close();
  97.                    
  98.                     labelInfo.setText("Wczytano dane");
  99.                    
  100.                 }
  101.                 catch (IOException e) {
  102.                     System.err.println(e);
  103.                     System.exit(1);
  104.                 }
  105.             }
  106.         });
  107.        
  108.         saveFile.addActionListener(new ActionListener() {
  109.             @Override
  110.             public void actionPerformed(ActionEvent arg0) {
  111.                 // TODO Auto-generated method stub
  112.                
  113.                 File file = new File("max.dat");
  114.                
  115.                 try {
  116.                     BufferedWriter writer = new BufferedWriter(new FileWriter(file));
  117.                     String line = maxNumber().toString();
  118.                    
  119.                     writer.write(line);
  120.                     writer.newLine();
  121.                     System.out.println(line);
  122.                     writer.close();
  123.                 }
  124.                 catch (IOException e) {
  125.                     System.err.println(e);
  126.                     System.exit(1);
  127.                 }
  128.                 labelInfo.setText("Zapisano dane");
  129.             }
  130.         });
  131.    
  132.         this.pack();
  133.         this.setVisible(true);
  134.     }
  135.    
  136.     public Double maxNumber() {
  137.         double p = arrayOfNumbers.get(0);
  138.        
  139.         for (int i = 1; i < arrayOfNumbers.size(); i++) {
  140.             if (arrayOfNumbers.get(i) > p) p = arrayOfNumbers.get(i);
  141.         }
  142.        
  143.         return (double) p;
  144.     }
  145. }
  146.  
  147.  
  148. public AppPanel() {
  149.         this.setLayout(new BorderLayout());
  150.         //Top Panel
  151.         JPanel topPanel = new JPanel();
  152.         topPanel.setPreferredSize(new Dimension(this.getWidth(), 50));
  153.        
  154.         textField = new JTextField(20);
  155.         textField.setText("0");
  156.         textLabel = new JLabel("Długość boku: ");
  157.         textLabel.setLabelFor(textField);
  158.        
  159.         topPanel.add(textLabel);
  160.         topPanel.add(textField);
  161.        
  162.         this.add(topPanel,BorderLayout.PAGE_START);
  163.        
  164.         //Bottom Panel
  165.         JPanel bottomPanel = new JPanel();
  166.         bottomPanel.setPreferredSize(new Dimension(this.getWidth(), 50));
  167.        
  168.         refreshButton = new JButton("Wczytaj obiekty");
  169.         bottomPanel.add(refreshButton);
  170.        
  171.         this.add(bottomPanel,BorderLayout.PAGE_END);
  172.        
  173.         refreshButton.addActionListener(new ActionListener() {
  174.             public void actionPerformed(ActionEvent arg0) {
  175.                 repaint();
  176.             }
  177.         });
  178.     }
  179.    
  180.     public void paint(Graphics g) {
  181.         super.paint(g);
  182.        
  183.         if (getLength() > 0) {
  184.             paintRectangle(getLength(), getCountFromFile(), g);
  185.             g.dispose();
  186.         }
  187.     }
  188.    
  189.     public void paintRectangle(int length, int count, Graphics g) {
  190.         int i, t;
  191.        
  192.         Dimension d = getSize();
  193.  
  194.         for (i = 0; i < count; i++) {
  195.              Graphics2D rectangle = (Graphics2D)g;
  196.              rectangle.setColor(Color.BLUE);
  197.              rectangle.drawRect(Math.round(d.width/2-length/2)+i*10, Math.round(d.height/2-length/2)+i*10, length, length);
  198.         }
  199.     }
  200.    
  201.     public void refreshPaint() {
  202.         this.paintPanel.repaint();
  203.     }
  204.    
  205.     public int getLength() {
  206.         String length = this.textField.getText();
  207.         return Integer.parseInt(length);
  208.     }
  209.    
  210.     public int getCountFromFile() {
  211.         String s = null;
  212.         try {
  213.             File file = new File("count.txt");
  214.        
  215.             FileReader inputFil = new FileReader(file);
  216.             BufferedReader in = new BufferedReader(inputFil);
  217.             s = in.readLine();
  218.             in.close();
  219.              inputFil.close();
  220.         }
  221.         catch(Exception e) {
  222.            
  223.         }
  224.        
  225.         if (s != null) {
  226.             return Integer.parseInt(s);
  227.         }
  228.         else {
  229.             return 0;
  230.         }
  231.     }
  232. }
Add Comment
Please, Sign In to add comment