Advertisement
TrodelHD

Untitled

Mar 11th, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.70 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.util.ArrayList;
  13.  
  14. import javax.swing.JComponent;
  15.  
  16.  
  17. public class DrawArea extends JComponent {
  18.  
  19. public Connection con;
  20.  
  21. public int radius;
  22. // Image in which we're going to draw
  23. private Image image;
  24. // Graphics2D object ==> used to draw on
  25. private Graphics2D g2;
  26. // Mouse coordinates
  27. private int currentX, currentY, oldX, oldY;
  28.  
  29. public DrawArea() {
  30. setDoubleBuffered(false);
  31. addMouseListener(new MouseAdapter() {
  32. public void mousePressed(MouseEvent e) {
  33. // save coord x,y when mouse is pressed
  34. oldX = e.getX()-20;
  35. oldY = e.getY();
  36. }
  37. });
  38.  
  39. addMouseMotionListener(new MouseMotionAdapter() {
  40. public void mouseDragged(MouseEvent e) {
  41. // coord x,y when drag mouse
  42. currentX = e.getX()-20;
  43. currentY = e.getY();
  44.  
  45. if (g2 != null) {
  46. // draw line if g2 context not null
  47. g2.setStroke(new BasicStroke(radius));
  48. g2.drawLine(oldX, oldY, currentX, currentY);
  49. //g2.drawOval(oldX, oldY, 20, 20);
  50. //g2.fillOval(currentX, currentY, radius, radius);
  51. // refresh draw area to repaint
  52. repaint();
  53. sendArray(radius, oldX, oldY, currentX, currentY);
  54. // store current coords x,y as olds x,y
  55. oldX = currentX;
  56. oldY = currentY;
  57. }
  58. }
  59. });
  60. }
  61.  
  62. protected void paintComponent(Graphics g) {
  63. if (image == null) {
  64. // image to draw null ==> we create
  65. image = createImage(480, 560);
  66. g2 = (Graphics2D) image.getGraphics();
  67. // enable antialiasing
  68. g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  69. // clear draw area
  70. clear();
  71. }
  72.  
  73. g.drawImage(image, 20, 0, null);
  74. }
  75.  
  76. // now we create exposed methods
  77. public void clear() {
  78. g2.setPaint(Color.white);
  79. // draw white on entire draw area to clear
  80. g2.fillRect(-20, 0, getSize().width, getSize().height);
  81. g2.setPaint(Color.black);
  82. repaint();
  83. }
  84.  
  85. public void setColor(Color c) {
  86. // apply red color on g2 context
  87. g2.setPaint(c);
  88. }
  89. public void sendArray(int radius,int oldX,int oldY,int currentX,int currentY){
  90. ArrayList<Integer> ar = new ArrayList<>();
  91. ar.add(radius);
  92. ar.add(oldX);
  93. ar.add(oldY);
  94. ar.add(currentX);
  95. ar.add(currentY);
  96. con.serversendMessage(ar);
  97. }
  98.  
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement