Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.*;
- import java.awt.event.*;
- import java.io.FileNotFoundException;
- import java.io.PrintWriter;
- import java.awt.*;
- public class Test extends JFrame implements ActionListener{
- public int x_1, x_2, x_3, y_1, y_2, y_3, delta;
- public String pierwszy_x, drugi_x, trzeci_x, pierwszy_y, drugi_y, trzeci_y;
- private JButton przycisk_zapisz, przycisk_zamknij, przycisk_oblicz;
- private JTextField x1, x2, x3, y1, y2, y3, wynikOperacji;
- private JLabel p1, p2, p3;
- @SuppressWarnings("serial")
- public Test()
- {
- setSize(300,200);
- setTitle("Wojnarowski");
- setLayout(null);
- przycisk_zapisz = new JButton("Zapisz"); //tworzę przycisk
- przycisk_zapisz.setBounds(0, 0, 100, 20); //nadaję mu położenie i rozmiary
- add(przycisk_zapisz); // dodaję do panelu
- przycisk_zapisz.addActionListener(this); //
- przycisk_zamknij = new JButton("Zamknij");
- przycisk_zamknij.setBounds(0, 20, 100, 20);
- add(przycisk_zamknij);
- przycisk_zamknij.addActionListener(this);
- przycisk_oblicz = new JButton("Oblicz");
- przycisk_oblicz.setBounds(150, 180, 100, 20);
- add(przycisk_oblicz);
- przycisk_oblicz.addActionListener(this);
- x1 = new JTextField("0");
- x2 = new JTextField("0");
- x3 = new JTextField("0");
- y1 = new JTextField("0");
- y2 = new JTextField("0");
- y3 = new JTextField("0");
- x1.setBounds(120, 50, 50, 20);
- x2.setBounds(270, 50, 50, 20);
- x3.setBounds(420, 50, 50, 20);
- y1.setBounds(190, 50, 50, 20);
- y2.setBounds(340, 50, 50, 20);
- y3.setBounds(490, 50, 50, 20);
- add(x1);
- add(y1);
- add(x2);
- add(y2);
- add(x3);
- add(y3);
- p1= new JLabel(" Punkt 1");
- p2= new JLabel(" Punkt 2");
- p3= new JLabel(" Punkt 3");
- wynikOperacji = new JTextField();
- p1.setBounds(120, 20, 120, 60);
- p1.setVerticalAlignment(JLabel.TOP);
- p1.setBorder(BorderFactory.createLineBorder(Color.black));
- p2.setBounds(270, 20, 120, 60);
- p2.setVerticalAlignment(JLabel.TOP);
- p2.setBorder(BorderFactory.createLineBorder(Color.black));
- p3.setBounds(420, 20, 120, 60);
- p3.setVerticalAlignment(JLabel.TOP);
- p3.setBorder(BorderFactory.createLineBorder(Color.black));
- wynikOperacji.setBounds(150, 200, 300, 20);
- add(p1);
- add(p2);
- add(p3);
- add(wynikOperacji);
- JComponent canvas = new JComponent() {
- public void paintComponent(Graphics g) {
- //ustala kolor markera
- g.setColor(Color.WHITE);
- g.setColor(Color.BLACK);
- g.drawLine(0, 150, 500, 150);//x
- g.drawLine(250, 0, 250, 300);//y
- //strzalka y
- g.drawLine(250, 0, 240, 10);
- g.drawLine(250, 0, 260, 10);
- //strzalka x
- g.drawLine(490, 150, 480, 160);
- g.drawLine(480, 140, 490, 150);
- //podzialka osi Y
- for (int i=10;i<=300;i=i+10) {
- g.drawLine(247, i, 253, i);//2
- if ((i % 50) == 0) {
- g.drawLine(245, i, 255, i);//2
- }
- }
- //podzialka osi X
- for (int i=10;i<=500;i=i+10) {
- g.drawLine(i, 147, i, 153);//x
- if ((i % 50) == 0) {
- g.drawLine(i, 145, i, 155);//x
- }
- }
- g.setColor(Color.RED);
- g.drawOval(25, 5, 5, 5);
- }
- };
- int pkt;
- Component canvas2 = new Component() {
- };
- canvas.setBounds(100, 300, 490, 300);//dorysowuje canva
- add(canvas);
- }
- // wspó³rzedne
- canvas.addMouseListener(new MouseListener() {
- public void mousePressed(MouseEvent me) { }
- public void mouseReleased(MouseEvent me) { }
- public void mouseEntered(MouseEvent me) { }
- public void mouseExited(MouseEvent me) { }
- public void mouseClicked(MouseEvent me) {
- int x = me.getX();
- int y = me.getY();
- // p3.setText("X:" + x + " Y:" + y);
- }
- });
- public int wspolrzedne(int liczba) {
- if(liczba>=0) {
- return 250+liczba;
- }
- return liczba;
- }
- @Override
- public void actionPerformed(ActionEvent e) {
- pierwszy_x= x1.getText();
- drugi_x = x2.getText();
- trzeci_x = x3.getText();
- pierwszy_y= y1.getText();
- drugi_y = y2.getText();
- trzeci_y = y3.getText();
- x_1 = Integer.parseInt(pierwszy_x);
- x_2 = Integer.parseInt(drugi_x);
- x_3 = Integer.parseInt(trzeci_x);
- y_1 = Integer.parseInt(pierwszy_y);
- y_2 = Integer.parseInt(drugi_y);
- y_3 = Integer.parseInt(trzeci_y);
- Object myszka = e.getSource(); //pobieram myszkę (tam gdzie klikam)
- if(myszka == przycisk_zapisz)
- {
- PrintWriter plik;
- try
- {
- plik = new PrintWriter("czysawspolliniowe.txt");
- delta = x_1*y_2 + x_2*y_3 + x_3*y_1 - x_3*y_2 - x_1*y_3 - x_2*y_1;
- if(delta == 0)
- {
- plik.print("(" + x_1 + "," + y_1 + ") (" + x_2 + "," + y_2 + ") (" + x_3 + "," + y_3 + ") Punkty są współliniowe");
- }
- else
- {
- plik.print("(" + x_1 + "," + y_1 + ") (" + x_2 + "," + y_2 + ") (" + x_3 + "," + y_3 + ") Punkty nie są współliniowe");
- }
- plik.close();
- }
- catch (FileNotFoundException e1)
- {
- e1.printStackTrace();
- }
- }
- else if (myszka == przycisk_zamknij)
- {
- dispose(); // komenda zamknięcia okna
- }
- else
- {
- delta = x_1*y_2 + x_2*y_3 + x_3*y_1 - x_3*y_2 - x_1*y_3 - x_2*y_1;
- if(delta == 0)
- {
- wynikOperacji.setText(" Punkty są współliniowe ");
- }
- else
- {
- wynikOperacji.setText(" Punkty nie są współliniowe ");
- }
- }
- }
- public static void main(String[] args)
- {
- Test interfejs = new Test();
- interfejs.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- interfejs.setLocationRelativeTo(null); //ustawia okno na środku ekranu
- interfejs.setVisible(true);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement