使用提供的代码,我想让它更加面向对象。具体来说,我想将一个布尔值(开或关)与每个椭圆相关联,告诉图形绘制它的颜色。一种颜色为真,另一种颜色为假。
我的问题是:定义一个Ellipse对象的最佳方法是告诉图形如何绘制它并将布尔值与每个对象相关联?
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 :(得分: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);
}
}