Rešitev: program Spirala (Java)

Iz MaFiRaWiki

Naloga: program Spirala

  1.  
  2. import java.applet.*;
  3. import java.awt.*
  4.  
  5. public class Spirala extends Applet
  6. {
  7. public void paint(Graphics g)
  8. {
  9. int n = Integer.parseInt(getParameter("n"));
  10. int dolzina = Integer.parseInt(getParameter("dolzina"));
  11. int korak = Integer.parseInt(getParameter("korak"));
  12. int x = getWidth()/2;
  13. int y = getHeight()/2;
  14.  
  15. int x1 = x;
  16. int y1 = y;
  17. int x2;
  18. int y2;
  19. int i = 1;
  20. int j = 0;
  21. while (i<=n){
  22. if(i%4==1){
  23. x2 = x1 + dolzina + j*korak;
  24. y2 = y1;
  25. }
  26. else if(i%4==2){
  27. x2 = x1;
  28. y2 = y1 - dolzina - j*korak;
  29. }
  30. else if(i%4==3){
  31. x2 = x1 - dolzina - j*korak;
  32. y2 = y1;
  33. }
  34. else{
  35. x2 = x1;
  36. y2 = y1 + dolzina + j*korak;
  37. }
  38. g.drawLine(x1, y1, x2, y2);
  39. x1 = x2;
  40. y1 = y2;
  41. ++i;
  42. ++j;
  43. }
  44. }
  45. }

  1.  
  2.  
  3. <applet width="300" height="300" code="Spirala.class">
  4. <param name="n" value"21">
  5. <param name="dolzina" value="10">
  6. <param name="korak" value="10">
  7. </applet>
  8.  
  9. </body>
  10. </html>
Osebna orodja