Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.70 KB | None | 0 0
  1. import java.awt.Graphics;
  2. import javax.swing.JApplet;
  3. import javax.swing.JOptionPane;
  4.  
  5. @SuppressWarnings("serial")
  6. public class Rectangle extends JApplet
  7. {
  8. private int x, y, w, h;
  9.  
  10. public void init()
  11. {
  12.  
  13. try
  14. {
  15. String xinput = JOptionPane.showInputDialog("Enter x-coordinate of the top left corner: ");
  16. x = Integer.parseInt(xinput);
  17. }
  18. catch (Exception e)
  19. {
  20. System.out.println("ERROR: You entered a non-integer value.");
  21. System.exit(0);
  22. }
  23. if (x < 0)
  24. {
  25. System.out.println("ERROR: You entered a negative number.");
  26. System.exit(0);
  27. }
  28.  
  29. try
  30. {
  31. String yinput = JOptionPane.showInputDialog("Enter y-coordinate of the top left corner: ");
  32. y = Integer.parseInt(yinput);
  33. }
  34. catch (Exception e)
  35. {
  36. System.out.println("ERROR: You entered a non-integer value.");
  37. System.exit(0);
  38. }
  39. if (y < 0)
  40. {
  41. System.out.println("ERROR: You entered a negative number.");
  42. System.exit(0);
  43. }
  44.  
  45. try
  46. {
  47. String winput = JOptionPane.showInputDialog("Enter the desired width of the rectangle: ");
  48. w = Integer.parseInt(winput);
  49. }
  50. catch (Exception e)
  51. {
  52. System.out.println("ERROR: You entered a non-integer value.");
  53. System.exit(0);
  54. }
  55. if (w < 0)
  56. {
  57. System.out.println("ERROR: You entered a negative number.");
  58. System.exit(0);
  59. }
  60.  
  61. try
  62. {
  63. String hinput = JOptionPane.showInputDialog("Enter the desired height of the rectangle: ");
  64. h = Integer.parseInt(hinput);
  65. }
  66. catch (Exception e)
  67. {
  68. System.out.println("ERROR: You entered a non-integer value.");
  69. System.exit(0);
  70. }
  71. if (h < 0)
  72. {
  73. System.out.println("ERROR: You entered a negative number.");
  74. System.exit(0);
  75. }
  76. }
  77.  
  78. public void paint(Graphics g)
  79. {
  80. super.paint(g);
  81.  
  82. g.drawRect(x, y, w, h);
  83. }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement