通过组合框<shape>绘制多个形状

时间:2015-12-10 04:59:35

标签: java jcombobox

在重新绘制并保留屏幕上所有绘制的对象时,我遇到了问题。基本上从我对Java的了解(不是很多)是每次重绘时我基本上调用同一个对象的draw方法因此只重新绘制相同的对象而不是新的对象。在mouseRelease上我将形状添加到列表中。我假设的每个元素引用都指向内存中的相同形状,因此调用相同的draw方法。你会如何解决这个问题,但仍然保持comboBox对象的面向?我宁愿不使用字符串和switch case,否则就是逻辑。

除最后一行外,椭圆形和矩形的绘制方法相同。

  @Override
  public void draw(Graphics context)
  {
    Point startingPoint = super.getStartingPoint();
    Point endingPoint = super.getEndingPoint();

    int minX = Math.min(endingPoint.x, startingPoint.x);
    int minY = Math.min(endingPoint.y, startingPoint.y);
    int maxX = Math.max(endingPoint.x, startingPoint.x);
    int maxY = Math.max(endingPoint.y, startingPoint.y);

    context.drawOval(minX, minY, maxX - minX, maxY - minY);
  }

具有组件和图纸的JPanel。

public class ShapeMakerPanel
  extends JPanel
{
  private JPanel controls;

  private JPanel currentColor;

  private JComboBox<Shape> shapeChoice;

  private JCheckBox filled;

  private JButton undo;

  private JButton clear;

  private List<Shape> list;


  public ShapeMakerPanel()
  {
    //Initialize Objects
    controls = new JPanel(new FlowLayout(FlowLayout.CENTER, 20, 10));
    currentColor = new JPanel();
    shapeChoice = new JComboBox<Shape>();
    undo = new JButton("Undo");
    clear = new JButton("Clear");
    filled = new JCheckBox("Filled");
    list = new ArrayList<Shape>();

    //Set object names
    controls.setName("controls");
    currentColor.setName("currentColor");
    undo.setName("undo");
    clear.setName("clear");
    shapeChoice.setName("shapeChoice");

    //Set JPanel current color to black and size it...will add jcolorchooser
    currentColor.setBackground(Color.BLACK);
    currentColor.setPreferredSize(new Dimension(25, 25));

    /*
     * Add items to comboBox, subclasses of type shape
     */
    shapeChoice.addItem(new Rectangle());
    shapeChoice.addItem(new Oval());

    //Add components to the controls panel
    controls.add(shapeChoice);
    controls.add(currentColor);
    controls.add(filled);
    controls.add(undo);
    controls.add(clear);

    //Add listeners, settings and components to main panel for frame
    addMouseListener(new ShapePanelMouseListener());
    addMouseMotionListener(new ShapePanelMouseListener());
    setBackground(Color.WHITE);
    add(controls);
  }


  public List<Shape> getShapes()
  {
    return list;
  }


  @Override
  protected void paintComponent(Graphics g)
  {
    super.paintComponent(g);
    Graphics2D g2d = (Graphics2D)g.create();

    if (!list.isEmpty())
    {
        for (Shape s : list) {
            g2d.setColor(currentColor.getBackground());
            s.draw(g2d);
        }
    }
    g2d.dispose();
  }


  private class ShapePanelMouseListener
      extends MouseAdapter
  {

    private Shape shape;

    private ShapePanelMouseListener()
    {
        shape = (Shape)shapeChoice.getSelectedItem();
    }


    @Override
    public void mousePressed(MouseEvent e)
    {
        shape = (Shape)shapeChoice.getSelectedItem();
        shape.setStartingPoint(e.getPoint());
    }


    @Override
    public void mouseDragged(MouseEvent e)
    {
        shape = (Shape)shapeChoice.getSelectedItem();
        shape.setEndingPoint(e.getPoint());
        repaint();
    }

    @Override
    public void mouseReleased(MouseEvent e)
    {
        list.add(shape);
        System.out.println(list.size());
    }
  }

0 个答案:

没有答案