/* * see programm avab eraldi joonistusakna ning laseb eri värvidega joonistada * sirgeid ja vabakäejooni * */ import java.awt.event.*; import java.awt.*; import java.awt.image.*; import java.applet.*; import java.util.Vector; import com.sun.image.codec.jpeg.*; //- jpeg algoritmi laadimine import java.io.*; //- input/output mooduli laadimine public class joonistus1 extends Applet{ DrawPanel panel; DrawControls controls; public void init() { setLayout(new BorderLayout()); panel = new DrawPanel(); controls = new DrawControls(panel); //-controls joonistatakse panel sisse add("Center", panel); add("North",controls); } //- end init public void destroy() { remove(panel); remove(controls); } //-end destroy public static void main(String args[]) { Frame f = new Frame("joonistus1"); joonistus1 drawTest = new joonistus1(); drawTest.init(); //-veebilehel kuvamiseks drawTest.start(); f.add("Center", drawTest); f.setSize(500, 500); f.show(); } //- end main } //-end public class /** eraldi klass p6hja joonistamiseks */ class DrawPanel extends Panel implements MouseListener, MouseMotionListener { public static final int LINES = 0; //- LINES v22rtus 0 public static final int POINTS = 1; //- POINTS v22rtus 1 int mode = LINES; //-mode defineeritakse siin Vector lines = new Vector(); Vector colors = new Vector(); int x1,y1; int x2,y2; public DrawPanel() { setBackground(Color.white); addMouseMotionListener(this); addMouseListener(this); } //- end DrawPanel /** joonistuse kujundi seadmiseks */ public void setDrawMode(int mode) { //- mode v22rtust kasutatakse siin switch (mode) { case LINES: case POINTS: this.mode = mode; break; default: throw new IllegalArgumentException(); } //-end switch } //-end setDrawMode /** hiire vedamise peale reageerimine */ public void mouseDragged(MouseEvent e) { e.consume(); switch (mode) { case LINES: x2 = e.getX(); y2 = e.getY(); break; case POINTS: default: colors.addElement(getForeground()); lines.addElement(new Rectangle(x1, y1, e.getX(), e.getY())); x1 = e.getX(); y1 = e.getY(); break; } //-end switch repaint(); } //- end mouseDragged public void mouseMoved(MouseEvent e) {} //- hiire kliki peale reageerimine */ public void mousePressed(MouseEvent e) { e.consume(); switch (mode) { case LINES: x1 = e.getX(); y1 = e.getY(); x2 = -1; break; case POINTS: default: colors.addElement(getForeground()); lines.addElement(new Rectangle(e.getX(), e.getY(), -1, -1)); x1 = e.getX(); y1 = e.getY(); repaint(); break; } //-end switch } //- end mousePressed /** nupu lahtilaskmise peale reageerimine */ public void mouseReleased(MouseEvent e) { e.consume(); switch (mode) { case LINES: colors.addElement(getForeground()); lines.addElement(new Rectangle(x1, y1, e.getX(), e.getY())); x2 = -1; break; case POINTS: default: break; } //-end switch repaint(); } //-end mouseReleased public void mouseEntered(MouseEvent e) {} public void mouseExited(MouseEvent e) {} public void mouseClicked(MouseEvent e) {} public void paint(Graphics g) { int np = lines.size(); //- np = ? /* draw the current lines */ g.setColor(getForeground()); for (int i=0; i < np; i++) { Rectangle p = (Rectangle)lines.elementAt(i); g.setColor((Color)colors.elementAt(i)); if (p.width != -1) { g.drawLine(p.x, p.y, p.width, p.height); } else { g.drawLine(p.x, p.y, p.x, p.y); } //-end if } //-end for if (mode == LINES) { //-if_1 g.setColor(getForeground()); if (x2 != -1) { //-if_2 g.drawLine(x1, y1, x2, y2); } //-end if_2 } //-end if_1 } //-end paint } //-end class DrawPanel class DrawControls extends Panel implements ItemListener { DrawPanel target; public DrawControls(DrawPanel target) { this.target = target; setLayout(new FlowLayout()); setBackground(Color.lightGray); target.setForeground(Color.red); //-miks see siin on? CheckboxGroup group = new CheckboxGroup(); Checkbox b; add(b = new Checkbox(null, group, false)); //-punane b.addItemListener(this); b.setForeground(Color.red); add(b = new Checkbox(null, group, false)); //-roheline b.addItemListener(this); b.setForeground(Color.green); add(b = new Checkbox(null, group, false)); //-sinine b.addItemListener(this); b.setForeground(Color.blue); add(b = new Checkbox(null, group, false)); //-roosa b.addItemListener(this); b.setForeground(Color.yellow); add(b = new Checkbox(null, group, true)); //-must (default) b.addItemListener(this); b.setForeground(Color.black); target.setForeground(b.getForeground()); //-seab joonistusv2rvi Choice shapes = new Choice(); //-kujundi valiku seadmine shapes.addItemListener(this); shapes.addItem("Sirged"); shapes.addItem("Vabalt"); shapes.setBackground(Color.lightGray); add(shapes); Button salvestusnupp=new Button("Salvesta"); Button lugemisnupp=new Button("Loe failist"); add(salvestusnupp); add(lugemisnupp); //salvestusnupp.addItemListener(this); //lugemisnupp.addItemListener(this); } //-end DrawControls public void paint(Graphics g) { Rectangle r = getBounds(); g.setColor(Color.lightGray); g.draw3DRect(0, 0, r.width, r.height, false); int n = getComponentCount(); for(int i=0; i