TrodelHD

Untitled

Mar 17th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.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 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.  
  28. public DrawAreaRater() {
  29. setDoubleBuffered(false);
  30.  
  31. }
  32. public void drawLine(int radius, int oldX, int oldY, int currentX, int currentY,int r ,int g, int b){
  33. g2.setColor(new Color(r, g, b));
  34. g2.setStroke(new BasicStroke(radius));
  35. g2.drawLine(oldX, oldY, currentX, currentY);
  36. repaint();
  37. }
  38.  
  39. protected void paintComponent(Graphics g) {
  40. if (image == null) {
  41. // image to draw null ==> we create
  42. image = createImage(480, 560);
  43. g2 = (Graphics2D) image.getGraphics();
  44. // enable antialiasing
  45. g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  46. // clear draw area
  47. clear();
  48. }
  49.  
  50. g.drawImage(image, 20, 0, null);
  51. }
  52.  
  53. // now we create exposed methods
  54. public void clear() {
  55. g2.setPaint(Color.white);
  56. // draw white on entire draw area to clear
  57. g2.fillRect(-20, 0, getSize().width, getSize().height);
  58. g2.setPaint(Color.black);
  59. repaint();
  60. }
  61.  
  62. public void setColor(Color c) {
  63. // apply red color on g2 context
  64. g2.setPaint(c);
  65. }
  66.  
  67. }
Add Comment
Please, Sign In to add comment