The Artima Developer Community
Sponsored Link

Java Answers Forum
ok, desperate for help with compile errors

2 replies on 1 page. Most recent reply: Mar 21, 2002 6:00 PM by Matt Gerrans

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 2 replies on 1 page
Sarah

Posts: 4
Nickname: mbonfyre
Registered: Mar, 2002

ok, desperate for help with compile errors Posted: Mar 21, 2002 3:18 PM
Reply to this message Reply
Advertisement
written in edit plus - 2 errors
1. 130: '}' expected
}
^
2. 132: Statement expected.
public void adjustmentChange (AdjustEvent e) {
^

I cannot work out what I have done wrong, Could one of you guys take a look and put a newbie to Java out of her misery?

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

public class new1 extends Applet implements AdjustmentListener, ItemListener{
private boolean mouseIn = true;
private Panel bottomPanel;
private Choice choosecolour;
private Color colourX;
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() {
choosecolour = new Choice();
choosecolour.add("Cyan");
choosecolour.add("Blue");
choosecolour.add("Yellow");
add(choosecolour);
choosecolour.addItemListener(this);

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);

}

public void itemStateChanged(ItemEvent e) {
if(e.getSource()==choosecolour) {
switch(colour.getSelectedIndex()) {
case 0:colourX = Color.red;break;
case 1:colourX = Color.green;break;
case 2:colourX = Color.blue;break;
}
}
public void adjustmentChange (AdjustEvent e) {
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);
}

}
}


Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: ok, desperate for help with compile errors Posted: Mar 21, 2002 5:45 PM
Reply to this message Reply
Well, it looks like you have a lot of problems (I see at least one undeclared variable compiler error that will appear after this one is resolved); if I were you, I'd try compiling more often between spurts of cranking out code. That way, it is easier to find the cause of a particular error (since you will know what you just changed) and there should be fewer of them per compile.

Now, the compiler error you are referring to is because the method itemStateChanged() (or one of its constructs) is missing a closing curly brace: You have three opening curlies (one to start the method, one to start the if clause and one for the switch), but only two closing). This would be much more obvious if you used the superiour coding style of matching curly braces on separate lines aligned by column:
public void itemStateChanged(ItemEvent e)
{
    if(e.getSource()==choosecolour) 
    {
        switch(colour.getSelectedIndex()) 
        {
            case 0:colourX = Color.red;break;
            case 1:colourX = Color.green;break;
            case 2:colourX = Color.blue;break;
        }
    }
}

Note in the above, that if any of the closing curlies were missing there would be sirens and flashing red lights going off in you head, but when you use the clearly inferiour style of placing the braces all over the place that is more popular with the unwashed masses, it is easier to lose a curly (or even a moe or larry) and still have things look okay:
public void itemStateChanged(ItemEvent e){
    if(e.getSource()==choosecolour){
        switch(colour.getSelectedIndex()){
            case 0:colourX = Color.red;break;
            case 1:colourX = Color.green;break;
            case 2:colourX = Color.blue;break;
        }
}

Also, indentation helps to reveal mismatched braces, but since you didn't use the java formatting tag on your post, we don't know what indentation you originally had.

Finally, if you have a real whiz-bang code editor, like IntelliJ's IDEA editor (http://www.intellij.com/), it immediately shows you matching braces (it also does things like fill in all the necessary parts of ifs, whiles, switches, etc.).

(for those who are offended by my unabashed bashing of conventional curly-placement style, it is only tongue-in-cheek -- I know it is a hopeless battle).

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: ok, desperate for help with compile errors Posted: Mar 21, 2002 6:00 PM
Reply to this message Reply
Here is your code, sans compiler errors:
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class new1 extends Applet implements AdjustmentListener,ItemListener
{
    private boolean mouseIn = true;
    private Panel bottomPanel;
    private Choice choosecolour;
    private Color colourX;
    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()
    {
        choosecolour = new Choice();
        choosecolour.add("Cyan");
        choosecolour.add("Blue");
        choosecolour.add("Yellow");
        add(choosecolour);
        choosecolour.addItemListener(this);
        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);
    }
    public void itemStateChanged(ItemEvent e)
    {
        if(e.getSource() == choosecolour)
        {
            switch(choosecolour.getSelectedIndex())
            {
              case 0:
                  colourX = Color.red;
                  break;
              
              case 1:
                  colourX = Color.green;
                  break;
              
              case 2:
                  colourX = Color.blue;
                  break;
            }
        }
    }
    public void adjustmentChange(AdjustmentEvent e)
    {
        // I added this getGraphics() so your code would
        // compile, but it is better design to do call
        // repaint() and let your paint() method do the
        // appropriate rendering based on the current state
        // of your object(s).
        Graphics g = getGraphics();
 
        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);
        }
    }
}


But it still needs work. In particular, you don't ever modify the mouseIn variable, so it will always draw the same color.

Flat View: This topic has 2 replies on 1 page
Topic: panels Previous Topic   Next Topic Topic: ExcelAccessor Bean Suite

Sponsored Links



Google
  Web Artima.com   

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