2015-11-05 61 views
1

隨着提供的代碼我想使它更面向對象。具體來說,我想將一個布爾值(打開或關閉)與每個橢圓相關聯,以告訴圖形繪製它的顏色。一種顏色爲真,另一種顏色爲假。將布爾值與Ellipse2D對象關聯

我的問題是:什麼是最好的方式來定義一個橢圓對象,告訴圖形如何繪製它並將布爾值與每個對象關聯?

import java.awt.*; 
import java.awt.geom.*; 
import java.util.*; 
import java.util.List; 
import javax.swing.*; 

public class SelfContainedExample extends JPanel { 
    private List<Shape> shapes = new ArrayList<>(); 

    public static void main(String[] args) 
    { 
     EventQueue.invokeLater(() -> createAndShowGUI()); 
    } 

    public SelfContainedExample() 
    { 
     //Circle of Radios 
     shapes.add(new Ellipse2D.Double(110, 70, 15, 15)); 
     shapes.add(new Ellipse2D.Double(90, 80, 15, 15)); 
     shapes.add(new Ellipse2D.Double(70, 100, 15, 15)); 
     shapes.add(new Ellipse2D.Double(70, 120, 15, 15)); 
     shapes.add(new Ellipse2D.Double(90, 140, 15, 15)); 
     shapes.add(new Ellipse2D.Double(110, 150, 15, 15)); 
     shapes.add(new Ellipse2D.Double(130, 140, 15, 15)); 
     shapes.add(new Ellipse2D.Double(150, 120, 15, 15)); 
     shapes.add(new Ellipse2D.Double(150, 100, 15, 15)); 
     shapes.add(new Ellipse2D.Double(130, 80, 15, 15)); 
    } 

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

     Graphics2D g2d = (Graphics2D)g.create(); 
     g2d.setColor(Color.BLACK); 

     g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, 
       RenderingHints.VALUE_ANTIALIAS_ON); 

     shapes.forEach(g2d::fill); 

     g2d.dispose(); 
    } 

    private static void createAndShowGUI() 
    { 
     //Make the big window be indented 50 pixels from each edge 
     //of the screen. 
     int inset = 50; 
     Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); 

     JFrame frame = new JFrame("Example"); 
     JDesktopPane desktopPane = new JDesktopPane(); 
     JInternalFrame internalFrame = new JInternalFrame("Example", 
       false, //resizable 
       false, //closable 
       false, //maximizable 
       true); //iconifiable 

     internalFrame.setSize(260, 260); 
     internalFrame.add(new SelfContainedExample()); 
     internalFrame.setVisible(true); 

     desktopPane.add(internalFrame); 
     desktopPane.setVisible(true); 
     desktopPane.setBounds(inset, inset, 
       screenSize.width - inset * 7, 
       screenSize.height - inset * 4); 

     frame.add(desktopPane); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setSize(desktopPane.getSize()); 
     frame.setLocationByPlatform(false); 
     frame.setLocationRelativeTo(null); 
     frame.setContentPane(desktopPane); 
     frame.setVisible(true); 
    } 
} 
+0

創建一個包含'boolean'屬性和'Ellipse2D'屬性的包裝或代理類 – MadProgrammer

+0

@MadProgrammer您能否詳細說明一下?也許提供一些示例代碼?謝謝! – feltersnach

回答

1

你可以簡單地創建它保持了參考Ellipse一個包裝類,它的狀態......

public class ColourFullShape { 
    private Shape shape; 
    private boolean state; 

    public ColourFullShape(Shape shape, boolean state) { 
     this.shape = shape; 
     this.state = state; 
    } 

    public boolean getState() { 
     return state; 
    } 

    public Shape getShape() { 
     return shape; 
    } 

} 

同樣,你可以只寫一個代理Shape類:

public class ColourFullShape implements Shape { 
    private Shape shape; 
    private boolean state; 

    public ColourFullShape(Shape shape, boolean state) { 
     this.shape = shape; 
     this.state = state; 
    } 

    public boolean getState() { 
     return state; 
    } 

    public Shape getShape() { 
     return shape; 
    } 

    @Override 
    public Rectangle getBounds() { 
     return getShape().getBounds(); 
    } 

    @Override 
    public Rectangle2D getBounds2D() { 
     return getShape().getBounds2D(); 
    } 

    @Override 
    public boolean contains(double x, double y) { 
     return getShape().contains(x, y); 
    } 

    @Override 
    public boolean contains(Point2D p) { 
     return getShape().contains(p); 
    } 

    @Override 
    public boolean intersects(double x, double y, double w, double h) { 
     return getShape().intersects(x, y, w, h); 
    } 

    @Override 
    public boolean intersects(Rectangle2D r) { 
     return getShape().intersects(r); 
    } 

    @Override 
    public boolean contains(double x, double y, double w, double h) { 
     return getShape().contains(x, y, w, h); 
    } 

    @Override 
    public boolean contains(Rectangle2D r) { 
     return getShape().contains(r); 
    } 

    @Override 
    public PathIterator getPathIterator(AffineTransform at) { 
     return getShape().getPathIterator(at); 
    } 

    @Override 
    public PathIterator getPathIterator(AffineTransform at, double flatness) { 
     return getShape().getPathIterator(at, flatness); 
    } 

}