2016-01-26 82 views
1

Im無法改變某種形狀的顏色。但是當用戶點擊一個菜單項時,它將成功轉換成另一個形狀。現在的問題是我想不出解決這個問題的解決方案。誰可以幫我這個事?非常感謝幫助。謝謝。在java中點擊menuitems時如何改變形狀和顏色

@Override 
    public void paintComponent(Graphics shapes) 
    { 
     super.paintComponent(shapes); 
     switch(colorNo) 
     { 
      case 0 : color = Color.RED;break; 
      case 1 : color = Color.BLUE;break; 
      case 2 : color = Color.MAGENTA;break; 
     } 
     switch(numbers) 
     { 
      case 0 : circle(shapes);break; 
      case 1 : rectangle(shapes);break; 
      case 2 : square(shapes);break; 
      case 3 : triangle(shapes);break; 
     } 
    } 

    public void circle(Graphics shapes) 
    { 
     shapes.setColor(color); 
     shapes.fillOval(250,100, 100, 100); 
    } 


    @Override 
    public void actionPerformed(ActionEvent click) { 
     if(click.getSource() == circle){ 
      numbers = 0; 
      repaint(); 
     }else if(click.getSource() == square){ 
      numbers = 1; repaint(); 
     }else if(click.getSource() == rectangle){ 
      numbers = 2; repaint(); 
     }else if(click.getSource() == triangle){ 
      numbers = 3; repaint(); 
     } 
     if(click.getSource() == red){ 
      colorNo = 0; repaint(); 
     } 

    } 

回答

2

簡單 - 在你的paintComponent方法,你超的的paintComponent電話後,和之前,你設定的形狀您的switch語句,設置圖形對象的顏色,在您設置的相同方式形狀。您當然必須爲每個顏色菜單項添加一個偵聽器才能使其工作。

這是如果您使用AbstractActions而不是讓所有東西都共享相同的ActionListener,則會更容易和更清晰。例如,這段代碼並不完全符合你所要做的,但它可以給你一些想法。

import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.Graphics; 
import java.awt.event.ActionEvent; 

import javax.swing.*; 


public class MyShapes extends JPanel { 
    private static final int PREF_W = 400; 
    private static final int PREF_H = PREF_W; 
    private JMenuBar menuBar = new JMenuBar(); 
    private Color color = Color.WHITE; 

    public MyShapes() { 
     JMenu colorMenu = new JMenu("Color"); 
     colorMenu.add(new JMenuItem(new ColorAction("Black", Color.BLACK))); 
     colorMenu.add(new JMenuItem(new ColorAction("White", Color.WHITE))); 
     colorMenu.add(new JMenuItem(new ColorAction("Red", Color.RED))); 
     colorMenu.add(new JMenuItem(new ColorAction("Blue", Color.BLUE))); 
     colorMenu.add(new JMenuItem(new ColorAction("Green", Color.GREEN))); 
     menuBar.add(colorMenu); 
    } 

    @Override 
    protected void paintComponent(Graphics g) { 
     super.paintComponent(g); 
     g.setColor(color); 
     g.fillRect(0, 0, getWidth(), getHeight()); 
    } 

    @Override 
    public Dimension getPreferredSize() { 
     if (isPreferredSizeSet()) { 
      return super.getPreferredSize(); 
     } 
     return new Dimension(PREF_W, PREF_H); 
    } 

    public JMenuBar getMenuBar() { 
     return menuBar; 
    } 

    private class ColorAction extends AbstractAction { 
     private Color actionColor; 

     public ColorAction(String name, Color color) { 
      super(name); 
      this.actionColor = color; 
     } 

     @Override 
     public void actionPerformed(ActionEvent e) { 
      color = actionColor; 
      repaint(); 
     } 
    } 

    private static void createAndShowGui() { 
     MyShapes mainPanel = new MyShapes(); 

     JFrame frame = new JFrame("MyShapes"); 
     frame.setJMenuBar(mainPanel.getMenuBar()); 
     frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
     frame.getContentPane().add(mainPanel); 
     frame.pack(); 
     frame.setLocationByPlatform(true); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 
      public void run() { 
       createAndShowGui(); 
      } 
     }); 
    } 
} 

需要注意的是,你可以畫出你的形狀以同樣的方式 - 從菜單項的操作的actionPerformed方法內填充的變量。只有這次DrawShape接口類型的變量將用於其方法,這裏是public void draw(Graphics g)。這種類型的程序設計模式被稱爲Command Pattern。例如:

import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 
import java.awt.Polygon; 
import java.awt.RenderingHints; 
import java.awt.event.ActionEvent; 

import javax.swing.*; 

@SuppressWarnings("serial") 
public class MyShapes extends JPanel { 
    private static final int PREF_W = 400; 
    private static final int PREF_H = PREF_W; 
    private JMenuBar menuBar = new JMenuBar(); 
    private Color color = null; 
    public DrawShape drawShape = null; 

    public MyShapes() { 
     // create and fill colorMenu 
     JMenu colorMenu = new JMenu("Color"); 
     colorMenu.add(new JMenuItem(new ColorAction("Black", Color.BLACK))); 
     colorMenu.add(new JMenuItem(new ColorAction("White", Color.WHITE))); 
     colorMenu.add(new JMenuItem(new ColorAction("Red", Color.RED))); 
     colorMenu.add(new JMenuItem(new ColorAction("Blue", Color.BLUE))); 
     colorMenu.add(new JMenuItem(new ColorAction("Green", Color.GREEN))); 

     // create and fill shapeMenu 
     JMenu shapeMenu = new JMenu("Shape"); 
     shapeMenu.add(new JMenuItem(new DrawShapeAction("Square", new DrawShape() { 

      @Override 
      public void draw(Graphics g) { 
       int x = getWidth()/4; 
       int y = getHeight()/4; 
       int width = getWidth()/2; 
       int height = getHeight()/2; 
       g.fillRect(x, y, width, height); 
      } 
     }))); 
     shapeMenu.add(new JMenuItem(new DrawShapeAction("Circle", new DrawShape() { 

      @Override 
      public void draw(Graphics g) { 
       int x = getWidth()/4; 
       int y = getHeight()/4; 
       int width = getWidth()/2; 
       int height = getHeight()/2; 
       g.fillOval(x, y, width, height); 
      } 
     }))); 
     shapeMenu.add(new JMenuItem(new DrawShapeAction("Triangle", new DrawShape() { 

      @Override 
      public void draw(Graphics g) { 
       int[] x = new int[3]; 
       int[] y = new int[3]; 
       x[0] = getWidth()/4; 
       x[1] = getWidth()/2; 
       x[2] = 3 * getWidth()/4; 
       y[0] = 3 * getHeight()/4; 
       y[1] = getHeight()/4; 
       y[2] = y[0]; 
       Polygon polygon = new Polygon(x, y, 3); 
       g.fillPolygon(polygon); 
      } 
     }))); 

     // add both JMenus to the JMenuBar 
     menuBar.add(colorMenu); 
     menuBar.add(shapeMenu); 
    } 

    @Override 
    protected void paintComponent(Graphics g) { 
     super.paintComponent(g); 

     // if either color or drawShape are not set, exit this method and draw nothing 
     if (color == null || drawShape == null) { 
      return; 
     } 

     // to smooth out the edges 
     Graphics2D g2 = (Graphics2D) g; 
     g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); 

     // set color and call drawShape's draw method 
     g.setColor(color); 
     drawShape.draw(g); 
    } 

    @Override 
    public Dimension getPreferredSize() { 
     if (isPreferredSizeSet()) { 
      return super.getPreferredSize(); 
     } 
     return new Dimension(PREF_W, PREF_H); 
    } 

    public JMenuBar getMenuBar() { 
     return menuBar; 
    } 

    private class ColorAction extends AbstractAction { 
     private Color actionColor; 

     public ColorAction(String name, Color color) { 
      super(name); 
      this.actionColor = color; 
     } 

     @Override 
     public void actionPerformed(ActionEvent e) { 
      MyShapes.this.color = actionColor; 
      repaint(); 
     } 
    } 

    private class DrawShapeAction extends AbstractAction { 
     private DrawShape actionDrawShape; 

     public DrawShapeAction(String name, DrawShape drawShape) { 
      super(name); 
      this.actionDrawShape = drawShape; 
     } 

     @Override 
     public void actionPerformed(ActionEvent e) { 
      MyShapes.this.drawShape = actionDrawShape; 
      repaint(); 
     } 
    } 

    private interface DrawShape { 
     void draw(Graphics g); 
    } 

    private static void createAndShowGui() { 
     MyShapes mainPanel = new MyShapes(); 

     JFrame frame = new JFrame("MyShapes"); 
     frame.setJMenuBar(mainPanel.getMenuBar()); 
     frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
     frame.getContentPane().add(mainPanel); 
     frame.pack(); 
     frame.setLocationByPlatform(true); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 
      public void run() { 
       createAndShowGui(); 
      } 
     }); 
    } 
} 
+0

是的它更清潔 –

+0

我會在解決問題後嘗試折射我的代碼。我更新了我的工作,但怎麼還沒有工作..當我點擊紅色menuitem什麼都沒有發生,但點擊形狀menuitem工作正常。你能先檢查一下嗎? –

+0

@JayGorio:我沒有看到你添加任何聽衆到你的顏色菜單項。你在做什麼?他們不會神奇地工作,但需要迷上任何事情。 –

相關問題