...

View Full Version : Help will a little dancing circle.



pineappelle
11-19-2011, 03:19 AM
i have this 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?

Fou-Lu
11-19-2011, 05:33 AM
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.

pineappelle
11-19-2011, 06:00 AM
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.


/**
* @(#)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.

Fou-Lu
11-19-2011, 04:33 PM
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.

pineappelle
11-19-2011, 06:54 PM
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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum