Advertisement
TrodelHD

Untitled

Apr 3rd, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.32 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 DrawAreaRater 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. public Graphics2D g2;
  26. // Mouse coordinates
  27. private static int sceenX=0;
  28. private static int sceenY=0;
  29. private static Double sceenX100=0.0;
  30. private static Double sceenY100=0.0;
  31. private static int minusX;
  32. private static int minusY;
  33. public DrawAreaRater(int sizeX,int sizeY) {
  34. minusX=sizeX;
  35. minusY=sizeY;
  36. sceenX=0;
  37.  
  38. setDoubleBuffered(false);
  39.  
  40. }
  41. public void drawLine(Double radius, Double oldX, Double oldY, Double currentX, Double currentY,Double r ,Double g, Double b){
  42. if(sceenX==0){
  43. sceenX=image.getWidth(null);
  44. sceenX100=((double)sceenX/100.0);
  45. sceenY=image.getHeight(null);
  46. sceenY100=((double)sceenY/100.0);
  47. }
  48. g2.setColor(new Color(r.intValue(), g.intValue(), b.intValue()));
  49. g2.setStroke(new BasicStroke(radius.intValue(), 1, 1));
  50. g2.drawLine((int)(oldX*sceenX100), (int)(oldY*sceenY100), (int)(currentX*sceenX100), (int)(currentY*sceenY100));
  51. g2.setStroke(new BasicStroke(radius.intValue()));
  52. repaint();
  53. }
  54.  
  55. protected void paintComponent(Graphics g) {
  56. if (image == null) {
  57. // image to draw null ==> we create
  58. image = createImage(minusX, minusY);
  59. g2 = (Graphics2D) image.getGraphics();
  60. // enable antialiasing
  61. g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  62. // clear draw area
  63. clear();
  64. }
  65.  
  66. g.drawImage(image, 20, 20, null);
  67. }
  68.  
  69. // now we create exposed methods
  70. public void clear() {
  71. g2.setPaint(Color.white);
  72. // draw white on entire draw area to clear
  73. g2.fillRect(-20, -20, getSize().width, getSize().height);
  74. g2.setPaint(Color.black);
  75. repaint();
  76. }
  77.  
  78. public void setColor(Color c) {
  79. // apply red color on g2 context
  80. g2.setPaint(c);
  81. }
  82.  
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement