2016-02-19 73 views
0

我是这个网站的新手,也是一位Java编程新手。我有一个任务,我正在努力。我被要求创建一个简单的绘图程序,如Windows Paint。该程序需要有四个JRadioButtonMenuItem(铅笔,线条,矩形,椭圆),当其中一个被选中时,用户可以使用分配给单选按钮的工具进行绘制。我能够使这些工具与switch语句一起工作。但是,我的讲师要求我们不要使用if/else或switch语句。相反,我需要实现一个类层次来处理这些工具。我对从哪里开始感到困惑,尤其是类层次结构部分。如果有人能给我建议从哪里开始,这将是很好的。谢谢!Java GUI和模型问题

+4

您首先继续询问您的教师寻求帮助,然后在您遇到特定编程问题后发帖。请参阅[本帮助指南](http://stackoverflow.com/help/how-to-ask)如何提出问题以及[本FAQ](http://stackoverflow.com/help/on-主题)以查看主题上的内容。 – CubeJockey

+0

复制Paint看起来不像一个新手项目。听起来更像是一个Java高级项目。 –

+0

@明看看我的解决方案。 – user3437460

回答

1

用抽象draw方法创建一个基类,并从中派生出四个工具。然后为每个工具实施该方法。然后每个工具都会知道如何处理绘图。

+0

但如何将工具分配给每个单选按钮而不使用if/else或switch?谢谢 – Ming

+0

我没有看到if/else的需求,或者在这里切换,基本上只是为使用相应工具的每个按钮创建一个ActionListerner。 –

+0

@FrankPuffer OP没有4个按钮,他有4个单选按钮。 – user3437460

0

我有一个和Frank Puffer相似的想法。在绘图时确定动作时消除ifswitch的使用。我们可以使用抽象与重写:

abstract class Tool{ 
    public abstract void draw(); 
} 

但是,每个绘图操作不会通过单击触发。它由一系列的多个来自鼠标的动作例如鼠标向下,鼠标向上,鼠标拖动。

abstract class Tool implements MouseListener, MouseMotionListener{ 

} 

注::以上都不可能是一个接口,以及因此,代替具有单个draw()方法,我们将如下改变抽象工具类。

现在我们可以实现的子类:

class PencilTool extends Tool{ 
    //With all the overridden methods 
}  
class LineTool extends Tool{ 
    //With all the overridden methods 
} 
class RectangleTool extends Tool{ 
    //With all the overridden methods 
}  
class EllipseTool extends Tool{ 
    //With all the overridden methods 
} 

在你paintApp,可以维持当前选中的工具的一个实例。当用户单击四个单选按钮之一(铅笔,直线,矩形,椭圆)时更新当前工具实例。

class PaintApp 
{ 
    private Tool currentTool; 
    private JRadioButton rdbPencil, rdbLine, rdbRectangle, rdbEllipse; 
    private DrawingSpace drawingSpace; //See below for details 

    //constructors and other initializations not shown.. 

    public void init(){ 
     //adding of buttonGroup for radio buttons not shown.. 
     rdbPencil = new JRadioButton(); 
     rdbPencil.addItemListener(new ItemListener(){ 
      @Override 
      public void itemStateChanged(ItemEvent e){ 
       //if rdbPencil was selected, set currentTool as pencilTool 
       currentTool = new PencilTool(); 
      } 
     }); 

     rdbLine = new JRadioButton(); 
     rdbLine.addItemListener(new ItemListener(){ 
      @Override 
      public void itemStateChanged(ItemEvent e){ 
       //if rdbLine was selected, set currentTool as lineTool 
       currentTool = new LineTool(); 
      } 
     }); 
    } 
    //continue to init other tools (rectangle and ellipse) 
} 

所以,现在,无论何时用户选择一个单选按钮,currentTool将相应地更新。最后一步是将实际的鼠标操作与工具的操作关联起来。

如果您正在JPanel上绘图。很可能您的drawingSpace(JPanel)会添加一个MouseListenerMouseMotionListener

当用户在你的drawingSpace触发鼠标操作,让你的当前工具分别处理措施:

class DrawingSpace extends JPanel implement MouseListener, MouseMotionListener 
{ 
    //Other potential variables and initializations not shown.. 

    public DrawingSpace(){ 
     addMouseListener(this); 
     addMouseMotionListener(this); 
    } 

    //let the currentTool handle the events 
    @Override public void mousePressed(MouseEvent e){ 
     currentTool.mousePressed(e); 
    } 
    @Override public void mouseReleased(MouseEvent e){ 
     currentTool.mouseReleased(e); 
    } 
    @Override public void mouseDragged(MouseEvent e){ 
     currentTool.mouseDragged(e); 
    } 

    //Other overridden methods not shown 
} 

最后,记住所有我们创建刚才工具子?我们只需要告诉他们当不同的工具触发各种鼠标活动时应该怎么做。

//EXAMPLE: 
class PencilTool extends Tool{ 

    @Override public void mousePressed(MouseEvent e){ 
     //Based on e.getX() & e.getY() 
     //Draw on JPanel (or onto a BufferedImage)   
    } 
    //Other overridden methods not shown (those not in use will be empty) 
}  

class LineTool extends Tool{ 
    @Override public void mousePressed(MouseEvent e){ 
     //Get point 1 from e.getX() & e.getY()   
    } 
    @Override public void mouseReleased(MouseEvent e){ 
     //Save drawing to buffer (a BufferedImage, if you have one) 
    } 
    @Override public void mouseDragged(MouseEvent e){ 
     //Get point 2 from e.getX() & e.get() 
     //Draw a line from pt1 to pt2 
     //repaint(); 
    } 
    //Other overridden methods not shown (those not in use will be empty) 
}