Advertisement
Guest User

Untitled

a guest
Apr 24th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.80 KB | None | 0 0
  1. package com.company;
  2. import javax.swing.*; //All components in swing are JComponent which can be added to container classes.All components in swing are JComponent which can be added to container classes.
  3. import java.awt.*; //I shall start with the AWT before moving into Swing to give you a complete picture of Java Graphics.
  4. import java.awt.event.*; //Contains all of the classes for creating user interfaces and for painting graphics and images.
  5. import java.awt.image.BufferStrategy; //The BufferStrategy class represents the mechanism with which to organize complex memory on a particular Canvas or Window
  6. import java.io.FileWriter;
  7. import java.io.IOException;
  8. import java.io.PrintWriter;
  9.  
  10. public class Main extends Canvas implements Runnable{
  11.  
  12. final static int WIDTH = 540;
  13. final static int HEIGHT = 540;
  14. final int TITLE_BAR_HEIGHT = 120;
  15. int x, y, radius;
  16. int dx, dy;
  17. //int objectW = 50, objectH = 50;
  18. private Thread thread;
  19. private boolean running = false;
  20.  
  21. private JFrame frame;
  22.  
  23. public JTextArea wResult = new JTextArea();
  24.  
  25. public Main(){
  26.  
  27. this.x = 20;
  28. this.y = 50;
  29. this.radius = 100;
  30. this.dx = 1;
  31. this.dy = 1;
  32.  
  33. /*
  34. frame = new JFrame();
  35.  
  36. frame.setSize(640, 640);
  37. frame.setTitle("Shapes");
  38. frame.setLocationRelativeTo(null);
  39. frame.setResizable(true);
  40. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  41. frame.add(this);
  42. frame.setVisible(true);
  43. */
  44. createFrame("Result", 640, 640);
  45. wResult.setBounds(0,0,640,640);
  46. frame.add(wResult);
  47.  
  48. createFrame("Shapes", 640, 640);
  49.  
  50.  
  51. start();
  52.  
  53. frame.addComponentListener(new ComponentAdapter() {
  54. @Override
  55. public void componentResized(ComponentEvent e) {
  56. int height = frame.getHeight();
  57. int width = frame.getWidth();
  58. System.out.println('R');
  59. //wResult.append("R");
  60. update();
  61. }
  62.  
  63. @Override
  64. public void componentMoved(ComponentEvent e) {
  65. int height = frame.getHeight();
  66. int width = frame.getWidth();
  67. System.out.println('M');
  68. //wResult.append("M" + "\n");
  69. update();
  70. }
  71. });
  72. }
  73.  
  74. public void createFrame(String title, int width, int height){
  75. frame = new JFrame();
  76.  
  77. frame.setSize(width, height);
  78. frame.setTitle(title);
  79. frame.setLocationRelativeTo(null);
  80. frame.setResizable(true);
  81. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  82. frame.add(this);
  83. frame.setVisible(true);
  84. }
  85.  
  86. public void start(){
  87. thread = new Thread(this);
  88. thread.start();
  89. running = true;
  90. }
  91.  
  92. @Override
  93. public void run(){
  94.  
  95.  
  96. long lastTime = System.nanoTime();
  97. double amountOfUpdate = 60.0;
  98. double ns = 1000000000 / amountOfUpdate;
  99. double delta = 0;
  100. long timer = System.currentTimeMillis();
  101. int frames = 0;
  102.  
  103. while(running){
  104. long now = System.nanoTime();
  105. delta += (now - lastTime) / ns;
  106. lastTime = now;
  107. while(delta >= 1){
  108. //update();
  109. delta--;
  110. }
  111. if(running)
  112. render();
  113. frames++;
  114.  
  115. if(System.currentTimeMillis() - timer > 1000){
  116. timer += 1000;
  117. System.out.println("FPS: " + frames);
  118.  
  119. wResult.append("FPS: " + frames + "\n"); //Append: this is only adding if I use setText everytime it is making update.
  120.  
  121.  
  122. frames = 0;
  123. }
  124. }
  125. }
  126.  
  127. public void update(){
  128. radius = ((frame.getWidth() / 50) * (frame.getHeight() / 50))/2;
  129. }
  130.  
  131. public void render() {
  132. BufferStrategy bs = this.getBufferStrategy();
  133. if(bs == null){
  134. this.createBufferStrategy(1);
  135. return;
  136. }
  137.  
  138. Graphics g = bs.getDrawGraphics();
  139.  
  140. //g.clearRect(0, 0, getWidth(), getHeight());
  141. g.setColor(Color.RED);
  142. g.fillOval((getWidth()-radius)/2 , (getHeight()-
  143. radius+TITLE_BAR_HEIGHT)/2, radius, radius);
  144.  
  145. g.setColor(Color.YELLOW);
  146. g.fillRect((getWidth()-150)/3, ((getHeight()-150+TITLE_BAR_HEIGHT)/3),
  147. radius, radius);
  148.  
  149. g.setColor(Color.YELLOW);
  150. g.fillRect(((getWidth()-100)/3)*2,
  151. (getHeight()-150+TITLE_BAR_HEIGHT)/3, radius, radius);
  152. }
  153.  
  154. public static void main(String[] args) {
  155. new Main();
  156. }
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement