Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You need to add a mouselistener and a mousemotionlistener (usually you make a single instance for both) and check wheter your shape contains the mouse pressed event or not. If yes, you keep track of where the mouse is dragged to translate your shape and continuously call <code>repaint()</code>, like usual.</p> <ul> <li>Single click creates a vertex of a polygon</li> <li>Double click creates the current drawn polygon (if it has at least 3 vertices) and we create a new one</li> <li>Right-click clears the current drawn polygon and creates a new one</li> <li>Press/Drag/Release moves the polygon located under the mouse (if there are several, it takes the first one found. it would probably better to make a reverse for-loop)</li> </ul> <p>Here is an example:</p> <pre><code>import java.awt.Color; import java.awt.Dimension; import java.awt.Font; import java.awt.Graphics; import java.awt.Point; import java.awt.Polygon; import java.awt.event.MouseAdapter; import java.awt.geom.Rectangle2D; import java.util.ArrayList; import java.util.List; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.SwingUtilities; public class TestNaming { private static final int PANEL_WIDTH = 600; private static final int PANEL_HEIGHT = 600; public static class Drawing extends JPanel { private static final Font FONT = new Font("Arial", Font.PLAIN, 12); private List&lt;Polygon&gt; polygons = new ArrayList&lt;Polygon&gt;(); private Polygon currentPolygon = new Polygon(); private MouseAdapter mouseListener = new MouseAdapter() { private Polygon dragged; private Point lastLocation; @Override public void mousePressed(java.awt.event.MouseEvent e) { for (Polygon p : polygons) { if (p.contains(e.getPoint())) { dragged = p; lastLocation = e.getPoint(); break; } } } @Override public void mouseDragged(java.awt.event.MouseEvent e) { if (dragged != null) { dragged.translate(e.getX() - lastLocation.x, e.getY() - lastLocation.y); lastLocation = e.getPoint(); repaint(); } } @Override public void mouseReleased(java.awt.event.MouseEvent e) { dragged = null; lastLocation = null; } @Override public void mouseClicked(java.awt.event.MouseEvent e) { if (SwingUtilities.isLeftMouseButton(e)) { if (e.getClickCount() == 1) { addPoint(e.getX(), e.getY()); } else if (e.getClickCount() == 2) { createPolygon(); } } else if (SwingUtilities.isRightMouseButton(e)) { clearCurrentPolygon(); } } }; public Drawing() { addMouseListener(mouseListener); addMouseMotionListener(mouseListener); } protected void addPoint(int x, int y) { currentPolygon.addPoint(x, y); repaint(); } protected void clearCurrentPolygon() { currentPolygon = new Polygon(); repaint(); } protected void createPolygon() { if (currentPolygon.npoints &gt; 2) { polygons.add(currentPolygon); } clearCurrentPolygon(); repaint(); } @Override public Dimension getPreferredSize() { return new Dimension(PANEL_WIDTH, PANEL_HEIGHT); } @Override protected void paintComponent(Graphics g) { super.paintComponent(g); g.setColor(Color.RED); g.setFont(FONT); for (Polygon polygon : polygons) { drawPolygon(g, polygon); } g.setColor(Color.GREEN); drawPolygon(g, currentPolygon); } private void drawPolygon(Graphics g, Polygon polygon) { if (polygon.npoints &lt; 3) { if (polygon.npoints == 1) { g.fillOval(polygon.xpoints[0] - 2, polygon.ypoints[0] - 2, 4, 4); drawNthPoint(g, polygon, 0); } else if (polygon.npoints == 2) { g.drawLine(polygon.xpoints[0], polygon.ypoints[0], polygon.xpoints[1], polygon.ypoints[1]); drawNthPoint(g, polygon, 0); drawNthPoint(g, polygon, 1); } } else { g.drawPolygon(polygon); for (int i = 0; i &lt; polygon.npoints; i++) { drawNthPoint(g, polygon, i); } } } private void drawNthPoint(Graphics g, Polygon polygon, int nth) { // Only works 26 times! String name = Character.toString((char) ('A' + nth)); int x = polygon.xpoints[nth]; int height = g.getFontMetrics().getHeight(); int y = polygon.ypoints[nth] &lt; height ? polygon.ypoints[nth] + height : polygon.ypoints[nth]; Rectangle2D stringBounds = g.getFontMetrics().getStringBounds(name, g); g.drawString(name, x, y); } } protected static void initUI() { JFrame frame = new JFrame("test"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(new Drawing()); frame.pack(); frame.setVisible(true); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { initUI(); } }); } } </code></pre>
 

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