Crenox

Java Game Programming Basics

Apr 24th, 2014
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.07 KB | None | 0 0
  1. // The Board is a panel, where the game takes place.
  2.  
  3. package com.zetcode.main;
  4.  
  5. import java.awt.BasicStroke;
  6. import java.awt.Color;
  7. import java.awt.Dimension;
  8. import java.awt.Graphics;
  9. import java.awt.Graphics2D;
  10. import java.awt.RenderingHints;
  11. import java.awt.geom.AffineTransform;
  12. import java.awt.geom.Ellipse2D;
  13. import javax.swing.*;
  14.  
  15. @SuppressWarnings("serial")
  16. public class Board extends JPanel
  17. {
  18. public void paint(Graphics g)
  19. {
  20. super.paint(g);
  21.  
  22. // Provides a sophisticated control over painting
  23. Graphics2D g2 = (Graphics2D) g;
  24.  
  25. // The rendering hints are used to make the drawing smooth
  26. RenderingHints rh = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  27.  
  28. rh.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
  29.  
  30. g2.setRenderingHints(rh);
  31.  
  32. // We get the height and the width of the window
  33. Dimension size = getSize();
  34. double w = size.getWidth();
  35. double h = size.getHeight();
  36.  
  37. // Here we create the ellipse
  38. Ellipse2D e = new Ellipse2D.Double(0, 0, 80, 130);
  39. g2.setStroke(new BasicStroke(1));
  40. g2.setColor(Color.gray);
  41.  
  42. // Here the ellipse is rotated 72 times to create a "donut"
  43. for (double deg = 0; deg < 360; deg += 5) {
  44. AffineTransform at = AffineTransform.getTranslateInstance(w / 2, h / 2);
  45. at.rotate(Math.toRadians(deg));
  46. g2.draw(at.createTransformedShape(e));
  47. }
  48. }
  49. }
  50. -------------------------------------------------------------------------------------------------------------------------------------
  51. package com.zetcode.main;
  52.  
  53. import javax.swing.*;
  54.  
  55. public class Zetcode extends JFrame
  56. {
  57. private static final int WIDTH = 360;
  58. private static final int HEIGHT = 310;
  59.  
  60. public Zetcode()
  61. {
  62. add(new Board());
  63. setVisible(true);
  64. setTitle("Donut");
  65. setSize(WIDTH, HEIGHT);
  66. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  67. setLocationRelativeTo(null);
  68. setResizable(false);
  69. }
  70.  
  71. public static void main(String args[])
  72. {
  73. new Zetcode();
  74. }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment