Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.26 KB | None | 0 0
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.ActionListener;
  5. import java.awt.event.KeyEvent;
  6. import java.awt.event.MouseListener;
  7. import java.io.*;
  8. import java.net.ServerSocket;
  9. import java.net.Socket;
  10. import javax.swing.ActionMap;
  11. import javax.swing.ImageIcon;
  12. import javax.swing.InputMap;
  13. import javax.swing.Action;
  14. import java.awt.event.ActionEvent;
  15. import java.awt.event.ActionListener;
  16. import java.awt.event.KeyEvent;
  17.  
  18. import javax.swing.AbstractAction;
  19. import javax.swing.Action;
  20. import javax.swing.ActionMap;
  21. import javax.swing.ImageIcon;
  22. import javax.swing.InputMap;
  23. import javax.swing.JComponent;
  24. import javax.swing.KeyStroke;
  25. import javax.swing.Timer;
  26. import javax.swing.plaf.basic.BasicPopupMenuUI;
  27.  
  28. //import static javax.swing.plaf.basic.BasicPopupMenuUI.getInputMap;
  29.  
  30. public class Main {
  31.  
  32.  
  33. public static void main(String[] args) {
  34.  
  35. Frame ramka = new Frame();
  36. Panel panel = new Panel();
  37. Klient klient = new Klient();
  38. ramka.setSize(500,500);
  39. ramka.add(panel,500,500);
  40. panel.setKlient(klient);
  41.  
  42.  
  43. ramka.setVisible(true);
  44. ActionListener perf = new ActionListener() {
  45. public void actionPerformed(ActionEvent event) {
  46. panel.Internet();
  47. panel.repaint();
  48. klient.setX(klient.getX() +3*klient.kierunek);
  49. if (klient.getX()<0){
  50. klient.setX(499);
  51. }
  52. if (klient.getX()>500){
  53. klient.setX(0);
  54. }
  55.  
  56.  
  57.  
  58.  
  59. }
  60. };//koniec petli glownej timera
  61. Timer t = new Timer(10, perf);
  62. t.start();
  63. }
  64.  
  65.  
  66.  
  67.  
  68. }
  69. class Klient extends JComponent{
  70. int kierunek;
  71. int x=50;
  72.  
  73. public int getKierunek() {
  74. return kierunek;
  75. }
  76.  
  77. public int getX() {
  78. return x;
  79. }
  80. InputMap imap = getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
  81. ActionMap amap = getActionMap();
  82. public Klient(){
  83. Action moveleft = new AbstractAction() {
  84. public void actionPerformed(ActionEvent e) {
  85. kierunek = -1;
  86.  
  87. }
  88. };
  89. Action release = new AbstractAction() {
  90. public void actionPerformed(ActionEvent e) {
  91. kierunek = 0;
  92.  
  93. }
  94. };
  95. Action moveright = new AbstractAction() {
  96. public void actionPerformed(ActionEvent e) {
  97. kierunek = +1;
  98. }
  99. };
  100. imap.put(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT,0,true), "moveleftrelease");
  101. amap.put("moveleftrelease", release);
  102. imap.put(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT,0,false), "moveleft");
  103. amap.put("moveleft", moveleft);
  104.  
  105.  
  106. imap.put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT,0,false), "moveright");
  107. amap.put("moveright", moveright);
  108.  
  109.  
  110. imap.put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT,0,true), "moverightrelease");
  111. amap.put("moverightrelease", release);
  112. }
  113.  
  114.  
  115. public void setKierunek(int kierunek) {
  116. this.kierunek = kierunek;
  117. }
  118.  
  119. public void setX(int x) {
  120. this.x = x;
  121. }
  122.  
  123.  
  124. }
  125.  
  126. class Frame extends JFrame {
  127. public void add(Panel panel,int _width,int _height){
  128. super.add(panel);
  129.  
  130. }
  131. }
  132.  
  133.  
  134. class Panel extends JPanel {
  135. double xkuli;
  136. double ykuli;
  137. int rkuli;
  138. double xplat1;
  139. double xplat2;
  140. int lengthplat1;
  141. double yplat1;
  142. double yplat2;
  143. int lengthplat2;
  144. int heightplat1;
  145. int heightplat2;
  146. ServerSocket gniazdo;
  147. Klient klient;
  148. int gkuli;
  149.  
  150. public void setKlient(Klient klient) {
  151. this.klient = klient;
  152. this.add(klient);
  153. }
  154.  
  155. public Panel(){
  156. try {
  157. gniazdo = new ServerSocket(5001);
  158. }
  159. catch (IOException e){
  160. e.printStackTrace();
  161. }
  162. }
  163. public void wyslij(){
  164. try {
  165. Socket gniazdo2 = gniazdo.accept();
  166.  
  167. PrintWriter pisarz = new PrintWriter(gniazdo2.getOutputStream());
  168.  
  169. pisarz.println(klient.getX());
  170. pisarz.close();
  171. }
  172. catch (IOException e){
  173. e.printStackTrace();
  174. }
  175. }
  176. public void Internet(){
  177. try{
  178. Socket s = new Socket("192.168.8.100",5000);
  179. InputStreamReader strczytelnik = new InputStreamReader(s.getInputStream());
  180. BufferedReader czytelnik = new BufferedReader(strczytelnik);
  181. gkuli = Integer.parseInt(czytelnik.readLine());
  182. xkuli = Double.parseDouble(czytelnik.readLine());
  183. ykuli = Double.parseDouble(czytelnik.readLine());
  184. rkuli = Integer.parseInt(czytelnik.readLine());
  185. xplat1 = Double.parseDouble(czytelnik.readLine());
  186. yplat1 = Double.parseDouble(czytelnik.readLine());
  187. lengthplat1 = Integer.parseInt(czytelnik.readLine());
  188. heightplat1 = Integer.parseInt(czytelnik.readLine());
  189. xplat2 = Double.parseDouble(czytelnik.readLine());
  190. yplat2 = Double.parseDouble(czytelnik.readLine());
  191. lengthplat2 = Integer.parseInt(czytelnik.readLine());
  192. heightplat2 = Integer.parseInt(czytelnik.readLine());
  193. czytelnik.close();
  194. this.wyslij();
  195.  
  196. }
  197. catch (IOException e){
  198. e.printStackTrace();
  199. }
  200. }
  201.  
  202. public void paintComponent(Graphics g) {
  203. super.paintComponent(g);
  204. draw(g);
  205. }
  206. public void draw(Graphics g) {
  207. if (gkuli==1){
  208. g.setColor(Color.BLUE);
  209. }
  210. else {
  211. g.setColor(Color.BLACK);
  212. }
  213. g.fillOval((int)xkuli,(int)ykuli,rkuli*2,rkuli*2);
  214. g.setColor(Color.BLACK);
  215. g.fillRect((int)xplat1,(int)yplat1,lengthplat1,heightplat1);
  216. g.fillRect((int)xplat1+500,(int)yplat1,lengthplat1,heightplat1);
  217. g.fillRect((int)xplat1-500,(int)yplat1,lengthplat1,heightplat1);
  218. g.setColor(Color.BLUE);
  219. g.fillRect((int)xplat2,(int)yplat2,lengthplat2,heightplat2);
  220. g.fillRect((int)xplat2+500,(int)yplat2,lengthplat2,heightplat2);
  221. g.fillRect((int)xplat2-500,(int)yplat2,lengthplat2,heightplat2);
  222.  
  223. }
  224. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement