Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-19-2011, 03:19 AM   PM User | #1
pineappelle
New Coder

 
Join Date: Jun 2011
Posts: 34
Thanks: 1
Thanked 0 Times in 0 Posts
pineappelle is an unknown quantity at this point
Help will a little dancing circle.

i have this code

Code:
/**
 * @(#)circle.java
 *
 * circle Applet application
 *
 * @author 
 * @version 1.00 2011/11/18
 */
 
import java.awt.*;
import java.awt.event.*;
import java.applet.*;

public class circle extends java.applet.Applet implements AdjustmentListener,ItemListener,ActionListener {
	
	Graphics screen;
	Scrollbar width, height, red, green, blue, xcord, ycord;
	Label widthL, heightL, redL, greenL, blueL, outlineL, xcordL, ycordL;
	int widthI, heightI, redI, greenI, blueI, xcordI, ycordI;
	Checkbox outline;
	Button dance;
	Color clr;
	String yon,danceS;
	
	public void init() {
		
		setLayout(null);
		
		width=new Scrollbar(0,105,1,10,210);
		height=new Scrollbar(0,105,1,10,210);
		red=new Scrollbar(0,255,1,0,255);
		green=new Scrollbar(0,0,1,0,255);
		blue=new Scrollbar(0,255,1,0,255);
		widthL=new Label("Width");
		heightL=new Label("Height");
		redL=new Label("Red");
		greenL=new Label("Green");
		blueL=new Label("Blue");
		outline=new Checkbox("",null,false);
		outlineL=new Label("Outline");
		xcord=new Scrollbar(0,0,1,200,400);
		ycord=new Scrollbar(0,0,1,200,400);
		xcordL=new Label("X");
		ycordL=new Label("Y");
		dance=new Button("Dance!");
			
		heightL.setBounds(10,10,50,20);
		widthL.setBounds(10,40,50,20);
		height.setBounds(70,10,200,20);
		width.setBounds(70,40,200,20);
		redL.setBounds(280,10,50,20);
		greenL.setBounds(280,40,50,20);
		blueL.setBounds(280,70,50,20);
		red.setBounds(340,10,200,20);
		green.setBounds(340,40,200,20);
		blue.setBounds(340,70,200,20);
		outline.setBounds(610,10,20,20);
		outlineL.setBounds(550,10,50,20);
		xcordL.setBounds(10,70,50,20);
		ycordL.setBounds(10,100,50,20);
		xcord.setBounds(70,70,200,20);
		ycord.setBounds(70,100,200,20);
		dance.setBounds(550,40,50,20);
		
		height.addAdjustmentListener(this);
		width.addAdjustmentListener(this);
		red.addAdjustmentListener(this);
		green.addAdjustmentListener(this);
		blue.addAdjustmentListener(this);
		outline.addItemListener(this);
		xcord.addAdjustmentListener(this);
		ycord.addAdjustmentListener(this);
		dance.addActionListener(this);
		
		add(height);
		add(width);
		add(heightL);
		add(widthL);
		add(red);
		add(green);
		add(blue);
		add(redL);
		add(greenL);
		add(blueL);
		add(outline);
		add(outlineL);
		add(xcord);
		add(ycord);
		add(xcordL);
		add(ycordL);
		add(dance);
		
	}
	
	public void adjustmentValueChanged(AdjustmentEvent event) {
		
		height.getValue();
		width.getValue();
		red.getValue();
		green.getValue();
		blue.getValue();
		xcord.getValue();
		ycord.getValue();
		heightI=height.getValue();
		widthI=width.getValue();
		redI=red.getValue();
		greenI=green.getValue();
		blueI=blue.getValue();
		xcordI=xcord.getValue();
		ycordI=ycord.getValue();
		repaint();
		
	}

	public void itemStateChanged(ItemEvent event) {
		
		if (outline.getState()==true) {
			yon="yes";
		}
		else if (outline.getState()==false) {
			yon="no";
		}
		repaint();
		
	}
	
	public void actionPerformed(ActionEvent event) {
		
		danceS="dance";
	
	}

	public void paint(Graphics screen) {
		
		height.getValue();
		width.getValue();
		red.getValue();
		green.getValue();
		blue.getValue();
		xcord.getValue();
		ycord.getValue();
		heightI=height.getValue();
		widthI=width.getValue();
		redI=red.getValue();
		greenI=green.getValue();
		blueI=blue.getValue();
		xcordI=xcord.getValue();
		ycordI=ycord.getValue();
			
		clr=new Color(redI,greenI,blueI);
		screen.setColor(clr);
		screen.fillOval(xcordI,ycordI,(widthI+1),(heightI+1));
		
		screen.setColor(Color.black);
		if (yon=="yes") {
			screen.drawOval(xcordI,ycordI,widthI,heightI);
		}
		else {
			
		}
	
		if(danceS=="dance") {
		
		xcord.setValue(200);
		ycord.setValue(200);
		for (int dummy=1;dummy<10;dummy=dummy+1){
		}
		xcord.setValue(400);
		ycord.setValue(200);
		for (int dummy=1;dummy<10;dummy=dummy+1){
		}
		xcord.setValue(400);
		ycord.setValue(400);
		for (int dummy=1;dummy<10;dummy=dummy+1){
		}
		xcord.setValue(200);
		ycord.setValue(400);
		for (int dummy=1;dummy<10;dummy=dummy+1){
		}
		xcord.setValue(200);
		ycord.setValue(200);
		danceS="nodance";
		
		}
		else {
			
		}
		
	}
}
and i want to make the circle dance. but the code that i have isnt working. can someone tell me why?
pineappelle is offline   Reply With Quote
Old 11-19-2011, 05:33 AM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
The same problem exists in the previous thread. There is no repaint called.
This won't let it 'dance' though, this will only move the location of x/ycord variables. To make it actually dance, you will need to implement multithreading on the components within it.
Fou-Lu is offline   Reply With Quote
Old 11-19-2011, 06:00 AM   PM User | #3
pineappelle
New Coder

 
Join Date: Jun 2011
Posts: 34
Thanks: 1
Thanked 0 Times in 0 Posts
pineappelle is an unknown quantity at this point
Quote:
Originally Posted by Fou-Lu View Post
The same problem exists in the previous thread. There is no repaint called.
This won't let it 'dance' though, this will only move the location of x/ycord variables. To make it actually dance, you will need to implement multithreading on the components within it.
all i want to do is make it go to four corners of the square. like here is the updated code.

Code:
/**
 * @(#)circle.java
 *
 * circle Applet application
 *
 * @author 
 * @version 1.00 2011/11/18
 */
 
import java.awt.*;
import java.awt.event.*;
import java.applet.*;

public class circle extends java.applet.Applet implements AdjustmentListener,ItemListener,ActionListener {
	
	Graphics screen;
	Scrollbar width, height, red, green, blue, xcord, ycord;
	Label widthL, heightL, redL, greenL, blueL, outlineL, xcordL, ycordL;
	int widthI, heightI, redI, greenI, blueI, xcordI, ycordI;
	Checkbox outline;
	Button dance;
	Color clr;
	String yon,danceS;
	
	public void init() {
		
		setLayout(null);
		
		width=new Scrollbar(0,105,1,10,210);
		height=new Scrollbar(0,105,1,10,210);
		red=new Scrollbar(0,255,1,0,255);
		green=new Scrollbar(0,0,1,0,255);
		blue=new Scrollbar(0,255,1,0,255);
		widthL=new Label("Width");
		heightL=new Label("Height");
		redL=new Label("Red");
		greenL=new Label("Green");
		blueL=new Label("Blue");
		outline=new Checkbox("",null,false);
		outlineL=new Label("Outline");
		xcord=new Scrollbar(0,0,1,200,400);
		ycord=new Scrollbar(0,0,1,200,400);
		xcordL=new Label("X");
		ycordL=new Label("Y");
		dance=new Button("Dance!");
			
		heightL.setBounds(10,10,50,20);
		widthL.setBounds(10,40,50,20);
		height.setBounds(70,10,200,20);
		width.setBounds(70,40,200,20);
		redL.setBounds(280,10,50,20);
		greenL.setBounds(280,40,50,20);
		blueL.setBounds(280,70,50,20);
		red.setBounds(340,10,200,20);
		green.setBounds(340,40,200,20);
		blue.setBounds(340,70,200,20);
		outline.setBounds(610,10,20,20);
		outlineL.setBounds(550,10,50,20);
		xcordL.setBounds(10,70,50,20);
		ycordL.setBounds(10,100,50,20);
		xcord.setBounds(70,70,200,20);
		ycord.setBounds(70,100,200,20);
		dance.setBounds(550,40,50,20);
		
		height.addAdjustmentListener(this);
		width.addAdjustmentListener(this);
		red.addAdjustmentListener(this);
		green.addAdjustmentListener(this);
		blue.addAdjustmentListener(this);
		outline.addItemListener(this);
		xcord.addAdjustmentListener(this);
		ycord.addAdjustmentListener(this);
		dance.addActionListener(this);
		
		add(height);
		add(width);
		add(heightL);
		add(widthL);
		add(red);
		add(green);
		add(blue);
		add(redL);
		add(greenL);
		add(blueL);
		add(outline);
		add(outlineL);
		add(xcord);
		add(ycord);
		add(xcordL);
		add(ycordL);
		add(dance);
		
		danceS="nodance";
		
	}
	
	public void adjustmentValueChanged(AdjustmentEvent event) {
		
		height.getValue();
		width.getValue();
		red.getValue();
		green.getValue();
		blue.getValue();
		xcord.getValue();
		ycord.getValue();
		heightI=height.getValue();
		widthI=width.getValue();
		redI=red.getValue();
		greenI=green.getValue();
		blueI=blue.getValue();
		xcordI=xcord.getValue();
		ycordI=ycord.getValue();
		repaint();
		
	}

	public void itemStateChanged(ItemEvent event) {
		
		if (outline.getState()==true) {
			yon="yes";
		}
		else if (outline.getState()==false) {
			yon="no";
		}
		repaint();
		
	}
	
	public void actionPerformed(ActionEvent event) {
		
		xcord.setValue(200);
		ycord.setValue(200);
		for (int dummy=1;dummy<1;dummy=dummy+1) {
		}
		xcord.setValue(400);
		ycord.setValue(200);
		for (int dummy=1;dummy<2;dummy=dummy+1) {
		}
		xcord.setValue(400);
		ycord.setValue(400);
		for (int dummy=1;dummy<2;dummy=dummy+1) {
		}
		xcord.setValue(200);
		ycord.setValue(400);
		for (int dummy=1;dummy<2;dummy=dummy+1) {
		}
		xcord.setValue(200);
		ycord.setValue(200);
		repaint();
	
	}

	public void paint(Graphics screen) {
		
		height.getValue();
		width.getValue();
		red.getValue();
		green.getValue();
		blue.getValue();
		xcord.getValue();
		ycord.getValue();
		heightI=height.getValue();
		widthI=width.getValue();
		redI=red.getValue();
		greenI=green.getValue();
		blueI=blue.getValue();
		xcordI=xcord.getValue();
		ycordI=ycord.getValue();
			
		screen.setColor(Color.black);
		if (yon=="yes") {
			screen.fillOval((xcordI-5),(ycordI-5),(widthI+10),(heightI+10));
		}
		else {
			
		}	
			
		clr=new Color(redI,greenI,blueI);
		screen.setColor(clr);
		screen.fillOval(xcordI,ycordI,widthI,heightI);
		
	}
}
it moves the circle back to 200,200 but then it doesnt do anything after that.
pineappelle is offline   Reply With Quote
Old 11-19-2011, 04:33 PM   PM User | #4
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
That's the final result for it are, so that's where it sets the value. The loops are quite pointless in there, so those should be removed.
If you want it to kinda animate, you need to repaint between each step and add a timer for it to wait a half second or so. No matter what, you won't get smooth animiation until you have implemented multithreading on the circle.
Fou-Lu is offline   Reply With Quote
Old 11-19-2011, 06:54 PM   PM User | #5
pineappelle
New Coder

 
Join Date: Jun 2011
Posts: 34
Thanks: 1
Thanked 0 Times in 0 Posts
pineappelle is an unknown quantity at this point
Quote:
Originally Posted by Fou-Lu View Post
That's the final result for it are, so that's where it sets the value. The loops are quite pointless in there, so those should be removed.
If you want it to kinda animate, you need to repaint between each step and add a timer for it to wait a half second or so. No matter what, you won't get smooth animiation until you have implemented multithreading on the circle.
yeah but i dont know how to make the timers thats the whole point
pineappelle is offline   Reply With Quote
Reply

Bookmarks

Tags
button, circle, dancing, java, programming

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:35 AM.


Advertisement
Log in to turn off these ads.