Advertisement
TrodelHD

Untitled

Apr 3rd, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.03 KB | None | 0 0
  1. package Spiel;
  2.  
  3. import java.awt.BasicStroke;
  4. import java.awt.Color;
  5. import java.awt.Graphics;
  6. import java.awt.Graphics2D;
  7. import java.awt.Image;
  8. import java.awt.RenderingHints;
  9. import java.awt.event.MouseAdapter;
  10. import java.awt.event.MouseEvent;
  11. import java.awt.event.MouseMotionAdapter;
  12. import java.io.File;
  13. import java.io.IOException;
  14. import java.util.ArrayList;
  15.  
  16. import javax.imageio.ImageIO;
  17. import javax.swing.JComponent;
  18.  
  19.  
  20. public class DrawArea extends JComponent {
  21.  
  22. public Connection con;
  23.  
  24. public MyPanel MyP;
  25.  
  26. public int radius;
  27. // Image in which we're going to draw
  28. private Image image;
  29. // Graphics2D object ==> used to draw on
  30. private Graphics2D g2;
  31. // Mouse coordinates
  32. private int currentX, currentY, oldX, oldY;
  33. //Send arr
  34. private static int sceenX=0;
  35. private static int sceenY=0;
  36. private static Double sceenX100=0.0;
  37. private static Double sceenY100=0.0;
  38. private static int minusX;
  39. private static int minusY;
  40.  
  41.  
  42.  
  43. public DrawArea(int sizeX,int sizeY) {
  44. minusX=sizeX;
  45. minusY=sizeY;
  46. ar= "";
  47.  
  48. sceenX=0;
  49.  
  50. setDoubleBuffered(false);
  51. addMouseListener(new MouseAdapter() {
  52. public void mousePressed(MouseEvent e) {
  53. // save coord x,y when mouse is pressed
  54. oldX = e.getX()-20;
  55. oldY = e.getY()-20;
  56. }
  57. });
  58.  
  59. addMouseMotionListener(new MouseMotionAdapter() {
  60. public void mouseDragged(MouseEvent e) {
  61. // coord x,y when drag mouse
  62. currentX = e.getX()-20;
  63. currentY = e.getY()-20;
  64.  
  65. if (g2 != null) {
  66. // draw line if g2 context not null
  67.  
  68. g2.setStroke(new BasicStroke(radius, 1, 1));
  69. g2.drawLine(oldX, oldY, currentX, currentY);
  70. g2.setStroke(new BasicStroke(radius));
  71. //g2.drawOval(oldX, oldY, 20, 20);
  72. //g2.fillOval(currentX, currentY, radius, radius);
  73. // refresh draw area to repaint
  74. repaint();
  75. addtoArray(radius, oldX, oldY, currentX, currentY,g2.getColor().getRed(),g2.getColor().getGreen(),g2.getColor().getBlue());
  76. // store current coords x,y as olds x,y
  77. oldX = currentX;
  78. oldY = currentY;
  79. }
  80. }
  81. });
  82. AktivateSender();
  83. }
  84.  
  85. protected void paintComponent(Graphics g) {
  86. if (image == null) {
  87. // image to draw null ==> we create
  88. image = createImage(minusX, minusY);
  89.  
  90. //image = createImage(900, 1000);
  91. g2 = (Graphics2D) image.getGraphics();
  92. // enable antialiasing
  93. g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  94. // clear draw area
  95. clear();
  96. }
  97.  
  98. g.drawImage(image, 20, 20, null);
  99. }
  100.  
  101. // now we create exposed methods
  102. public void clear() {
  103. g2.setPaint(Color.white);
  104. // draw white on entire draw area to clear
  105. g2.fillRect(-20, -20, getSize().width, getSize().height);
  106. g2.setPaint(Color.black);
  107. con.serversendMessage("clear");
  108. repaint();
  109. }
  110.  
  111. public void setColor(Color c) {
  112. // apply red color on g2 context
  113. g2.setPaint(c);
  114. }
  115. private String ar;
  116. public void addtoArray(int radius,int oldX,int oldY,int currentX,int currentY,int r,int g,int b){
  117. if(sceenX==0){
  118. sceenX=image.getWidth(null);
  119. sceenX100=(100.0/(double)sceenX);
  120. sceenY=image.getHeight(null);
  121. sceenY100=(100.0/(double)sceenY);
  122. }
  123. String st = "";
  124. st=st+(radius)+" ";
  125. st=st+(oldX*sceenX100)+" ";
  126. st=st+(oldY*sceenY100)+" ";
  127. st=st+(currentX*sceenX100)+" ";
  128. st=st+(currentY*sceenY100)+" ";
  129. st=st+(r)+" ";
  130. st=st+(g)+" ";
  131. st=st+(b)+System.getProperty("line.separator");
  132. this.ar=ar+st;
  133.  
  134. }
  135. private boolean run;
  136. public void AktivateSender(){
  137. run = true;
  138. Thread Sender = new Thread(){
  139. public void run(){
  140. while(run){
  141. try {sleep(200);} catch (Exception e) {}
  142. if(ar.length()!=0){
  143. con.serversendMessage(ar);
  144. ar="";
  145. }
  146. }
  147. }
  148. };
  149. Sender.start();
  150. }
  151. public void DeaktivateSender(){
  152. run= false;
  153. }
  154.  
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement