Advertisement
Guest User

Untitled

a guest
Jun 18th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.85 KB | None | 0 0
  1. import javax.swing.*;
  2. import java.awt.event.*;
  3. import java.io.FileNotFoundException;
  4. import java.io.PrintWriter;
  5. import java.awt.*;
  6.  
  7.  
  8. public class Test extends JFrame implements ActionListener{
  9.  
  10. public int x_1, x_2, x_3, y_1, y_2, y_3, delta;
  11. public String pierwszy_x, drugi_x, trzeci_x, pierwszy_y, drugi_y, trzeci_y;
  12. private JButton przycisk_zapisz, przycisk_zamknij, przycisk_oblicz;
  13. private JTextField x1, x2, x3, y1, y2, y3, wynikOperacji;
  14. private JLabel p1, p2, p3;
  15. @SuppressWarnings("serial")
  16. public Test()
  17. {
  18. setSize(300,200);
  19. setTitle("Wojnarowski");
  20. setLayout(null);
  21.  
  22. przycisk_zapisz = new JButton("Zapisz"); //tworzę przycisk
  23. przycisk_zapisz.setBounds(0, 0, 100, 20); //nadaję mu położenie i rozmiary
  24. add(przycisk_zapisz); // dodaję do panelu
  25. przycisk_zapisz.addActionListener(this); //
  26.  
  27. przycisk_zamknij = new JButton("Zamknij");
  28. przycisk_zamknij.setBounds(0, 20, 100, 20);
  29. add(przycisk_zamknij);
  30. przycisk_zamknij.addActionListener(this);
  31.  
  32. przycisk_oblicz = new JButton("Oblicz");
  33. przycisk_oblicz.setBounds(150, 180, 100, 20);
  34. add(przycisk_oblicz);
  35. przycisk_oblicz.addActionListener(this);
  36.  
  37. x1 = new JTextField("0");
  38. x2 = new JTextField("0");
  39. x3 = new JTextField("0");
  40.  
  41. y1 = new JTextField("0");
  42. y2 = new JTextField("0");
  43. y3 = new JTextField("0");
  44.  
  45. x1.setBounds(120, 50, 50, 20);
  46. x2.setBounds(270, 50, 50, 20);
  47. x3.setBounds(420, 50, 50, 20);
  48.  
  49. y1.setBounds(190, 50, 50, 20);
  50. y2.setBounds(340, 50, 50, 20);
  51. y3.setBounds(490, 50, 50, 20);
  52.  
  53. add(x1);
  54. add(y1);
  55. add(x2);
  56. add(y2);
  57. add(x3);
  58. add(y3);
  59.  
  60. p1= new JLabel(" Punkt 1");
  61. p2= new JLabel(" Punkt 2");
  62. p3= new JLabel(" Punkt 3");
  63. wynikOperacji = new JTextField();
  64.  
  65. p1.setBounds(120, 20, 120, 60);
  66. p1.setVerticalAlignment(JLabel.TOP);
  67. p1.setBorder(BorderFactory.createLineBorder(Color.black));
  68. p2.setBounds(270, 20, 120, 60);
  69. p2.setVerticalAlignment(JLabel.TOP);
  70. p2.setBorder(BorderFactory.createLineBorder(Color.black));
  71. p3.setBounds(420, 20, 120, 60);
  72. p3.setVerticalAlignment(JLabel.TOP);
  73. p3.setBorder(BorderFactory.createLineBorder(Color.black));
  74. wynikOperacji.setBounds(150, 200, 300, 20);
  75.  
  76. add(p1);
  77. add(p2);
  78. add(p3);
  79. add(wynikOperacji);
  80.  
  81. JComponent canvas = new JComponent() {
  82. public void paintComponent(Graphics g) {
  83. //ustala kolor markera
  84. g.setColor(Color.WHITE);
  85. g.setColor(Color.BLACK);
  86. g.drawLine(0, 150, 500, 150);//x
  87. g.drawLine(250, 0, 250, 300);//y
  88. //strzalka y
  89. g.drawLine(250, 0, 240, 10);
  90. g.drawLine(250, 0, 260, 10);
  91. //strzalka x
  92. g.drawLine(490, 150, 480, 160);
  93. g.drawLine(480, 140, 490, 150);
  94. //podzialka osi Y
  95. for (int i=10;i<=300;i=i+10) {
  96. g.drawLine(247, i, 253, i);//2
  97. if ((i % 50) == 0) {
  98. g.drawLine(245, i, 255, i);//2
  99. }
  100. }
  101. //podzialka osi X
  102. for (int i=10;i<=500;i=i+10) {
  103. g.drawLine(i, 147, i, 153);//x
  104. if ((i % 50) == 0) {
  105. g.drawLine(i, 145, i, 155);//x
  106. }
  107. }
  108.  
  109. g.setColor(Color.RED);
  110. g.drawOval(25, 5, 5, 5);
  111. }
  112. };
  113.  
  114. int pkt;
  115. Component canvas2 = new Component() {
  116. };
  117.  
  118. canvas.setBounds(100, 300, 490, 300);//dorysowuje canva
  119. add(canvas);
  120. }
  121.  
  122. // wspó³rzedne
  123. canvas.addMouseListener(new MouseListener() {
  124. public void mousePressed(MouseEvent me) { }
  125. public void mouseReleased(MouseEvent me) { }
  126. public void mouseEntered(MouseEvent me) { }
  127. public void mouseExited(MouseEvent me) { }
  128. public void mouseClicked(MouseEvent me) {
  129. int x = me.getX();
  130. int y = me.getY();
  131. // p3.setText("X:" + x + " Y:" + y);
  132. }
  133. });
  134. public int wspolrzedne(int liczba) {
  135. if(liczba>=0) {
  136. return 250+liczba;
  137. }
  138. return liczba;
  139. }
  140.  
  141. @Override
  142. public void actionPerformed(ActionEvent e) {
  143.  
  144. pierwszy_x= x1.getText();
  145. drugi_x = x2.getText();
  146. trzeci_x = x3.getText();
  147. pierwszy_y= y1.getText();
  148. drugi_y = y2.getText();
  149. trzeci_y = y3.getText();
  150.  
  151. x_1 = Integer.parseInt(pierwszy_x);
  152. x_2 = Integer.parseInt(drugi_x);
  153. x_3 = Integer.parseInt(trzeci_x);
  154.  
  155. y_1 = Integer.parseInt(pierwszy_y);
  156. y_2 = Integer.parseInt(drugi_y);
  157. y_3 = Integer.parseInt(trzeci_y);
  158.  
  159. Object myszka = e.getSource(); //pobieram myszkę (tam gdzie klikam)
  160. if(myszka == przycisk_zapisz)
  161. {
  162. PrintWriter plik;
  163. try
  164. {
  165. plik = new PrintWriter("czysawspolliniowe.txt");
  166.  
  167. 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;
  168. if(delta == 0)
  169. {
  170. plik.print("(" + x_1 + "," + y_1 + ") (" + x_2 + "," + y_2 + ") (" + x_3 + "," + y_3 + ") Punkty są współliniowe");
  171. }
  172. else
  173. {
  174. plik.print("(" + x_1 + "," + y_1 + ") (" + x_2 + "," + y_2 + ") (" + x_3 + "," + y_3 + ") Punkty nie są współliniowe");
  175. }
  176.  
  177. plik.close();
  178. }
  179. catch (FileNotFoundException e1)
  180. {
  181. e1.printStackTrace();
  182. }
  183. }
  184. else if (myszka == przycisk_zamknij)
  185. {
  186. dispose(); // komenda zamknięcia okna
  187. }
  188. else
  189. {
  190. 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;
  191. if(delta == 0)
  192. {
  193. wynikOperacji.setText(" Punkty są współliniowe ");
  194. }
  195. else
  196. {
  197. wynikOperacji.setText(" Punkty nie są współliniowe ");
  198. }
  199. }
  200. }
  201.  
  202. public static void main(String[] args)
  203. {
  204.  
  205. Test interfejs = new Test();
  206. interfejs.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  207. interfejs.setLocationRelativeTo(null); //ustawia okno na środku ekranu
  208. interfejs.setVisible(true);
  209.  
  210. }
  211. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement