java - Use geometric shapes as components -


i trying create application similar paint, in must use basic shapes in order create more complex ones.

i use swing. have able drag , drop objects 1 jeditorpane another. want use primitives such line or circle.

what know - must primitives components in order able drag , drop them? if so, how achieve this?

i planning on doing blog entry on "playing shapes" weekend. have class creates component out of shape. component straight forward use:

shapecomponent component = new shapecomponent(shape, color.???);

here release version of code:

import java.awt.color; import java.awt.dimension; import java.awt.graphics; import java.awt.graphics2d; import java.awt.insets; import java.awt.rectangle; import java.awt.renderinghints; import java.awt.shape; import javax.swing.jcomponent;  /**  *  component paint shape object. click detection  *  determined shape itself, not bounding rectangle of shape.  *  *  shape objects can created x/y offset. these offsets  *  ignored , shape painted @ (0, 0) shape  *  contained within component.  *  *  foreground color used "fill" shape.  */ public class shapecomponent extends jcomponent {     private shape shape;     private boolean antialiasing = true;      /**      *  create shapecomponent painted black.      *      *  @param shape shape painted      */     public shapecomponent(shape shape)     {         this(shape, color.black);     }      /**      *  create shapecomponent painted filled , outlined.      *      *  @param shape shape painted      *  @param color color of shape      */     public shapecomponent(shape shape, color color)     {         setshape( shape );         setforeground( color );          setopaque( false );     }      /**      *  shape of component      *      *  @returns the shape of compnent      */     public shape getshape()     {         return shape;     }      /**      *  set shape component      *      *  @param shape shape of component      */     public void setshape(shape shape)     {         this.shape = shape;         revalidate();         repaint();     }      /**      *  use antialiasing when painting shape      *      *  @returns true antialiasing false otherwise      */     public boolean isantialiasing()     {         return antialiasing;     }      /**      *  set antialiasing property painting shape      *      *  @param antialiasing true antialiasing, false otherwise      */     public void setantialiasing(boolean antialiasing)     {         this.antialiasing = antialiasing;         revalidate();         repaint();     }      /**      * {@inheritdoc}      */     @override     public dimension getpreferredsize()     {         //  include border insets , shape bounds          insets insets = getinsets();         rectangle bounds = shape.getbounds();          //  determine preferred size          int width = insets.left + insets.right + bounds.width;         int height = insets.top + insets.bottom + bounds.height;          return new dimension(width, height);     }      /**      * {@inheritdoc}      */     @override     public dimension getminimumsize()     {         return getpreferredsize();     }      /**      * {@inheritdoc}      */     @override     public dimension getmaximumsize()     {         return getpreferredsize();     }      @override     protected void paintcomponent(graphics g)     {         super.paintcomponent(g);          //  graphics2d required antialiasing , painting shapes          graphics2d g2d = (graphics2d)g.create();          if (isantialiasing())             g2d.setrenderinghint(renderinghints.key_antialiasing, renderinghints.value_antialias_on);          //  shape translation (ie. non-zero x/y position in bounding rectangle)         //  , border insets.          rectangle bounds = shape.getbounds();         insets insets = getinsets();          //  translations @ once          g2d.translate(insets.left - bounds.x, insets.top - bounds.y);          //  fill shape          g2d.fill( shape );          g2d.dispose();     }      /**      *  determine if point in bounds of shape      *      * {@inheritdoc}      */     @override     public boolean contains(int x, int y)     {         rectangle bounds = shape.getbounds();         insets insets = getinsets();          //  check see if shape contains point. take account         //  shape x/y coordinates, border insets , shape translation.          int translatex = x + bounds.x - insets.left;         int translatey = y + bounds.y - insets.top;          return shape.contains(translatex, translatey);     } } 

it component part of question.


Comments

Popular posts from this blog

c# - Operator '==' incompatible with operand types 'Guid' and 'Guid' using DynamicExpression.ParseLambda<T, bool> -