Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In fact, it's pretty easy. Don't use anonymous class.</p> <p>For example, with your code, you just have to attach a defined listener. For example :</p> <pre><code>bCircle.addActionListener(new SampleActionListener(pan, new CircleShapeGenerator())); </code></pre> <p>Here is the SampleActionListener witch implements the ActionListener interface</p> <pre><code>public class SampleActionListener implements ActionListener { private final DrawingCanvas canvas; private final ShapeGenerator shapeGenerator; public SampleActionListener(DrawingCanvas canvas, ShapeGenerator shapeGenerator) { this.canvas = canvas; this.shapeGenerator = shapeGenerator; } public void actionPerformed(ActionEvent event) { this.canvas.setShapeGenerator(shapeGenerator); } } </code></pre> <p>Here is the class CircleShapeGenerator</p> <pre><code>public class CircleShapeGenerator implements ShapeGenerator { public Shape getGeneratedShape(int x, int y) { return new Ellipse2D.Float(x, y, 10, 10); } } </code></pre> <p>and the interface ShapeGenerator</p> <pre><code>public interface ShapeGenerator { Shape getGeneratedShape(int x, int y); } </code></pre> <p>and, if you want to add the action listener on the rectangle button, it's really easy now. create this class:</p> <pre><code>public class RectangleShapeGenerator implements ShapeGenerator { public Shape getGeneratedShape(int x, int y) { return new Rectangle2D.Float(x, y, 10, 10); } } </code></pre> <p>and in your application class, add the following code</p> <pre><code>toolBar.add(bRectangle, toolBar.getComponentCount() - 1); bRectangle.addActionListerner(pan, new RectangleShapeGenerator() ) </code></pre> <p>for your method paint in your DrawingCanvas, i think, you should use a code like this :</p> <pre><code>for (Shape s : shapes) { Graphics2D g2 = (Graphics2D) g; g2.draw(s); } </code></pre> <p>BR, Patouche</p> <p>Sorry for my english, i know it's not really perfect...</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload