Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.84 KB | None | 0 0
  1. import java.awt.Graphics2D;
  2. import java.awt.Rectangle;
  3. import java.awt.geom.Ellipse2D;
  4. import java.awt.geom.Line2D;
  5. import java.awt.geom.Point2D;
  6.  
  7. /**
  8. A Target shape that can be positioned anywhere on the screen.
  9. */
  10. public class Target
  11. {
  12. private int xLeft;
  13. private int yTop;
  14.  
  15. /**
  16. * Constructs a Target with a given top left corner.
  17. * @param x the x-coordinate of the top-left corner
  18. * @param y the y-coordinate of the top-left corner
  19. */
  20. public Target(int x, int y)
  21. {
  22.  
  23. xLeft = x;
  24. yTop = y;
  25. }
  26.  
  27. /**
  28. * Draws the Target.
  29. * @param g2 the graphics context
  30. */
  31. public void draw(Graphics2D g2)
  32. {
  33. Ellipse2D.Double Target1 = new Ellipse2D.Double(xLeft + 69, yTop + 69, 666, 666);
  34. Ellipse2D.Double Target2 = new Ellipse2D.Double(xLeft + 220, yTop + 220, 333, 333);
  35. g2.draw(Target1);
  36. g2.draw(Target2);
  37. }
  38.  
  39.  
  40.  
  41. import javax.swing.JFrame;
  42. public class TargetViewer
  43. {
  44. public static void main(String[] args)
  45. {
  46. JFrame frame = new JFrame();
  47.  
  48. frame.setSize(300, 400);
  49. frame.setTitle("Two Targets");
  50. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  51.  
  52. TargetComponent component = new TargetComponent();
  53. frame.add(component);
  54.  
  55. frame.setVisible(true);
  56. }
  57. }
  58.  
  59. import java.awt.Graphics;
  60. import java.awt.Graphics2D;
  61. import javax.swing.JComponent;
  62. /**
  63. This component draws two Target shapes.
  64. */
  65. public class TargetComponent extends JComponent
  66. {
  67. public void paintComponent(Graphics g)
  68. {
  69. Graphics2D g2 = (Graphics2D) g;
  70.  
  71. Target target1 = new Target(0, 0);
  72.  
  73. int x = getWidth() - 60;
  74. int y = getHeight() - 30;
  75.  
  76. Target target2 = new Target(x, y);
  77.  
  78. target1.draw(g2);
  79. target2.draw(g2);
  80. }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement