简体   繁体   中英

Adding 2 shapes at the same time on a jFrame

I read many other posts regarding this and I learned that the frame is set to BorderLayout by default. I added one shape to the west and one shape to the center of the frame. But still, only one shape comes up on the frame. The shape that comes up on the frame is the one whose location is at center.

Here is the code:

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class ShapeMover {

  private static final int FRAME_WIDTH  = 400;
  private static final int FRAME_HEIGHT = 400;
  private static final int SHAPE_WIDTH  = 50;
  private static final int INITIAL_X  = 30;
  private static final int INITIAL_Y  = 100;
  private static final int rec_x = 200;
  private static final int rec_y = 200;
  private static final int rec_height = 30;
  private static final int rec_width = 50;

  private boolean recToggle = true;
  private boolean circleToggle = true;

  private JFrame frame;
  private CircleComponent myShape;
  private RectangleComponent recShape;
  private JButton circle, rectangle;
  private JPanel panel, panel2;

  private void initialSetUp() {
      frame = new JFrame();

    myShape = new CircleComponent(INITIAL_X, INITIAL_Y, SHAPE_WIDTH);
    recShape = new RectangleComponent(rec_x, rec_y,rec_height, rec_width);

    circle = new JButton("Click for circle");
    event c = new event();
    circle.addActionListener(c);

    rectangle = new JButton("Click for rectangle");
    event2 r = new event2();
    rectangle.addActionListener(r);
    panel = new JPanel();
    panel.add(circle);
    panel.add(rectangle);
    frame.add(panel, BorderLayout.NORTH);


    frame.add(myShape, BorderLayout.WEST);
    frame.add(recShape,BorderLayout.CENTER);
    myShape.setVisible(false);
    recShape.setVisible(false);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
    frame.setVisible(true);

  } //method

  public class event implements ActionListener{
    @Override
    public void actionPerformed(ActionEvent e) {
        if(circleToggle == true){
            myShape.setVisible(true);
            circleToggle = false;
        }
        else{
            myShape.setVisible(false);
            circleToggle = true;
        }
        }
  }

  public class event2 implements ActionListener{
      @Override
      public void actionPerformed(ActionEvent e){
          if(recToggle == true){
              recShape.setVisible(true);
              recToggle = false;
          }
          else{
              recShape.setVisible(false);
              recToggle = true;
          }
          }
  }


  public static void main(String[] args) {

    ShapeMover sm = new ShapeMover();
    sm.initialSetUp();

  } //main

} //class

I warn you the other code is pretty long. Here is rectangle component:

    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Point;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseMotionAdapter;
    import javax.swing.JComponent;

    public class RectangleComponent extends JComponent{
        private CompoundShape shape2;
        private Point mousePoint2;

    public RectangleComponent(int x, int y, int height, int width){
          shape2 = new RectangleShape(x, y, height, width);
          addMouseListener(new MouseAdapter(){
              @Override
              public void mousePressed(MouseEvent event2){
                  mousePoint2 = event2.getPoint();
                  if(!shape2.contains(mousePoint2)){
                      mousePoint2 = null;
                  }
              }
          });

          addMouseMotionListener(new MouseMotionAdapter(){
              @Override
              public void mouseDragged(MouseEvent event2){
                  if(mousePoint2 == null){
                      return;
                  }
                  Point lastMousePoint2 = mousePoint2;
                  mousePoint2 = event2.getPoint();
                  double dx = mousePoint2.getX() - lastMousePoint2.getX();
                  double dy = mousePoint2.getY() - lastMousePoint2.getY();
                  shape2.translate((int) dx, (int) dy);
                  repaint();
              }
          });
    }
    public void paintComponent(Graphics g) {
        Graphics2D g2 = (Graphics2D) g;
        //shape.draw(g2);
        shape2.draw(g2);
      } //method
    }

Here is the circle Component:

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import javax.swing.JComponent;

public class CircleComponent extends JComponent{
    private Circle shape2;
    private Point mousePoint2;

public CircleComponent(int x, int y, int width){
      shape2 = new Circle(x, y, width);
      addMouseListener(new MouseAdapter(){
          @Override
          public void mousePressed(MouseEvent event2){
              mousePoint2 = event2.getPoint();
              if(!shape2.contains(mousePoint2)){
                  mousePoint2 = null;
              }
          }
      });

      addMouseMotionListener(new MouseMotionAdapter(){
          @Override
          public void mouseDragged(MouseEvent event2){
              if(mousePoint2 == null){
                  return;
              }
              Point lastMousePoint2 = mousePoint2;
              mousePoint2 = event2.getPoint();
              double dx = mousePoint2.getX() - lastMousePoint2.getX();
              double dy = mousePoint2.getY() - lastMousePoint2.getY();
              shape2.translate((int) dx, (int) dy);
              repaint();
          }
      });
}
public void paintComponent(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;
    //shape.draw(g2);
    shape2.draw(g2);
  } //method
}

Here is circle:

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.geom.Ellipse2D;
import java.awt.geom.GeneralPath;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;

public class Circle implements CompoundShape {
    private GeneralPath path = null;
    private int x;
    private int y;
    private final int width;

    public Circle(int x, int y, int width){
        this.x = x;
        this.y = y;
        this.width = width;
    }


    @Override
    public void draw(Graphics2D g2) {
        Ellipse2D.Double c = new Ellipse2D.Double(x,y,width,width);
        g2.setColor(Color.RED);
        g2.fill(c);
        g2.draw(c);

        path = new GeneralPath();
        path.append(c,false);
        g2.draw(path);
    }

    @Override
    public void translate(int dx, int dy) {
        // TODO Auto-generated method stub
        x = x + dx;
        y = y + dy;

    }

    @Override
    public boolean contains(Point point) {
        // TODO Auto-generated method stub

        if(path == null){
            return false;
        }
        return path.contains(point);
    }

}

Here is rectangle shape:

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.geom.Ellipse2D;
import java.awt.geom.GeneralPath;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;

public class RectangleShape implements CompoundShape {
    private GeneralPath path = null;
    private int x;
    private int y;
    private final int width;
    private final int height;

    public RectangleShape(int x, int y, int width, int height){
        this.x = x;
        this.y = y;
        this.width = width;
        this.height = height;
    }


    @Override
    public void draw(Graphics2D g2) {
        Rectangle r = new Rectangle(x,y,width,height);
        g2.setColor(Color.BLUE);
        g2.fill(r);
        g2.draw(r);

        path = new GeneralPath();
        path.append(r,false);
        g2.draw(path);
    }

    @Override
    public void translate(int dx, int dy) {
        // TODO Auto-generated method stub
        x = x + dx;
        y = y + dy;

    }

    @Override
    public boolean contains(Point point) {
        // TODO Auto-generated method stub

        if(path == null){
            return false;
        }
        return path.contains(point);
    }

}

Here is compound shape:

import java.awt.Graphics2D;
import java.awt.Point;

public interface CompoundShape {

  void draw(Graphics2D g2);
  void translate(int dx, int dy);
  boolean contains(Point point);

} //interface

Examples of iterating an ArrayList<Shape> can be seen in:

Of course, those examples are painting to an image, but once there is a Graphics (or Graphics2D as I prefer to work with) the principle is much the same. Copy/pasted from one example:

ArrayList<Shape> regions = separateShapeIntoRegions(imageShapeArea);
// ..
for (Shape region : regions) {
    // ..
    g.fill(region);
    // ..
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM