Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.KeyEvent;
- import java.awt.event.MouseListener;
- import java.io.*;
- import java.net.ServerSocket;
- import java.net.Socket;
- import javax.swing.ActionMap;
- import javax.swing.ImageIcon;
- import javax.swing.InputMap;
- import javax.swing.Action;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.KeyEvent;
- import javax.swing.AbstractAction;
- import javax.swing.Action;
- import javax.swing.ActionMap;
- import javax.swing.ImageIcon;
- import javax.swing.InputMap;
- import javax.swing.JComponent;
- import javax.swing.KeyStroke;
- import javax.swing.Timer;
- import javax.swing.plaf.basic.BasicPopupMenuUI;
- //import static javax.swing.plaf.basic.BasicPopupMenuUI.getInputMap;
- public class Main {
- public static void main(String[] args) {
- Frame ramka = new Frame();
- Panel panel = new Panel();
- Klient klient = new Klient();
- ramka.setSize(500,500);
- ramka.add(panel,500,500);
- panel.setKlient(klient);
- ramka.setVisible(true);
- ActionListener perf = new ActionListener() {
- public void actionPerformed(ActionEvent event) {
- panel.Internet();
- panel.repaint();
- klient.setX(klient.getX() +3*klient.kierunek);
- if (klient.getX()<0){
- klient.setX(499);
- }
- if (klient.getX()>500){
- klient.setX(0);
- }
- }
- };//koniec petli glownej timera
- Timer t = new Timer(10, perf);
- t.start();
- }
- }
- class Klient extends JComponent{
- int kierunek;
- int x=50;
- public int getKierunek() {
- return kierunek;
- }
- public int getX() {
- return x;
- }
- InputMap imap = getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
- ActionMap amap = getActionMap();
- public Klient(){
- Action moveleft = new AbstractAction() {
- public void actionPerformed(ActionEvent e) {
- kierunek = -1;
- }
- };
- Action release = new AbstractAction() {
- public void actionPerformed(ActionEvent e) {
- kierunek = 0;
- }
- };
- Action moveright = new AbstractAction() {
- public void actionPerformed(ActionEvent e) {
- kierunek = +1;
- }
- };
- imap.put(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT,0,true), "moveleftrelease");
- amap.put("moveleftrelease", release);
- imap.put(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT,0,false), "moveleft");
- amap.put("moveleft", moveleft);
- imap.put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT,0,false), "moveright");
- amap.put("moveright", moveright);
- imap.put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT,0,true), "moverightrelease");
- amap.put("moverightrelease", release);
- }
- public void setKierunek(int kierunek) {
- this.kierunek = kierunek;
- }
- public void setX(int x) {
- this.x = x;
- }
- }
- class Frame extends JFrame {
- public void add(Panel panel,int _width,int _height){
- super.add(panel);
- }
- }
- class Panel extends JPanel {
- double xkuli;
- double ykuli;
- int rkuli;
- double xplat1;
- double xplat2;
- int lengthplat1;
- double yplat1;
- double yplat2;
- int lengthplat2;
- int heightplat1;
- int heightplat2;
- ServerSocket gniazdo;
- Klient klient;
- int gkuli;
- public void setKlient(Klient klient) {
- this.klient = klient;
- this.add(klient);
- }
- public Panel(){
- try {
- gniazdo = new ServerSocket(5001);
- }
- catch (IOException e){
- e.printStackTrace();
- }
- }
- public void wyslij(){
- try {
- Socket gniazdo2 = gniazdo.accept();
- PrintWriter pisarz = new PrintWriter(gniazdo2.getOutputStream());
- pisarz.println(klient.getX());
- pisarz.close();
- }
- catch (IOException e){
- e.printStackTrace();
- }
- }
- public void Internet(){
- try{
- Socket s = new Socket("192.168.8.100",5000);
- InputStreamReader strczytelnik = new InputStreamReader(s.getInputStream());
- BufferedReader czytelnik = new BufferedReader(strczytelnik);
- gkuli = Integer.parseInt(czytelnik.readLine());
- xkuli = Double.parseDouble(czytelnik.readLine());
- ykuli = Double.parseDouble(czytelnik.readLine());
- rkuli = Integer.parseInt(czytelnik.readLine());
- xplat1 = Double.parseDouble(czytelnik.readLine());
- yplat1 = Double.parseDouble(czytelnik.readLine());
- lengthplat1 = Integer.parseInt(czytelnik.readLine());
- heightplat1 = Integer.parseInt(czytelnik.readLine());
- xplat2 = Double.parseDouble(czytelnik.readLine());
- yplat2 = Double.parseDouble(czytelnik.readLine());
- lengthplat2 = Integer.parseInt(czytelnik.readLine());
- heightplat2 = Integer.parseInt(czytelnik.readLine());
- czytelnik.close();
- this.wyslij();
- }
- catch (IOException e){
- e.printStackTrace();
- }
- }
- public void paintComponent(Graphics g) {
- super.paintComponent(g);
- draw(g);
- }
- public void draw(Graphics g) {
- if (gkuli==1){
- g.setColor(Color.BLUE);
- }
- else {
- g.setColor(Color.BLACK);
- }
- g.fillOval((int)xkuli,(int)ykuli,rkuli*2,rkuli*2);
- g.setColor(Color.BLACK);
- g.fillRect((int)xplat1,(int)yplat1,lengthplat1,heightplat1);
- g.fillRect((int)xplat1+500,(int)yplat1,lengthplat1,heightplat1);
- g.fillRect((int)xplat1-500,(int)yplat1,lengthplat1,heightplat1);
- g.setColor(Color.BLUE);
- g.fillRect((int)xplat2,(int)yplat2,lengthplat2,heightplat2);
- g.fillRect((int)xplat2+500,(int)yplat2,lengthplat2,heightplat2);
- g.fillRect((int)xplat2-500,(int)yplat2,lengthplat2,heightplat2);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement