Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
500 views
in Technique[技术] by (71.8m points)

swing - drawing shape objects in java (draggable,resizable and can rotate)

I am trying to draw shapes (triangle, rectangle, square and circle) that can be used in creating a picture. A user should be able to place a shape on a canvas and drag it around or even enlarge it to create a desired picture.

I tried overriding the paintComponent() of a JPanel to achieve this but realize that the Jpanels shape remains a square so when you have a circle you can still drag it even if u are not necessarily touching it as it is within a square and also having difficulty drawing the triangle

What libraries would you suggest?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The Shape interface and derived classes, such as Path2D, Ellipse2D and Rectangle2D, will help you here, since all classes derived from it must have a contains(Point p) method that you can use.

For example:

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;

@SuppressWarnings("serial")
public class DragShape extends JPanel {
   private static final int PREF_W = 800;
   private static final int PREF_H = 700;
   private static final Color SHAPE_COLOR = new Color(255, 100, 100);
   private Path2D myShape = new Path2D.Double();

   public DragShape() {
      myShape.append(new Ellipse2D.Double(150, 50, 200, 200), true);
      myShape.append(new Rectangle2D.Double(150, 150, 200, 400), true);
      myShape.append(new Ellipse2D.Double(350, 250, 150, 150), true);
      myShape.append(new Ellipse2D.Double(150, 450, 200, 200), true);

      MyMouseAdapter myMouseAdapter = new MyMouseAdapter();
      addMouseListener(myMouseAdapter);
      addMouseMotionListener(myMouseAdapter);
   }

   @Override
   protected void paintComponent(Graphics g) {
      super.paintComponent(g);
      Graphics2D g2 = (Graphics2D) g;
      g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
            RenderingHints.VALUE_ANTIALIAS_ON);
      if (myShape != null) {
         g2.setColor(SHAPE_COLOR);
         g2.fill(myShape);
      }
   }

   @Override
   public Dimension getPreferredSize() {
      return new Dimension(PREF_W, PREF_H);
   }

   class MyMouseAdapter extends MouseAdapter {
      private boolean pressed = false;
      private Point point;

      @Override
      public void mousePressed(MouseEvent e) {
         if (e.getButton() != MouseEvent.BUTTON1) {
            return;
         }

         if (myShape != null && myShape.contains(e.getPoint())) {
            pressed = true;
            this.point = e.getPoint();
         }
      }

      @Override
      public void mouseDragged(MouseEvent e) {
         if (pressed) {
            int deltaX = e.getX() - point.x;
            int deltaY = e.getY() - point.y;           
            myShape.transform(AffineTransform.getTranslateInstance(deltaX, deltaY));
            point = e.getPoint();
            repaint();
         }
      }

      @Override
      public void mouseReleased(MouseEvent e) {
         pressed = false;
      }
   }

   private static void createAndShowGui() {
      DragShape mainPanel = new DragShape();

      JFrame frame = new JFrame("DragShape");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

I'll let you figure out rotation and re-sizing.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...