Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Operacje na plikach:
- File file = new File(nazwa_pliku);
- try {
- BufferedReader reader = new BufferedReader(new FileReader(file));
- BufferedWriter writer = new BufferedWriter(new FileWriter(file));
- String line = null;
- while ((line = reader.readLine()) != null) {
- writer.write(line);
- writer.newLine();
- }
- reader.close();
- writer.close();
- } catch (IOException e) {
- System.err.println(e);
- System.exit(1);
- }
- Implementacje dla JPanel
- Runnable, KeyListener, MouseListener, MouseMotionListener, MouseWheelListener
- Implmentacja w JFrame
- this.addKeyListener(JPanel);
- this.addMouseListener(JPanel);
- this.addMouseWheelListener(JPanel);
- this.addMouseMotionListener(JPanel);
- Wątki
- Thread nazwaWatka = new Thread(this);
- nazwaWatka.start();
- JFrame
- super("Kolokwium II");
- this.setSize(400, 600);
- this.setDefaultCloseOperation(EXIT_ON_CLOSE);
- this.setLocationRelativeTo(null);
- this.setResizable(false);
- AppPanel framePanel = new AppPanel();
- this.setContentPane(framePanel);
- this.setVisible(true);
- public class ApiFrame extends JFrame {
- private JButton loadFile;
- private JButton saveFile;
- private JLabel labelInfo;
- private String textInfo;
- private ArrayList <Double> arrayOfNumbers = new ArrayList <Double>();
- public static void main(String[] args) {
- ApiFrame apiFrame = new ApiFrame();
- }
- public ApiFrame() {
- super("Kolokwium");
- this.setDefaultCloseOperation(EXIT_ON_CLOSE);
- this.setLocationRelativeTo(null);
- this.setResizable(false);
- final JPanel apiPanel = new JPanel();
- this.setContentPane(apiPanel);
- apiPanel.setLayout(new BorderLayout());
- loadFile = new JButton("Wczytaj dane");
- saveFile = new JButton("Zapisz dane");
- textInfo = "Nie wczytano danych";
- labelInfo = new JLabel(textInfo,JLabel.CENTER);
- apiPanel.add(labelInfo,BorderLayout.PAGE_START);
- apiPanel.add(loadFile,BorderLayout.WEST);
- apiPanel.add(saveFile,BorderLayout.EAST);
- loadFile.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent arg0) {
- // TODO Auto-generated method stub
- File file = new File("numbers.dat");
- try {
- BufferedReader reader = new BufferedReader(new FileReader(file));
- String line = null;
- while ((line = reader.readLine()) != null) {
- line = line.replace(",", ".");
- arrayOfNumbers.add(Double.parseDouble(line));
- System.out.println(line);
- }
- reader.close();
- labelInfo.setText("Wczytano dane");
- }
- catch (IOException e) {
- System.err.println(e);
- System.exit(1);
- }
- }
- });
- saveFile.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent arg0) {
- // TODO Auto-generated method stub
- File file = new File("max.dat");
- try {
- BufferedWriter writer = new BufferedWriter(new FileWriter(file));
- String line = maxNumber().toString();
- writer.write(line);
- writer.newLine();
- System.out.println(line);
- writer.close();
- }
- catch (IOException e) {
- System.err.println(e);
- System.exit(1);
- }
- labelInfo.setText("Zapisano dane");
- }
- });
- this.pack();
- this.setVisible(true);
- }
- public Double maxNumber() {
- double p = arrayOfNumbers.get(0);
- for (int i = 1; i < arrayOfNumbers.size(); i++) {
- if (arrayOfNumbers.get(i) > p) p = arrayOfNumbers.get(i);
- }
- return (double) p;
- }
- }
- public AppPanel() {
- this.setLayout(new BorderLayout());
- //Top Panel
- JPanel topPanel = new JPanel();
- topPanel.setPreferredSize(new Dimension(this.getWidth(), 50));
- textField = new JTextField(20);
- textField.setText("0");
- textLabel = new JLabel("Długość boku: ");
- textLabel.setLabelFor(textField);
- topPanel.add(textLabel);
- topPanel.add(textField);
- this.add(topPanel,BorderLayout.PAGE_START);
- //Bottom Panel
- JPanel bottomPanel = new JPanel();
- bottomPanel.setPreferredSize(new Dimension(this.getWidth(), 50));
- refreshButton = new JButton("Wczytaj obiekty");
- bottomPanel.add(refreshButton);
- this.add(bottomPanel,BorderLayout.PAGE_END);
- refreshButton.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent arg0) {
- repaint();
- }
- });
- }
- public void paint(Graphics g) {
- super.paint(g);
- if (getLength() > 0) {
- paintRectangle(getLength(), getCountFromFile(), g);
- g.dispose();
- }
- }
- public void paintRectangle(int length, int count, Graphics g) {
- int i, t;
- Dimension d = getSize();
- for (i = 0; i < count; i++) {
- Graphics2D rectangle = (Graphics2D)g;
- rectangle.setColor(Color.BLUE);
- rectangle.drawRect(Math.round(d.width/2-length/2)+i*10, Math.round(d.height/2-length/2)+i*10, length, length);
- }
- }
- public void refreshPaint() {
- this.paintPanel.repaint();
- }
- public int getLength() {
- String length = this.textField.getText();
- return Integer.parseInt(length);
- }
- public int getCountFromFile() {
- String s = null;
- try {
- File file = new File("count.txt");
- FileReader inputFil = new FileReader(file);
- BufferedReader in = new BufferedReader(inputFil);
- s = in.readLine();
- in.close();
- inputFil.close();
- }
- catch(Exception e) {
- }
- if (s != null) {
- return Integer.parseInt(s);
- }
- else {
- return 0;
- }
- }
- }
Add Comment
Please, Sign In to add comment