Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: rotating

  1. #1
    Junior Member
    Join Date
    Aug 2021
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default rotating

    Hello,

    im in the thick of trying to make a game with a rotating ship i got along way with it but got stuck just on rotation now im just trying to figure out how to get a shape to rotate in java.
    so i simplified all the code and back to basics but now im stuck it i just want the square to start spinning on the screen and i though i would use a for loop but it just stares at me stationary cursing my stupidity

    please help

    package rotate;
     
    import java.awt.Canvas;
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.image.BufferStrategy;
     
    public class Main extends Canvas implements Runnable {
     
    	/**
    	 * 
    	 */
    	private static final long serialVersionUID = 1L;
     
    	public static final int WIDTH = 800, HEIGHT = WIDTH / 12*9;
     
    	private Thread thread;
    	private boolean running = false;
     
    	public Main() {
    		new Window(WIDTH, HEIGHT, "rotate", this);
     
    	}
     
    	public synchronized void start() {
    		thread = new Thread(this);
    		thread.start();
    		running = true;
    	}
     
    	public synchronized void stop() {
    		try {
    			thread.join();
    			running = false;
    		}catch(Exception e) {
    			e.printStackTrace();
    		}
    	}
     
    	public void run() {
    		long lastTime = System.nanoTime();
    		double amountOfTicks = 60.0;
    		double ns = 1000000000/ amountOfTicks;
    		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) {
    				tick();
    				delta--;
    			}
    			if(running)
    				render();
    			frames++;
     
    			if(System.currentTimeMillis() - timer > 1000) {
    				timer += 1000;
    				System.out.println("FPS: " + frames);
    				frames = 0;
    			}
    		}
    		stop();
    	}
     
    	private void tick() {
     
    	}
     
    	private void render() {
    		BufferStrategy bs = this.getBufferStrategy();
    		if (bs == null) {
    			this.createBufferStrategy(3);
    			return;
    		}
    		Graphics g = bs.getDrawGraphics();
    		Graphics2D g2d = (Graphics2D) g;
    		int count = 0;
     
    		for (int i = 0; i < 360; i++) {
    			count++;
    		}
     
     
    		g.setColor(Color.BLACK);
    		g.fillRect(0,0,WIDTH, HEIGHT);
     
    		g.setColor(Color.BLUE);
    		g.translate(100,100);
     
    		for (int i = 0; i < 360; i++) {
    			count++;
    			g2d.rotate(Math.toRadians(count));
    		}
    		g.fillRect(-25,-25,50,50);
     
     
     
     
    		g.dispose();
    		bs.show();
     
    	}
     
    	public static void main(String[] args) {
    		new Main();
     
     
     
    	}
    }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: rotating

    The code has an incorrect call to the Window class's constructor:
    		new Window(WIDTH, HEIGHT, "rotate", this);         //<<<<< no suitable constructor
    Also there was no import statement for the Window class.

    Where did you copy this code from? Can you post a link to the site?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Aug 2021
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: rotating

    this is my window class

    Im using a tutorial on youtube by a guy called gametut

    import java.awt.Canvas;
    import java.awt.Dimension;
     
    import javax.swing.JFrame;
     
    public class Window extends Canvas{
     
    	/**
    	 * 
    	 */
    	private static final long serialVersionUID = 1L;
     
    	public Window(int width, int height, String title, Main main) {
    		JFrame frame = new JFrame(title);
     
    		frame.setPreferredSize(new Dimension (width, height));
    		frame.setMaximumSize(new Dimension (width, height));
    		frame.setMinimumSize(new Dimension (width, height));
     
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setResizable(false);
    		frame.setLocationRelativeTo(null);
    		frame.add(main);
    		frame.setVisible(true);
    		main.start();
     
    	}
     
    }

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: rotating

    What do you see when you execute the program?
    Why are there two loops that iterate 360 times?
    The iteration of a loop will be much faster than the eye can see.

    Note: Window is the name of a java SE class in the java.awt package. That is a poor choice for your class's name.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 0
    Last Post: February 26th, 2012, 12:57 PM
  2. Replies: 0
    Last Post: August 6th, 2011, 01:07 AM
  3. rotating a rectangular image
    By ighor10 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: October 19th, 2010, 06:12 PM
  4. Problem in rotating and moving image at the same time
    By SlimShady in forum What's Wrong With My Code?
    Replies: 10
    Last Post: June 17th, 2010, 02:33 PM