/* * *Graafiline programm *Ette antud väärtusega joonistatakse kujundeid * */ import java.awt.*; import java.awt.event.*; import java.awt.Choice; import java.applet.*; import java.applet.Applet; public class Graafiline extends Applet implements ActionListener, ItemListener { TextField tf=new TextField("Arv"); Button nupp=new Button("Sisesta"); int nr=0; // mitu kujundit int s=0; // mis kujund int x=20; // kujundi x positsioon int y=40; // kujundi y positsioon boolean act=false; public Graafiline(){ setLayout(new BorderLayout()); Panel p=new Panel(new GridLayout(1,3)); Choice valik=new Choice(); valik.addItem("Kriips"); valik.addItem("Ring"); valik.addItem("Ruut"); valik.addItemListener(this); p.add(valik); p.add(tf); p.add(nupp); nupp.addActionListener(this); add(p,BorderLayout.SOUTH); } public void itemStateChanged(ItemEvent ie){ String kujund=(String)ie.getItem(); if (kujund.equals("Kriips")) s=1; if (kujund.equals("Ring")) s=2; if (kujund.equals("Ruut")) s=3; } public void actionPerformed(ActionEvent e){ nr=Integer.parseInt(tf.getText().trim()); act=true; x=20; y=40; repaint(); } public void paint(Graphics g){ if(act){ for(int i=1;i<=nr;i++){ if(nr>10){ tf.setText("Max 10"); break;} switch (s){ case 1: g.drawLine(x,y,x,y+20); break; case 2: g.drawOval(x,y,20,20); break; case 3: g.drawRect(x,y,20,20); break; default: tf.setText("ERROR"); break; } x=x+30; } } } public static void main(String argumendid[]){ Frame f=new Frame("Graafiline"); f.setSize(500,500); f.add(new Graafiline()); f.setVisible(true); f.addWindowListener(new Raamikuular()); } } class Raamikuular extends WindowAdapter{ public void windowClosing(WindowEvent e){ System.out.println("\n Copyright by Hannes \n (c)2005"); System.exit(0); } }