Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Spiel;
- import java.awt.BasicStroke;
- import java.awt.Color;
- import java.awt.Graphics;
- import java.awt.Graphics2D;
- import java.awt.Image;
- import java.awt.RenderingHints;
- import java.awt.event.MouseAdapter;
- import java.awt.event.MouseEvent;
- import java.awt.event.MouseMotionAdapter;
- import java.util.ArrayList;
- import javax.swing.JComponent;
- public class DrawAreaRater extends JComponent {
- public Connection con;
- public int radius;
- // Image in which we're going to draw
- private Image image;
- // Graphics2D object ==> used to draw on
- public Graphics2D g2;
- // Mouse coordinates
- private static int sceenX=0;
- private static int sceenY=0;
- private static Double sceenX100=0.0;
- private static Double sceenY100=0.0;
- private static int minusX;
- private static int minusY;
- public DrawAreaRater(int sizeX,int sizeY) {
- minusX=sizeX;
- minusY=sizeY;
- sceenX=0;
- setDoubleBuffered(false);
- }
- public void drawLine(Double radius, Double oldX, Double oldY, Double currentX, Double currentY,Double r ,Double g, Double b){
- if(sceenX==0){
- sceenX=image.getWidth(null);
- sceenX100=((double)sceenX/100.0);
- sceenY=image.getHeight(null);
- sceenY100=((double)sceenY/100.0);
- }
- g2.setColor(new Color(r.intValue(), g.intValue(), b.intValue()));
- g2.setStroke(new BasicStroke(radius.intValue(), 1, 1));
- g2.drawLine((int)(oldX*sceenX100), (int)(oldY*sceenY100), (int)(currentX*sceenX100), (int)(currentY*sceenY100));
- g2.setStroke(new BasicStroke(radius.intValue()));
- repaint();
- }
- protected void paintComponent(Graphics g) {
- if (image == null) {
- // image to draw null ==> we create
- image = createImage(minusX, minusY);
- g2 = (Graphics2D) image.getGraphics();
- // enable antialiasing
- g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
- // clear draw area
- clear();
- }
- g.drawImage(image, 20, 20, null);
- }
- // now we create exposed methods
- public void clear() {
- g2.setPaint(Color.white);
- // draw white on entire draw area to clear
- g2.fillRect(-20, -20, getSize().width, getSize().height);
- g2.setPaint(Color.black);
- repaint();
- }
- public void setColor(Color c) {
- // apply red color on g2 context
- g2.setPaint(c);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement