我想在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
}
}