swing - Java Paint Application -


i've been working on java paint application practice; however, part i'm stuck @ right how change color of pen without changing color of have drawn? i've been advised create arraylist , incorporate paintcomponent i'm confused , unsure of do. can me? didn't include tester class has buttons created already, code far.

package drawing;  import java.awt.*; import java.awt.event.*; import java.util.*; import javax.swing.*;  public class drawing extends jpanel {  private final arraylist<point> points = new arraylist<>(); private boolean drawinginprogress; private color shapecolor = color.black;  public void setshapecolor(color color) { this.shapecolor = color; }  public drawing(){ setbackground(color.white); drawinginprogress = false; addmouselistener(     new mouseadapter(){         @override         public void mouseclicked(mouseevent ev)     {         if(!drawinginprogress)     {         drawinginprogress = true;     } else {             drawinginprogress = false;         }                 }              } );      addmousemotionlistener( new mousemotionadapter(){ @override public void mousemoved(mouseevent event) {     if (drawinginprogress){         points.add(event.getpoint());         repaint();     } else {                }  } }        );     }  @override public void paintcomponent(graphics g) { super.paintcomponent(g); //g.setcolor(shapecolor); had before wrong. (point point: points) g.filloval(point.x, point.y, 8, 8); }  public void red() {     shapecolor = color.red; repaint(); }  public void blue() { shapecolor = color.blue; repaint();  }  public void green() { shapecolor = color.green; repaint(); }  } 

you create pseudo "shape" carries not information needs paint self, color (and other properties)

public interface paintshape {     public rectangle getbounds();     public color getcolor();     public void paint(graphics2d g2d); } 

then can create ever shapes want...

public abstract class abstractpaintshape implements paintshape {      private final rectangle bounds;     private final color color;      public abstractpaintshape(rectangle bounds, color color) {         this.bounds = bounds;         this.color = color;     }      @override     public rectangle getbounds() {         return bounds;     }      @override     public color getcolor() {         return color;     }  }  public class ovalpaintshape extends abstractpaintshape {      private ellipse2d oval;      public ovalpaintshape(rectangle bounds, color color) {         super(bounds, color);         oval = new ellipse2d.double(bounds.getx(), bounds.gety(), bounds.getwidth(), bounds.getheight());     }      @override     public void paint(graphics2d g2d) {         g2d.setcolor(getcolor());         g2d.fill(oval);     }  } 

or similar

you should take closer @ 2d graphics , working geometry more ideas


Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -