1 Replies - 14462 Views - Last Post: 14 June 2015 - 03:00 PM

#1 CasiOo   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1577
  • View blog
  • Posts: 3,551
  • Joined: 05-April 11

Rotating fan

Posted 30 June 2012 - 06:13 AM

Description: Copy the three classes into their own file and run the code :)How to make a rotating fan in swing
import java.awt.BorderLayout;

import javax.swing.JFrame;


public class AppFrame extends JFrame {

	public static void main(String[] args) {
		new AppFrame();
	}

	public AppFrame() {
		super();
		
		add(new AppPanel(), BorderLayout.CENTER);
		
		setVisible(true);
		pack();
		setDefaultCloseOperation(EXIT_ON_CLOSE);
	}
}


import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JPanel;
import javax.swing.Timer;


public class AppPanel extends JPanel implements ActionListener {
	private Fan fan;

	public AppPanel() {
		setPreferredSize(new Dimension(500, 500));
		fan = new Fan(250, 250);
		new Timer(16, this).start(); //Update our animations every 16ms
	}
	
	@Override
	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		fan.draw(g);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		fan.update(); //Let the fan move
		repaint(); //Draw the changes
	}
}


import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Polygon;
import java.awt.RenderingHints;

public class Fan {
	private final int triWidth = 20, triHeight = 40; //Wing triangle variables
	private final int arcWidth = 20, arcHeight = 10; //Wing arc variables

	private int centerX, centerY; //The center of the fan
	private double angel; //Angel in radians

	public Fan(int centerX, int centerY) {
		this.centerX = centerX;
		this.centerY = centerY;
	}
	
	public void update() {
		angel = addRad(angel, 0.1);
	}
	
	private double addRad(double current, double addition) {
		double value = current + addition;
		
		if (value >= 2 * Math.PI) 
			value -= 2 * Math.PI;
		return value;
	}

	public void draw(Graphics g) {
		Graphics2D gx = (Graphics2D) g;
		//Make it look a little prettier
		gx.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
		
		//Setting up triangle.
		//We only want to do the calculations for the top wing. We rotate the top wing to get the other four wings.
		int[] x = { centerX + triWidth / 2, centerX - triWidth / 2, centerX };
		int[] y = { centerY - triHeight, centerY - triHeight, centerY };
		Polygon triangle = new Polygon(x, y, x.length);
		
		//Rotate 'angel' radians around the center
		gx.rotate(angel, centerX, centerY);
		
		for (int i = 0; i < 4; i++) {
			//Draw the wing arc (top)
			//Start with an angel of 0 for starting horizontal left to right. Let the arc have an angel of 180
			gx.fillArc(centerX - triWidth / 2, centerY - triHeight - arcHeight / 2, arcWidth, arcHeight, 0, 180);

			//Draw the wing triangle
			gx.fillPolygon(triangle);
			
			//Rotate on top of the existing rotation. Rotate 45 degrees
			gx.rotate(Math.PI / 2, centerX, centerY);
		}
	}
}



Is This A Good Question/Topic? 1
  • +

Replies To: Rotating fan

#2 ChrisNt   User is offline

  • cute bug

Reputation: 262
  • View blog
  • Posts: 896
  • Joined: 31-July 13

Re: Rotating fan

Posted 14 June 2015 - 03:00 PM

Lovely :chinese:

P.S

In class AppFrame we should also import BorderLayout
import java.awt.BorderLayout;



This post has been edited by ChrisNt: 14 June 2015 - 03:03 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1