Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- 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.
- import java.awt.*; //I shall start with the AWT before moving into Swing to give you a complete picture of Java Graphics.
- import java.awt.event.*; //Contains all of the classes for creating user interfaces and for painting graphics and images.
- import java.awt.image.BufferStrategy; //The BufferStrategy class represents the mechanism with which to organize complex memory on a particular Canvas or Window
- import java.io.FileWriter;
- import java.io.IOException;
- import java.io.PrintWriter;
- public class Main extends Canvas implements Runnable{
- final static int WIDTH = 540;
- final static int HEIGHT = 540;
- final int TITLE_BAR_HEIGHT = 120;
- int x, y, radius;
- int dx, dy;
- //int objectW = 50, objectH = 50;
- private Thread thread;
- private boolean running = false;
- private JFrame frame;
- public JTextArea wResult = new JTextArea();
- public Main(){
- this.x = 20;
- this.y = 50;
- this.radius = 100;
- this.dx = 1;
- this.dy = 1;
- /*
- frame = new JFrame();
- frame.setSize(640, 640);
- frame.setTitle("Shapes");
- frame.setLocationRelativeTo(null);
- frame.setResizable(true);
- frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
- frame.add(this);
- frame.setVisible(true);
- */
- createFrame("Result", 640, 640);
- wResult.setBounds(0,0,640,640);
- frame.add(wResult);
- createFrame("Shapes", 640, 640);
- start();
- frame.addComponentListener(new ComponentAdapter() {
- @Override
- public void componentResized(ComponentEvent e) {
- int height = frame.getHeight();
- int width = frame.getWidth();
- System.out.println('R');
- //wResult.append("R");
- update();
- }
- @Override
- public void componentMoved(ComponentEvent e) {
- int height = frame.getHeight();
- int width = frame.getWidth();
- System.out.println('M');
- //wResult.append("M" + "\n");
- update();
- }
- });
- }
- public void createFrame(String title, int width, int height){
- frame = new JFrame();
- frame.setSize(width, height);
- frame.setTitle(title);
- frame.setLocationRelativeTo(null);
- frame.setResizable(true);
- frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
- frame.add(this);
- frame.setVisible(true);
- }
- public void start(){
- thread = new Thread(this);
- thread.start();
- running = true;
- }
- @Override
- public void run(){
- long lastTime = System.nanoTime();
- double amountOfUpdate = 60.0;
- double ns = 1000000000 / amountOfUpdate;
- double delta = 0;
- long timer = System.currentTimeMillis();
- int frames = 0;
- while(running){
- long now = System.nanoTime();
- delta += (now - lastTime) / ns;
- lastTime = now;
- while(delta >= 1){
- //update();
- delta--;
- }
- if(running)
- render();
- frames++;
- if(System.currentTimeMillis() - timer > 1000){
- timer += 1000;
- System.out.println("FPS: " + frames);
- wResult.append("FPS: " + frames + "\n"); //Append: this is only adding if I use setText everytime it is making update.
- frames = 0;
- }
- }
- }
- public void update(){
- radius = ((frame.getWidth() / 50) * (frame.getHeight() / 50))/2;
- }
- public void render() {
- BufferStrategy bs = this.getBufferStrategy();
- if(bs == null){
- this.createBufferStrategy(1);
- return;
- }
- Graphics g = bs.getDrawGraphics();
- //g.clearRect(0, 0, getWidth(), getHeight());
- g.setColor(Color.RED);
- g.fillOval((getWidth()-radius)/2 , (getHeight()-
- radius+TITLE_BAR_HEIGHT)/2, radius, radius);
- g.setColor(Color.YELLOW);
- g.fillRect((getWidth()-150)/3, ((getHeight()-150+TITLE_BAR_HEIGHT)/3),
- radius, radius);
- g.setColor(Color.YELLOW);
- g.fillRect(((getWidth()-100)/3)*2,
- (getHeight()-150+TITLE_BAR_HEIGHT)/3, radius, radius);
- }
- public static void main(String[] args) {
- new Main();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement