//EJEMPLO DE GRAFICOS EN JAVA 2D API
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
public class Draw2D extends Frame {
static final int numShapes = 5;
Shape shapes[] = new Shape[numShapes];
static final int width = 600;
static final int height = 400;
MyMenuBar menuBar;
EventHandler eh = new EventHandler();
public static void main(String args[]){
Draw2D app = new Draw2D();
}
public Draw2D() {
super("Draw2D");
setupMenuBar();
add("Center",new MyCanvas());
createShapes();
setSize(width,height);
addWindowListener(eh);
show();
}
void createShapes() {
for(int i=0;i<shapes.length;++i) shapes[i] = null;
shapes[0] = new Line2D.Double(0.0,0.0,100.0,100.0);
shapes[1] = new Rectangle2D.Double(100.0,100.0,200.0,200.0);
shapes[2] = new Ellipse2D.Double(200.0,200.0,100.0,100.0);
GeneralPath path =
new GeneralPath(new Line2D.Double(300.0,100.0,400.0,150.0));
path.append(new Line2D.Double(400.0,150.0,350.0,200.0),true);
path.append(new Line2D.Double(350.0,200.0,325.0,175.0),true);
path.append(new Line2D.Double(325.0,175.0,300.0,100.0),true);
shapes[3] = path;
shapes[4] = new RoundRectangle2D.Double(350.0,250,200.0,100.0,50.0,25.0);
}
void setupMenuBar(){
Object menuItems[][] = {{"File","Exit"}};
menuBar = new MyMenuBar(menuItems,eh,eh);
setMenuBar(menuBar);
}
class MyCanvas extends Canvas {
public void paint(Graphics graphics) {
Graphics2D g = (Graphics2D) graphics;
for(int i=0;i<shapes.length;++i) {
if(shapes[i]!=null) g.draw(shapes[i]);
}
}
}
class EventHandler extends WindowAdapter implements ActionListener,
ItemListener {
public void actionPerformed(ActionEvent e){
String selection=e.getActionCommand();
if("Exit".equals(selection)){
System.exit(0);
}
}
public void itemStateChanged(ItemEvent e){
}
public void windowClosing(WindowEvent e){
System.exit(0);
}
}
}