Skip to content

Instantly share code, notes, and snippets.

@ad-m
Last active August 29, 2015 14:21
Show Gist options
  • Select an option

  • Save ad-m/45aa8552fc3891c8801f to your computer and use it in GitHub Desktop.

Select an option

Save ad-m/45aa8552fc3891c8801f to your computer and use it in GitHub Desktop.
package lab11;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.JTextField;
class AddAction implements ActionListener {
ArrayList<Double> data;
JTextField field;
public AddAction(JTextField field, ArrayList<Double> data) {
this.data = data;
this.field = field;
}
@Override
public void actionPerformed(ActionEvent e) {
this.data.add(Double.parseDouble(this.field.getText()));
}
}
package lab11;
import java.io.File;
import java.util.ArrayList;
import javax.swing.*;
public class Okno extends JFrame {
/**
*
*/
private static final long serialVersionUID = 3875344705554977030L;
JTextField Yy;
JLabel LY;
JButton dodaj, zapisz, rysuj;
OknoGrafiki graf;
ArrayList<Double> data = new ArrayList<Double>();
public Okno() {
setTitle("Labolatoria 11");
setSize(700, 600);
setLayout(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
LY = new JLabel("Y:");
LY.setBounds(100, 25, 20, 30);
add(LY);
Yy = new JTextField("");
Yy.setBounds(130, 30, 40, 20);
add(Yy);
dodaj = new JButton("Dodaj");
dodaj.setBounds(180, 30, 100, 20);
dodaj.addActionListener(new AddAction(this.Yy, this.data));
add(dodaj);
zapisz = new JButton("Zapisz");
zapisz.setBounds(300, 30, 100, 20);
zapisz.addActionListener(new SaveAction(this.data));
add(zapisz);
rysuj = new JButton("Rysuj");
rysuj.setBounds(420, 30, 100, 20);
// rysuj.addActionListener(this);
add(rysuj);
graf = new OknoGrafiki();
graf.setBounds(20, 60, 655, 500);
// graf.Dr2P();
add(graf);
}
}
package lab11;
import java.awt.*;
import java.awt.geom.Line2D;
import javax.swing.*;
public class OknoGrafiki extends JPanel {
/**
*
*/
private static final long serialVersionUID = -6678211622449395643L;
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
/*setBackground(Color.WHITE);
g.setColor(Color.BLACK);
g.drawLine(20, 10, 20, 470); // os y
// podzialki na osi (opcjonalne, na labkach pewnie nie)
g.drawLine(10, 30, 30, 30);
g.drawLine(10, 50, 30, 50);
g.drawLine(10, 70, 30, 70);
g.drawLine(10, 90, 30, 90);
g.drawLine(10, 110, 30, 110);
g.drawLine(10, 130, 30, 130);
g.drawLine(10, 150, 30, 150);
g.drawLine(10, 170, 30, 170);
g.drawLine(10, 190, 30, 190);
g.drawLine(10, 210, 30, 210);
g.drawLine(10, 230, 30, 230);
g.drawLine(10, 250, 30, 250);
g.drawLine(10, 270, 30, 270);
g.drawLine(10, 290, 30, 290);
g.drawLine(10, 310, 30, 310);
g.drawLine(10, 330, 30, 330);
g.drawLine(10, 350, 30, 350);
g.drawLine(10, 370, 30, 370);
g.drawLine(10, 390, 30, 390);
g.drawLine(10, 410, 30, 410);
g.drawLine(10, 430, 30, 430);
g.drawLine(10, 450, 30, 450);
// strzalka na osi y
g.drawLine(20, 10, 10, 20);
g.drawLine(20, 10, 30, 20);
// os x
g.drawLine(10, 250, 620, 250);
// podzialki osi x (opcjonalne, na labkach pewnie nie)
g.drawLine(40, 240, 40, 260);
g.drawLine(60, 240, 60, 260);
g.drawLine(80, 240, 80, 260);
g.drawLine(100, 240, 100, 260);
g.drawLine(120, 240, 120, 260);
g.drawLine(140, 240, 140, 260);
g.drawLine(160, 240, 160, 260);
g.drawLine(180, 240, 180, 260);
g.drawLine(200, 240, 200, 260);
g.drawLine(220, 240, 220, 260);
g.drawLine(240, 240, 240, 260);
g.drawLine(260, 240, 260, 260);
g.drawLine(280, 240, 280, 260);
g.drawLine(300, 240, 300, 260);
g.drawLine(320, 240, 320, 260);
g.drawLine(340, 240, 340, 260);
g.drawLine(360, 240, 360, 260);
g.drawLine(380, 240, 380, 260);
g.drawLine(400, 240, 400, 260);
g.drawLine(420, 240, 420, 260);
g.drawLine(440, 240, 440, 260);
g.drawLine(460, 240, 460, 260);
g.drawLine(480, 240, 480, 260);
g.drawLine(500, 240, 500, 260);
g.drawLine(520, 240, 520, 260);
g.drawLine(540, 240, 540, 260);
g.drawLine(560, 240, 560, 260);
g.drawLine(580, 240, 580, 260);
g.drawLine(600, 240, 600, 260); */
g2.draw(new Line2D.Double(new Point(300, 400), new Point(20, 250)));
}
}
package lab11;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
class SaveAction implements ActionListener {
ArrayList<Double> data = new ArrayList<Double>();
public SaveAction(ArrayList<Double> data) {
this.data = data;
}
@Override
public void actionPerformed(ActionEvent e) {
JFileChooser fc = new JFileChooser();
if (fc.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
try(FileWriter fw = new FileWriter(fc.getSelectedFile())){
for(Double row: data){
fw.write(row.toString()+"\n");
};
} catch (IOException e1) {
JOptionPane.showMessageDialog(null,
"Bładz podczas pracy na pliku!", "",
JOptionPane.ERROR_MESSAGE);
}
JOptionPane.showMessageDialog(null,
"Zapisywanie przebiogło pomyślnie", "",
JOptionPane.INFORMATION_MESSAGE);
}
this.data.clear();
}
}
package lab11;
public class test {
public static void main(String[] args) {
Okno ob= new Okno();
ob.setVisible(true);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment