在jFrame上同时添加2个形状

时间:2014-11-18 06:58:37

标签: java swing java-2d jcomponent

我读了很多关于此的帖子,我了解到框架默认设置为BorderLayout。我在西边添加了一个形状,在框架的中心添加了一个形状。但仍然只有一个形状出现在框架上。框架上出现的形状是位于中心的形状。

以下是代码:

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

我警告你其他代码很长。 这是矩形组件:

    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
    }

这是圈子组件:

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
}

这是圆圈:

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);
    }

}

这是矩形:

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);
    }

}

这是复合形状:

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

1 个答案:

答案 0 :(得分:1)

迭代ArrayList<Shape>的示例见:

当然,这些示例是绘制到图像,但是一旦有Graphics(或Graphics2D我喜欢使用),原则就大致相同。复制/粘贴一个例子:

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