Graphics/GUI

  • (2 Pages)
  • +
  • 1
  • 2

29 Replies - 3209 Views - Last Post: 20 November 2009 - 10:05 AM Rate Topic: -----

#1 theautokustomizer  Icon User is offline

  • D.I.C Regular

Reputation: 14
  • View blog
  • Posts: 250
  • Joined: 20-September 09

Graphics/GUI

Posted 09 November 2009 - 05:44 PM

Hello all, I am writing this program, and I am running into a question...
I am supposed to write a program that places four fans in a frame of GridLayout with two rows and two columns... I have got the fan, but I wanted to know if I needed to create four separate fans, or is there some way to draw the same thing four times? I also wanted to know how would I move them if I could just redraw them.... This is what I have...

import java.awt.*;
import javax.swing.*;
import java.util.*;

public class FOURFANS extends JPanel
{
	protected void paintComponent(Graphics c)
	{
		super.paintComponents(c);
		
		//Initialize parameters
		int circleRadius = (int)(Math.min(getWidth(), getHeight()) * 0.5);
		int xCenter = getWidth() / 2;
		int yCenter = getHeight() / 2; 
		int x = xCenter - circleRadius;
		int y = yCenter - circleRadius;
		
		//Draw circle
		c.setColor(Color.black);
		c.drawOval(xCenter - circleRadius, yCenter - circleRadius, 2 * circleRadius, 2 * circleRadius);
		
		c.fillArc(x, y, 2 * circleRadius, 2 * circleRadius, 0, 30);
		c.fillArc(x, y, 2 * circleRadius, 2 * circleRadius, 90, 30);
		c.fillArc(x, y, 2 * circleRadius, 2 * circleRadius, 180, 30);
		c.fillArc(x, y, 2 * circleRadius, 2 * circleRadius, 270, 30);
		
	}
}


And here is my tester:

import java.awt.*;
import javax.swing.*;

public class TESTFOURFANS extends JFrame
{
	public TESTFOURFANS()
	{
		setLayout(new GridLayout(2, 2, 5, 5));
		add(new FOURFANS());
	}
	
	public static void main(String[] args)
	{
		TESTFOURFANS frame = new TESTFOURFANS();
		frame.setSize(400, 200);
		frame.setTitle("FOUR FANS");
		frame.setLocationRelativeTo(null);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);
	}
}


Is This A Good Question/Topic? 0
  • +

Replies To: Graphics/GUI

#2 chaos386  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 28
  • Joined: 27-February 09

Re: Graphics/GUI

Posted 09 November 2009 - 05:53 PM

well this puts four in is it wat your affter?

	public TESTFOURFANS()
	{
		setLayout(new GridLayout(2, 2, 5, 5));
		add(new FOURFANS());
		add(new FOURFANS());
		add(new FOURFANS());
		add(new FOURFANS());
	}


Was This Post Helpful? 1
  • +
  • -

#3 jimdandy75  Icon User is offline

  • D.I.C Regular

Reputation: 37
  • View blog
  • Posts: 311
  • Joined: 30-June 08

Re: Graphics/GUI

Posted 09 November 2009 - 05:56 PM

Very cool! I like how it repaints when you change the size of the window :D :^:
Was This Post Helpful? 1
  • +
  • -

#4 theautokustomizer  Icon User is offline

  • D.I.C Regular

Reputation: 14
  • View blog
  • Posts: 250
  • Joined: 20-September 09

Re: Graphics/GUI

Posted 09 November 2009 - 06:03 PM

View Postjimdandy75, on 9 Nov, 2009 - 04:56 PM, said:

Very cool! I like how it repaints when you change the size of the window :D :^:



TY :D



View Postchaos386, on 9 Nov, 2009 - 04:53 PM, said:

well this puts four in is it wat your affter?

	public TESTFOURFANS()
	{
		setLayout(new GridLayout(2, 2, 5, 5));
		add(new FOURFANS());
		add(new FOURFANS());
		add(new FOURFANS());
		add(new FOURFANS());
	}



Thank you, I didn't know if there was a way to have them redrawn, and show the blades in different postions...like they were moving??

This post has been edited by theautokustomizer: 09 November 2009 - 06:02 PM

Was This Post Helpful? 1
  • +
  • -

#5 sakshamkum  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 19
  • View blog
  • Posts: 232
  • Joined: 09-June 09

Re: Graphics/GUI

Posted 09 November 2009 - 06:04 PM

you can add four fans without writing code for each one.
in your class add two variables x,y and add a constructor to initialize them and in the tester class you you add them in your JPanel. better is that you make a class describing a Fan, its two coordinates and then make four objects andadd them in the order.
regarding yourfan's movemet you can redraw them by usig repaint() but you are adviced to do it in run() method of Thread and rpaint after periodic intervals the way you like.
Was This Post Helpful? 1
  • +
  • -

#6 theautokustomizer  Icon User is offline

  • D.I.C Regular

Reputation: 14
  • View blog
  • Posts: 250
  • Joined: 20-September 09

Re: Graphics/GUI

Posted 09 November 2009 - 06:10 PM

I didn't want movement persay, I just wanted the frame to be redrawn and the blades in different postions....Is that possible? Like to redraw it say four, five, six times??? Or is that dumb?

This post has been edited by theautokustomizer: 09 November 2009 - 06:10 PM

Was This Post Helpful? 0
  • +
  • -

#7 sakshamkum  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 19
  • View blog
  • Posts: 232
  • Joined: 09-June 09

Re: Graphics/GUI

Posted 09 November 2009 - 06:12 PM

yes just reinitialize the objects
Was This Post Helpful? 0
  • +
  • -

#8 theautokustomizer  Icon User is offline

  • D.I.C Regular

Reputation: 14
  • View blog
  • Posts: 250
  • Joined: 20-September 09

Re: Graphics/GUI

Posted 09 November 2009 - 06:13 PM

Thats all I have to do?
Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Graphics/GUI

Posted 09 November 2009 - 06:35 PM

View Posttheautokustomizer, on 9 Nov, 2009 - 05:13 PM, said:

Thats all I have to do?

With a javax.swing.Timer() that updates the x,y of each FOURFANS every n millisec and call back their repaint() method

Would be a lot easier with a simple JPanel with null layout
But I guess your assignment is to used a GridLayout
Was This Post Helpful? 1
  • +
  • -

#10 theautokustomizer  Icon User is offline

  • D.I.C Regular

Reputation: 14
  • View blog
  • Posts: 250
  • Joined: 20-September 09

Re: Graphics/GUI

Posted 09 November 2009 - 06:46 PM

View Postpbl, on 9 Nov, 2009 - 05:35 PM, said:

View Posttheautokustomizer, on 9 Nov, 2009 - 05:13 PM, said:

Thats all I have to do?

With a javax.swing.Timer() that updates the x,y of each FOURFANS every n millisec and call back their repaint() method

Would be a lot easier with a simple JPanel with null layout
But I guess your assignment is to used a GridLayout


It is, but my assignment was just to do the fans, I wanted to know for myself....so thank you very much!!! :)
Just for shits and giggles, what is the syntax for the Timer? And from what I gather the repaint, is just that, it repaints what I am drawing???

This post has been edited by theautokustomizer: 09 November 2009 - 06:49 PM

Was This Post Helpful? 0
  • +
  • -

#11 chaos386  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 28
  • Joined: 27-February 09

Re: Graphics/GUI

Posted 09 November 2009 - 07:11 PM

TESTFOURFANS updated


import java.awt.*;
import javax.swing.*;

public class TESTFOURFANS extends JFrame implements Runnable
{
	   
	   FOURFANS fan1 = new FOURFANS();
	   FOURFANS fan2 = new FOURFANS();
	   FOURFANS fan3 = new FOURFANS();
	   FOURFANS fan4 = new FOURFANS();
	   
	public TESTFOURFANS()
		{
		setLayout(new GridLayout(2, 2, 5, 5));
		add(fan1);
		add(fan2);
		add(fan3);
		add(fan4);
		setVisible(true);
		setSize(400,600);
		run();

		
		}
	public void run(){
	try{
		Thread.sleep(100);
	   
		fan1.moveFan();
		fan2.moveFan();
		fan3.moveFan();
		fan4.moveFan();
		this.repaint();
		
			}catch (Exception e){return;}
		
	run();
	}

	
	public static void main(String[] args)
	{
		TESTFOURFANS frame = new TESTFOURFANS();
		
		frame.setSize(400, 200);
		frame.setTitle("FOUR FANS");
		frame.setLocationRelativeTo(null);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);
	}
}


FOURFANS updated

import java.awt.*;
import javax.swing.*;
import java.util.*;

public class FOURFANS extends JPanel
{
	private static int  n=0;
	private static int	e=90;
	private static int	s=180;
	private static int	w=270;
	
	
	protected void paintComponent(Graphics c)
	{
		super.paintComponents(c);
		
		//Initialize parameters
		int circleRadius = (int)(Math.min(getWidth(), getHeight()) * 0.5);
		int xCenter = getWidth() / 2;
		int yCenter = getHeight() / 2; 
		int x = xCenter - circleRadius;
		int y = yCenter - circleRadius;
		
		//Draw circle
		c.setColor(Color.black);
		c.drawOval(xCenter - circleRadius, yCenter - circleRadius, 2 * circleRadius, 2 * circleRadius);
		
		c.fillArc(x, y, 2 * circleRadius, 2 * circleRadius, n, 30);
		c.fillArc(x, y, 2 * circleRadius, 2 * circleRadius, e, 30);
		c.fillArc(x, y, 2 * circleRadius, 2 * circleRadius, s, 30);
		c.fillArc(x, y, 2 * circleRadius, 2 * circleRadius, w, 30);
	 
	}
	public static void moveFan(){
		n = (n + 20) % 360;
		e = (e + 20) % 360;
		s = (s + 20) % 360;
		w = (w + 20) % 360;
		}

}



hope this is what you wants
Was This Post Helpful? 1
  • +
  • -

#12 theautokustomizer  Icon User is offline

  • D.I.C Regular

Reputation: 14
  • View blog
  • Posts: 250
  • Joined: 20-September 09

Re: Graphics/GUI

Posted 09 November 2009 - 07:22 PM

WOW!!! It looks like that is what I want, but the compiler is complaining about the symbol : method moveFan()
location: class FOURFANS
fan1.moveFan();
on all 4 of the moveFan commands...

This post has been edited by theautokustomizer: 09 November 2009 - 07:28 PM

Was This Post Helpful? 0
  • +
  • -

#13 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Graphics/GUI

Posted 09 November 2009 - 08:05 PM

Swing is NOT thread safe
Use a javax.swing.Timer not a Thread !
Was This Post Helpful? 1
  • +
  • -

#14 theautokustomizer  Icon User is offline

  • D.I.C Regular

Reputation: 14
  • View blog
  • Posts: 250
  • Joined: 20-September 09

Re: Graphics/GUI

Posted 09 November 2009 - 08:10 PM

Like I said, this is new to me... I have no idea how to use a Timer?? I don't need this for my assignment. I am trying to figure it out for my own knowledge... :^: I wanted the code for my twirling fans so I could study it... :P He offered that code, I tried to follow, but got lost on some of it. :crazy:

This post has been edited by theautokustomizer: 09 November 2009 - 08:11 PM

Was This Post Helpful? 0
  • +
  • -

#15 chaos386  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 28
  • Joined: 27-February 09

Re: Graphics/GUI

Posted 09 November 2009 - 08:19 PM

View Posttheautokustomizer, on 9 Nov, 2009 - 07:10 PM, said:

Like I said, this is new to me... I have no idea how to use a Timer?? I don't need this for my assignment. I am trying to figure it out for my own knowledge... :^: I wanted the code for my twirling fans so I could study it... :P He offered that code, I tried to follow, but got lost on some of it. :crazy:


did you up date both classes? i added the moveFan method into your FOURFANS class :)
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2