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-18-2011, 09:20 PM   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 with school coding

I have this code.
Code:
/**
 * @(#)chapter9a.java
 *
 * chapter9a Applet application
 *
 * @author 
 * @version 1.00 2011/11/4
 */
 
import java.awt.*;
import java.awt.event.*;
import java.applet.*;

public class chapter9a extends java.applet.Applet implements AdjustmentListener,ItemListener{
	
	Graphics screen;
	Scrollbar width, height, red, blue, green;
	Label widthL, heightL, blueL, redL, greenL,outlineL;
	int widthI, heightI, blueI, redI, greenI;
	Checkbox outline;
	String yon;
	Color clr;
	
	public void init() {
		
		setLayout(null);
		
		width=new Scrollbar(0,55,10,10,110);
		height=new Scrollbar(0,55,10,10,110);
		red=new Scrollbar(0,255,10,0,255);
		blue=new Scrollbar(0,255,10,0,255);
		green=new Scrollbar(0,0,10,0,255);
		widthL=new Label("Width");
		heightL=new Label("Height");
		redL=new Label("Red");
		blueL=new Label("Blue");
		greenL=new Label("Green");
		outline=new Checkbox("Outline",null,false);
		outlineL=new Label("Outline");
		
		width.addAdjustmentListener(this);
		height.addAdjustmentListener(this);
		red.addAdjustmentListener(this);
		blue.addAdjustmentListener(this);
		green.addAdjustmentListener(this);
		outline.addItemListener(this);
		
		width.setBounds(100,10,150,20);
		height.setBounds(100,40,150,20);
		red.setBounds(320,10,200,20);
		blue.setBounds(320,40,200,20);
		green.setBounds(320,70,200,20);
		widthL.setBounds(10,10,100,20);
		heightL.setBounds(10,40,100,20);
		redL.setBounds(260,10,50,20);
		blueL.setBounds(260,40,50,20);
		greenL.setBounds(260,70,50,20);
		outline.setBounds(530,10,10,10);
		outlineL.setBounds(530,30,50,20);
		
		add(width);
		add(height);
		add(red);
		add(blue);
		add(green);
		add(widthL);
		add(heightL);
		add(redL);
		add(blueL);
		add(greenL);
		add(outline);
		add(outlineL);
		
	}
	
	public void adjustmentValueChanged(AdjustmentEvent event) {
		
		widthI=width.getValue();
		heightI=height.getValue();
		redI=red.getValue();
		blueI=blue.getValue();
		greenI=green.getValue();
		repaint();
		
	}
	
	public void itemStateChanged(ItemEvent event) {
		
		if (outline.getState()==true) {
			yon="yes";
		}
		else if (outline.getState()==false) {
			yon="no";
		}
		
	}
	
	public void paint(Graphics screen) {
		//circle
		widthI=width.getValue();
		heightI=height.getValue();
		redI=red.getValue();
		blueI=blue.getValue();
		greenI=green.getValue();
		
		//outline
		outline.getState();
		if (outline.getState()==true) {
			yon="yes";
		}
		else if (outline.getState()==false) {
			yon="no";
		}
		
		if (yon=="yes") {
			screen.drawOval(199,199,(widthI+1),(heightI+1));
		}
		else {
			
		}
		
		//circle continued
		Color clr=new Color(redI,greenI,blueI);
		screen.setColor(clr);
		screen.fillOval(200,200,widthI,heightI);
		
	}
}
and i want it so when u press the checkbox it automatically puts the outline around the circle without having to press anything else. right now i would have to click the checkbox and then press one of the scrollbars to make the outline appear. Any help would be great. Thanks.
pineappelle is offline   Reply With Quote
Old 11-18-2011, 09:53 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,650
Thanks: 4
Thanked 2,451 Times in 2,420 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
Have you tried to repaint from the itemStateChanged? You should be working with the event as well, not the outline. Somewhat irrelevant though since you are relying on paint to perform the work, so you may as well call a simple repaint.
Also, this won't work: if (yon=="yes"). yon is a string, you cannot compare strings this way. That should be comparing to .equals, but I don't really see a need for this variable at all since you can just detect true/false states otherwise.
Fou-Lu is offline   Reply With Quote
Old 11-19-2011, 03:16 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
Have you tried to repaint from the itemStateChanged? You should be working with the event as well, not the outline. Somewhat irrelevant though since you are relying on paint to perform the work, so you may as well call a simple repaint.
Also, this won't work: if (yon=="yes"). yon is a string, you cannot compare strings this way. That should be comparing to .equals, but I don't really see a need for this variable at all since you can just detect true/false states otherwise.
that actually did work. So i dont know why u dont think it would. i updated the code so it could do more things

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 {
			
		}
		
	}
}
it works like this. but the dance funtion won't work. If anyone can tell why that would be amazing. Thanks
pineappelle is offline   Reply With Quote
Old 11-19-2011, 05:30 AM   PM User | #4
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,650
Thanks: 4
Thanked 2,451 Times in 2,420 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
Yes sorry I should be more specific.
== will not work consistently for a string. That equality works because Java uses an optimization technique for its immutable objects. This will not work consistently if you are wanting to compare a value. For an example:
PHP Code:
String s1 = new String("yes");
String s2 = new String("yes"); // This forces a new object
if (s1 == s2)
{
    
System.out.println("s1 and s2 share the same MEMORY");
}
else if (
s1.equals(s2))
{
    
System.out.println("s1 and s2 share the same VALUE");
}
if (
s1 == "yes")
{
    
System.out.println("s1 shares the same memory as constant yes"); // no, this will never show
}
else if (
s1.equals("yes"))
{
     
System.out.println("s1 has the value of yes"); // yes this is fine

The above will state that they share the same value, not the same memory (so == does not equal for the two objects).

As for your second part, that you have a new thread for, so I'll get into that there.
Fou-Lu is offline   Reply With Quote
Reply

Bookmarks

Tags
checkbox, java, outline, programming, school

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 11:46 AM.


Advertisement
Log in to turn off these ads.