Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 通过组合框绘制多个形状<;形状>;_Java_Jcombobox - Fatal编程技术网

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

Java 通过组合框绘制多个形状<;形状>;,java,jcombobox,Java,Jcombobox,我在重新绘制并将所有绘制的对象保留在屏幕上时遇到问题。根据我对Java的了解(不多),每次重新绘制时,我基本上都在调用同一对象的draw方法,因此只需重新绘制同一对象而不是新对象。在mouseRelease上,我正在将形状添加到列表中。我假设的每个元素引用引用内存中的相同形状,因此调用相同的draw方法。如何解决这个问题,但仍然保持组合框面向对象?我不希望使用字符串和开关盒,或者使用逻辑 除最后一条线外,椭圆和矩形的绘制方法相同 @Override public void draw(Gr

我在重新绘制并将所有绘制的对象保留在屏幕上时遇到问题。根据我对Java的了解(不多),每次重新绘制时,我基本上都在调用同一对象的draw方法,因此只需重新绘制同一对象而不是新对象。在mouseRelease上,我正在将形状添加到列表中。我假设的每个元素引用引用内存中的相同形状,因此调用相同的draw方法。如何解决这个问题,但仍然保持组合框面向对象?我不希望使用字符串和开关盒,或者使用逻辑

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

  @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());
    }
  }
公共类ShapeMakerPanel
扩展JPanel
{
私人JPanel控制;
私人JPanel currentColor;
私人JComboBox shapeChoice;
填写私人JCheckBox;
私有JButton撤销;
私人按钮清晰;
私人名单;
公共ShapeMakerPanel()
{
//初始化对象
控件=新的JPanel(新的FlowLayout(FlowLayout.CENTER,20,10));
currentColor=newjpanel();
shapeChoice=newjcombobox();
撤销=新的JButton(“撤销”);
清除=新的按钮(“清除”);
填充=新的JCheckBox(“填充”);
列表=新的ArrayList();
//设置对象名称
控件。设置名称(“控件”);
currentColor.setName(“currentColor”);
撤销。设置名称(“撤销”);
clear.setName(“clear”);
shapeChoice.setName(“shapeChoice”);
//将JPanel当前颜色设置为黑色并调整大小…将添加jcolorchooser
currentColor.setBackground(颜色:黑色);
currentColor.setPreferredSize(新维度(25,25));
/*
*将项目添加到组合框,shape类型的子类
*/
addItem(新矩形());
添加项(新椭圆形());
//将组件添加到“控制面板”
控件。添加(shapeChoice);
控件。添加(currentColor);
控件。添加(已填充);
控件。添加(撤消);
控件。添加(清除);
//将侦听器、设置和组件添加到框架的主面板
addMouseListener(新的ShapePanelMouseListener());
addMouseMotionListener(新的ShapePanelMouseStener());
挫折地面(颜色:白色);
添加(控制);
}
公共列表getShapes()
{
退货清单;
}
@凌驾
受保护组件(图形g)
{
超级组件(g);
Graphics2D g2d=(Graphics2D)g.create();
如果(!list.isEmpty())
{
用于(形状s:列表){
g2d.setColor(currentColor.getBackground());
s、 绘图(g2d);
}
}
g2d.dispose();
}
私有类ShapePanelMouseListener
扩展鼠标头捕捉器
{
私人造型;
私有ShapePanelMouseListener()
{
shape=(shape)shapeChoice.getSelectedItem();
}
@凌驾
公共无效鼠标按下(MouseEvent e)
{
shape=(shape)shapeChoice.getSelectedItem();
shape.setStartingPoint(例如getPoint());
}
@凌驾
公共无效鼠标标记(鼠标事件e)
{
shape=(shape)shapeChoice.getSelectedItem();
setEndingPoint(例如getPoint());
重新油漆();
}
@凌驾
公共无效MouseEvent(MouseEvent e)
{
列表。添加(形状);
System.out.println(list.size());
}
}

不要将
MouseListener
JComboBox
一起使用,使用
ActionListener
代替标题我根本没有一个监听器连接到JComboBox。监听器连接到主面板。另外一条评论是Shape类不是java API的,而是用于学习目的的抽象类。我相信我n不是重新发明轮子,而是为了学习目的,只是因为我以前从未处理过图形,我需要更多地学习OOP概念。在这种情况下,考虑提供一个演示你的问题的代码。这不是一个代码转储,而是你正在做的一个例子,它突出了你所面临的问题。这将导致更少。混乱和更好的响应从外观上看,您正在使用相同的
形状实例
,这意味着如果以前使用该实例添加了该形状,当您再次尝试添加该形状时,它只会更新现有实例的属性,从而将所有这些形状绘制到相同的位置