The Artima Developer Community
Sponsored Link

Java Answers Forum
if statements

4 replies on 1 page. Most recent reply: Mar 21, 2002 1:46 PM by Sarah

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 4 replies on 1 page
Sarah

Posts: 4
Nickname: mbonfyre
Registered: Mar, 2002

if statements Posted: Mar 21, 2002 12:29 PM
Reply to this message Reply
Advertisement
I am new to java and am having trouble with an if statement, my code is:
if (mouseIn ==true) {
g.setColor(Color.red);
g.fillRect(23,23,10,57);//L
g.fillRect(23,70,33,10);}
else {
g.setColor(Color.blue);
g.fillRect(23,23,10,57);//L
g.fillRect(23,70,33,10);
}

I get a compile fail message : undefined variable: mouseIn

I do not reaaly understand what is going on - can someone enlighten me?

Many thanks


Kishori Sharan

Posts: 211
Nickname: kishori
Registered: Feb, 2002

Re: if statements Posted: Mar 21, 2002 1:18 PM
Reply to this message Reply
Where did you declare <b>mouseIn</b> variable. If you post whole code then we can tell you something about this error.

Sarah

Posts: 4
Nickname: mbonfyre
Registered: Mar, 2002

Re: if statements Posted: Mar 21, 2002 1:36 PM
Reply to this message Reply
Here goes (please be gentle,but acurate with your critisims, I'm a newbie to java)

import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;

public class try1abcd extends Applet implements AdjustmentListener {
private
private Panel bottomPanel;

private Scrollbar scrollX;
private Scrollbar scrollY;
private Scrollbar scrollBig;
private Scrollbar scrollWide;
int startX = 110;
int startY = 170;
int big = 80;
int wide = 160;

public void init() {

scrollX = new Scrollbar(Scrollbar.HORIZONTAL,100,1,0,200);
scrollY = new Scrollbar(Scrollbar.VERTICAL,100,1,0,200);
add(scrollX);
add(scrollY);
scrollX.addAdjustmentListener(this);
scrollY.addAdjustmentListener(this);
scrollBig = new Scrollbar(Scrollbar.HORIZONTAL,50,1,0,200);
scrollWide = new Scrollbar(Scrollbar.VERTICAL,100,1,0,200);
add(scrollBig);
add(scrollWide);
scrollBig.addAdjustmentListener(this);
scrollWide.addAdjustmentListener(this);
setLayout(new BorderLayout() );
bottomPanel = new Panel();
add("South",bottomPanel);
bottomPanel.setBackground(Color.yellow);
bottomPanel.add(scrollX);
bottomPanel. add(scrollY);
bottomPanel.add(scrollBig);
bottomPanel.add(scrollWide);
}

public void adjustmentValueChanged(AdjustmentEvent e) {
startX = scrollX.getValue();
startY = scrollY.getValue();
big = scrollBig.getValue();
wide = scrollWide.getValue();
repaint();
}

public void paint(Graphics g) {
//g.drawString("startX Value is "+startX,10,50);
//g.drawString("startY Value is "+startY,10,80);

//g.drawRect(startX,startY,big,wide);example

g.setColor(Color.blue);//period
g.fillOval(70,70,10,10);
g.setColor(Color.green);
g.fillArc(90,20,40,60,45,280);//Outer Arc for E
g.setColor(Color.lightGray);
g.fillArc(100,29,20,40,45,360);//Inner Arc for E
g.setColor(Color.green);
g.fillRect(100,42,17,10);//Bar for E
g.setColor(Color.orange);//period
g.fillOval(140,70,10,10);
g.setColor(Color.yellow);
g.fillArc(160,20,40,60,45,280);//Outer Arc for C
g.setColor(Color.lightGray);
g.fillArc(170,29,20,40,45,360);//Inner Arc for C
g.setColor(Color.magenta);//period
g.fillOval(210,70,10,10);
g.setColor(Color.blue);
g.fillArc(230,20,40,60,45,280);//Outer Arc for E
g.setColor(Color.lightGray);
g.fillArc(240,29,20,40,45,360);//Inner Arc for E
g.setColor(Color.blue);
g.fillRect(240,42,17,10);//Bar for E
g.setColor(Color.red);//period
g.fillOval(280,70,10,10);

g.setColor(Color.green);//colours moveable rectangle
g.fillRect(startX,startY,big,wide);//gives co ords for starting point

g.setColor(Color.black);
g.drawLine(110,170,150,110);//tower
g.drawLine(150,110,190,170);//tower
g.drawRect(110,170,80,160);//main body

g.setColor(Color.white);
g.fillOval(130,180,40,40);//clock face
g.setColor(Color.black);
g.drawLine(150,200,160,190);//Big Hand
g.drawLine(154,190,160,190);
g.drawLine(160,190,160,195);

g.drawLine(140,200,150,200);//Little Hand
g.drawLine(140,200,143,197);
g.drawLine(140,200,143,203);

g.setColor(Color.red);
g.fillRect(130,270,40,60);//disk
g.setColor(Color.black);
g.fillRect(132,272,3,4);
g.fillRect(163,272,3,4);
g.setColor(Color.gray);
g.fillRect(140,312,23,19);//slide
g.setColor(Color.black);
g.fillRect(143,317,6,9);


g.setColor(Color.red);
g.drawString("LECE, Get London Computing",80,350);

if (mouseIn ==true) {
g.setColor(Color.red);
g.fillRect(23,23,10,57);//L
g.fillRect(23,70,33,10);}
else {
g.setColor(Color.blue);
g.fillRect(23,23,10,57);//L
g.fillRect(23,70,33,10);
}
}
}

Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: if statements Posted: Mar 21, 2002 1:38 PM
Reply to this message Reply
you need to either define a boolean variable called mouseIn in the method your code is in or declare it as a class variable if used other methods and you need a global reference.

boolean mouseIn = false;
or
boolean mouseIn = true;

Sarah

Posts: 4
Nickname: mbonfyre
Registered: Mar, 2002

Re: if statements Posted: Mar 21, 2002 1:46 PM
Reply to this message Reply
thanks for that, it has compiled now, BUT I have the problem that it does not do what I thought it would.

I thought that the if statement would make the letter 'L' change from red to blue when the mouse passed over it, but alas the letter 'L' jut appears blue.

Can you point me in the right direction?

thanks again

Flat View: This topic has 4 replies on 1 page
Topic: voice chatting Previous Topic   Next Topic Topic: How to call a method inside a class in a Jar file

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use