liangm20

Questions 12 and 13

Oct 30th, 2016
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.65 KB | None | 0 0
  1. 12.
  2. import javax.swing.JFrame;
  3. import java.awt.*;
  4. public class Triangle extends JFrame
  5. {
  6. public void paint(Graphics g) //paint class
  7. {
  8. super.paint(g);
  9. Polygon triangle = new Polygon(); //instantiating triangle object
  10. triangle.addPoint(0,0); //adding 3 points
  11. triangle.addPoint(300,200);
  12. triangle.addPoint(600,0);
  13. g.setColor(Color.BLUE);
  14. g.drawPolygon(triangle);
  15. lines(g);
  16. }
  17. public static void lines(Graphics g) //method for drawing lines inside triangle
  18. {
  19. for(int i=1;i<=20;i++)
  20. {
  21. g.drawLine(i*15,i*10,600-(i*15),i*10);
  22. }
  23. }
  24. public static void main(String [] args)
  25. {
  26. Triangle app = new Triangle();
  27. app.setSize(600,200); //the frame might block some of the drawing
  28. app.setBackground(Color.YELLOW); //sets background color to yellow (doesn't show up though for some reason)
  29. app.setVisible(true);
  30. }
  31. }
  32.  
  33. 13.
  34. import javax.swing.JFrame;
  35. import java.awt.*;
  36. public class Spiral extends JFrame
  37. {
  38. public void paint(Graphics g) //paint class
  39. {
  40. super.paint(g);
  41. spiralMethod(g);
  42. }
  43. public static void spiralMethod(Graphics g) //method for drawing spiral
  44. {
  45. for(int i=0;i<8;i++) //for loop to make the spiral
  46. {
  47. g.drawLine(i*10,i*10+10,160-10*i,i*10+10);
  48. g.drawLine(160-10*i,i*10+10,160-10*i,160-10*i);
  49. g.drawLine(160-10*i,160-10*i,i*10+10,160-10*i);
  50. g.drawLine(i*10+10,160-10*i,i*10+10,i*10+20);
  51. }
  52. }
  53. public static void main(String [] args)
  54. {
  55. Spiral app = new Spiral();
  56. app.setSize(170,170); //size of frame
  57. app.setVisible(true);
  58. }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment