2014-04-25 44 views
0

我想在JFrame中实现一个菜单,在其中可以选择要绘制的形状。 我有一个扩展JFrame的类和一个扩展JPanel的类:所以在我的框架中,我添加菜单和面板,就这么简单。为了区分不同的形状,我使用了一个带有4个值的Enum对象(Rect,Ellipse,Line和FreeHand)。Java - 在菜单中选择形状并在JPanel中绘制

问题是我无法理解如何在JPanel上绘制形状,因为我有一个MyShape类和4个不同的类,它们扩展了MyShape,它为不同形状行为重载MyShape的函数。

你可以检查我的代码,并建议我最好的办法吗?

谢谢!

这里是我的代码:

MyFrame.java

public class MainFrame extends JFrame implements Runnable,ActionListener { 

JMenuBar menuBar; 
JMenu menu; 
JMenu subMenu = new JMenu(); 
JMenu subMenu2 = new JMenu(); 

JMenuItem menuItemActionsDraw; 
JMenuItem menuItemActionsMove; 
JMenuItem menuItemActionsResize; 
JMenuItem menuItemActionsDel; 

JMenuItem menuItemRect; 
JMenuItem menuItemOval; 
JMenuItem menuItemLine; 
JMenuItem menuItemFreeHand; 

DrawingArea area; 

public MainFrame(){ 

} 

public static void main(String[] args) { 
    // TODO Auto-generated method stub 
    MainFrame mframe = new MainFrame(); 
    SwingUtilities.invokeLater(mframe); 

} 

@Override 
public void run() { 
    // TODO Auto-generated method stub 
    final JFrame jf = new JFrame("ShapeDraw"); 
    area = new DrawingArea(); 

    createMenu(); 
    jf.setJMenuBar(menuBar); 
    jf.add(area); 

    jf.pack(); 
    jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    jf.setVisible(true); 

} 

@Override 
public void actionPerformed(ActionEvent arg0) { 
    // TODO Auto-generated method stub 
    JMenuItem source = (JMenuItem)(arg0.getSource()); 

    if (source.equals(menuItemActionsDraw)){ 

    } 
    else if (source.equals(menuItemActionsMove)){ 

    } 
    else if (source.equals(menuItemActionsResize)){ 

    } 
    else if (source.equals(menuItemActionsDel)){ 

    } 
    else if (source.equals(menuItemRect)){ 
     //MyRect rect = new MyRect(x, y, width, heigth); 
     DrawingArea.shape = EnumShapes.RECT; 

    } 
    else if (source.equals(menuItemOval)){ 

    } 
    else if (source.equals(menuItemLine)){ 

    } 
    else if (source.equals(menuItemFreeHand)){ 

    } 
} 

public void createMenu(){ 
    menuBar = new JMenuBar(); 
    menu = new JMenu("Menu"); 

    subMenu = new JMenu("Actions"); 

    menuItemActionsDraw = new JMenuItem("Draw"); 
    menuItemActionsMove = new JMenuItem("Move"); 
    menuItemActionsResize = new JMenuItem("Resize"); 
    menuItemActionsDel = new JMenuItem("Delete"); 

    menuItemRect = new JMenuItem("Rect"); 
    menuItemOval = new JMenuItem("Oval"); 
    menuItemLine = new JMenuItem("Line"); 
    menuItemFreeHand= new JMenuItem("FreeHand"); 

    subMenu.add(menuItemActionsDraw); 
    subMenu.add(menuItemActionsMove); 
    subMenu.add(menuItemActionsResize); 
    subMenu.add(menuItemActionsDel); 

    menuItemActionsDraw.addActionListener(this); 
    menuItemActionsMove.addActionListener(this); 
    menuItemActionsResize.addActionListener(this); 
    menuItemActionsDel.addActionListener(this); 

    menu.add(subMenu); 

    subMenu2 = new JMenu("Shapes"); 

    menuItemRect.addActionListener(this); 
    menuItemOval.addActionListener(this); 
    menuItemLine.addActionListener(this); 
    menuItemFreeHand.addActionListener(this); 

    subMenu2.add(menuItemRect); 
    subMenu2.add(menuItemOval); 
    subMenu2.add(menuItemLine); 
    subMenu2.add(menuItemFreeHand); 

    menu.add(subMenu2); 

    menuBar.add(menu); 
} 
} 

DrawingArea.java

public class DrawingArea extends JPanel implements MouseInputListener, MouseMotionListener { 

public ArrayList<MyShape> displayList = new ArrayList<MyShape>(); 
public static EnumShapes shape; 
public MyRect rect; 
public Graphics2D g2d; 

public DrawingArea() { 
    super(); 
    this.setLayout(new BorderLayout()); 
    super.setPreferredSize(new Dimension(500,500)); 
    setBorder(BorderFactory.createLineBorder(Color.black)); 

    addMouseListener(this); 
    addMouseMotionListener(this); 
} 

@Override 
public void paintComponent(Graphics g){ 

} 

@Override 
public void mouseClicked(MouseEvent arg0) { 
    // TODO Auto-generated method stub 

} 

@Override 
public void mouseEntered(MouseEvent arg0) { 
    // TODO Auto-generated method stub 

} 

@Override 
public void mouseExited(MouseEvent arg0) { 
    // TODO Auto-generated method stub 

} 

@Override 
public void mousePressed(MouseEvent arg0) { 
    // TODO Auto-generated method stub 
    switch(shape){ 
    case RECT: 
     rect = new MyRect(arg0.getX(), arg0.getY(), 0, 0); 
    case OVAL: 
     // to do stuff 
    case LINE: 
     // to do stuff 
    case FREEHAND: 
     // to do stuff 
    default: 
     break; 
    } 
    repaint(); 

} 

@Override 
public void mouseReleased(MouseEvent arg0) { 
    // TODO when finished dragging, add shape to displayList 
    //displayList.add(rect); 
} 

@Override 
public void mouseDragged(MouseEvent arg0) { 
    // TODO while mouse is dragged, set new width and height and repaint 

    // I should use x and y and calculate the distance between x2 and y2, 
    // and then set the difference as width and height 

    switch(shape){ 
    case RECT: 
     rect.setWidthHeight(arg0.getX()-rect.x, rect.y-arg0.getY()); 
     rect.draw(g2d); 
    case OVAL: 
     // to do stuff 
    case LINE: 
     // to do stuff 
    case FREEHAND: 
     // to do stuff 
    default: 
     break; 
    } 
} 

@Override 
public void mouseMoved(MouseEvent arg0) { 
    // TODO Auto-generated method stub 

} 

public void setShape(EnumShapes shape){ 
    this.shape = shape; 
} 
} 

MyShape.java

public abstract class MyShape { 
protected int x; 
protected int y; 
protected int width; 
protected int height; 
protected EnumShapes shape; 

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

public abstract void draw(Graphics2D g); 
public abstract void fill(Graphics2D g); 
public abstract void setFrame(double x, double y, double width, double height); 
public abstract void drawSelectionBox(Graphics2D g); 

public void setWidthHeight(int width, int height) { 
    // TODO Auto-generated method stub 
    this.width = width; 
    this.height = height; 
} 

} 

MyRect.java

public class MyRect extends MyShape { 

public MyRect(int x, int y, int width, int height) { 
    super(x, y, width, height); 
    // TODO Auto-generated constructor stub 
} 

@Override 
public void draw(Graphics2D g) { 
    // TODO Auto-generated method stub 
    g.drawRect(x, y, width, height); 
} 

@Override 
public void fill(Graphics2D g) { 
    // TODO Auto-generated method stub 

} 

@Override 
public void setFrame(double x, double y, double width, double height) { 
    // TODO write stuff 

} 

@Override 
public void drawSelectionBox(Graphics2D g) { 
    // TODO write stuff 

} 
} 

回答

1

“的问题是,我不明白怎么画在JPanel的形状,因为我有一个MyShape的类和扩展该MyShape的过载4个不同的类别MyShape用于不同形状行为的功能。“

在您的DrawingArea类中有MyShape selectedShape;。并有二传手它

public void setSelectedShape(MyShape selectedShape) { 
    this.selectedShape = selectedShape; 
    repaint(); 
} 

你可以画选定的形状

protected void paintComponent(Graphics g) { 
    super.paintComponent(g); 
    Graphics2D g2 = (Graphics2D)g; 
    if (selectedShaped != null) { 
     selectedShape.draw(g2); 
    } 
} 

只需拨打从听者setSelectedShape方法在你的框架类,只要你想得出不同的形状。

如果你的形状有不同的方法可能要调用(比在抽象类中定义的除外),检查instanceof

if (selectedShape instanceof MyRect) { 
    // do something 
}