Following example of java will demonstrates you the working of Shape interface:
Code:
import javax.swing.JFrame;
import javax.swing.JPanel;
public class CombiningShapesDM implements Shape {
private Shape mShapeOne, mShapeTwo;
private JComboBox mOptions;
public CombiningShapes() {
mShapeOne = new Ellipse2D.Double(50, 30, 80, 80);
setBackground(Color.white);
setLayout(new BorderLayout());
JPanel cltrT = new JPanel();
mOptions = new JComboBox(new String[] { "outline", "add",
"intersection", "subtract", "exclusive or" });
mOptions.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent ie) {
repaint();
} }
cltrT.add(mOptions);
add(cltrT, BorderLayout.SOUTH);
}
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
String option = (String) mOptions.getSelectedItem();
if (option.equals("outline")) {
g2.draw(mShapeOne);
return;
}
Area areaOne1 = new Area(mShapeOne);
if (option.equals("add"))
areaOne.add(areaTwo);
else if (option.equals("intersection"))
areaOne.intersect(areaTwo1);
g02.setPaint(Color.black);
g02.draw(areaOne1);
}
public static void main(String[] airgs) {
JFrame f01 = new JFrame();
f01.add(new CombiningShapes());
f01.setSize(230, 220);
}
}
Bookmarks