Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / April 2006

Tip: Looking for answers? Try searching our database.

Newbie help needed with Applet

Thread view: 
CelticFC - 05 Apr 2006 12:57 GMT
The following is code I have written for an Applet. The code is very
large and cumbersome (I am not that confident with Arrays as I am very
new to this).
However the applet works fine and does everything I want it to do
except for one crucial piece. I have tried over the last few days to
get this part working but with no success the following is the part
that does not work:

//This is the solution for the puzzle based on points within the grid
that pawns are placed

           if(x >= 90 && x <= 130 && y >= 10 && y <= 50){

           if(x >= 250 && x <= 290 && y >= 10 && y <= 50){

           if(x >= 90 && x <= 130 && y >= 50 && y <= 90){

           if(x >= 170 && x <= 210 && y >= 50 && y <= 90){

           if(x >= 10 && x <= 50 && y >= 90 && y <= 130){

           if(x >= 50 && x <= 90 && y >= 90 && y <= 130){

           if(x >= 250 && x <= 290 && y >= 130 && y <= 170){

           if(x >= 50 && x <= 90 && y >= 170 && y <= 210){

           if(x >= 210 && x <= 250 && y >= 210 && y <= 250){

           if(x >= 290 && x <= 350 && y >= 210 && y <= 250){

           if(x >= 10 && x <= 50 && y >= 250 && y <= 290){

           if(x >= 130 && x <= 170 && y >= 250 && y <= 290){

           if(x >= 210 && x <= 250 && y >= 290 && y <= 350){

           if(x >= 290 && x <= 350 && y >= 290 && y <= 350){

           message = "Congratulations!";
           message1 = "You solved it";
           pawncount = 14;
           }
           }
           }
           }
           }
           }
           }
           }
           }
           }
           }
           }
           }
           }

Could anybody point me in the right direction or perhaps have a look at
the code and fix it. As I said it is not for lack of trying to get it
to work, I just lack the knowledge although I am learning every day
I would be very grateful for any help.
Here is the full code;

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

public class MyPawns extends Applet implements
MouseListener,ActionListener,
            MouseMotionListener{

                public MyPawns(){
                    points = new ArrayList();
                    pawncount = 0;
                    pawncount1 = 14;
                    message = "";
                    message1 = "";
                    message2 = "";
                    title = new Font ("Arial", Font.BOLD, 18);
                    title1 = new Font ("Arial", Font.BOLD, 12);

                }

    private Point square1, square2, square3, square4, square5, square6,
square7,square8,square9,

square10,square11,square12,square13,square14,square15,square16,square17,square18,square19,

square20,square21,square22,square23,square24,square25,square26,square27,square28,square29,
                 square30,square31,square32,mouse;
    private int select;
    private Image pawn;
    private Point icon;
    private ArrayList points;
    private int x;
    private int y;
    private int pawncount;
    private int pawncount1,pawncount2;
    private Button undoButton;
    private String message,message1,message2;
    private Font title;
    private Font title1;

    public void init(){
        setBackground(new Color(210,255,210));
        this.addMouseMotionListener(this);
        this.addMouseListener(this);
        select = 0;
        square1=new Point(50,10);
        square2=new Point(130,10);
        square3=new Point(210,10);
        square4=new Point(290,10);
        square5=new Point(10,50);
        square6=new Point(90,50);
        square7=new Point(170,50);
        square8=new Point(250,50);
        square9=new Point(50,90);
        square10=new Point(130,90);
        square11=new Point(210,90);
        square12=new Point(290,90);
        square13=new Point(10,130);
        square14=new Point(90,130);
        square15=new Point(170,130);
        square16=new Point(250,130);
        square17=new Point(50,170);
        square18=new Point(130,170);
        square19=new Point(210,170);
        square20=new Point(290,170);
        square21=new Point(10,210);
        square22=new Point(90,210);
        square23=new Point(170,210);
        square24=new Point(250,210);
        square25=new Point(50,250);
        square26=new Point(130,250);
        square27=new Point(210,250);
        square28=new Point(290,250);
        square29=new Point(10,290);
        square30=new Point(90,290);
        square31=new Point(170,290);
        square32=new Point(250,290);
        undoButton = new Button("Undo");
       add(undoButton);
       undoButton.addActionListener(this);
        mouse= new Point();
        icon = new Point();
        pawn = getImage(getCodeBase(), "pawn.gif");
       }

//This allows the user to be able to undo any pawn placed wrongly on
the grid
//It also resets the pawncount by -1 for every pawn taken off

   public void actionPerformed(ActionEvent actionevent){
       if(actionevent.getSource() == undoButton){
       int a = points.size();
       int b = a - 1;
       points.remove(b);
       a = points.size();
       if(a == 0){

          pawncount = 0;
       }
           repaint();
       }
   }

   public void paint (Graphics g){
       drawBox(g);
       g.fillRect(square1.x, square1.y,40,40);
       g.fillRect(square2.x, square2.y,40,40);
       g.fillRect(square3.x, square3.y,40,40);
       g.fillRect(square4.x, square4.y,40,40);
       g.fillRect(square5.x, square5.y,40,40);
       g.fillRect(square6.x, square6.y,40,40);
       g.fillRect(square7.x, square7.y,40,40);
       g.fillRect(square8.x, square8.y,40,40);
       g.fillRect(square9.x, square9.y,40,40);
       g.fillRect(square10.x, square10.y,40,40);
       g.fillRect(square11.x, square11.y,40,40);
       g.fillRect(square12.x, square12.y,40,40);
       g.fillRect(square13.x, square13.y,40,40);
       g.fillRect(square14.x, square14.y,40,40);
       g.fillRect(square15.x, square15.y,40,40);
       g.fillRect(square16.x, square16.y,40,40);
       g.fillRect(square17.x, square17.y,40,40);
       g.fillRect(square18.x, square18.y,40,40);
       g.fillRect(square19.x, square19.y,40,40);
       g.fillRect(square20.x, square20.y,40,40);
       g.fillRect(square21.x, square21.y,40,40);
       g.fillRect(square22.x, square22.y,40,40);
       g.fillRect(square23.x, square23.y,40,40);
       g.fillRect(square24.x, square24.y,40,40);
       g.fillRect(square25.x, square25.y,40,40);
       g.fillRect(square26.x, square26.y,40,40);
       g.fillRect(square27.x, square27.y,40,40);
       g.fillRect(square28.x, square28.y,40,40);
       g.fillRect(square29.x, square29.y,40,40);
       g.fillRect(square30.x, square30.y,40,40);
       g.fillRect(square31.x, square31.y,40,40);
       g.fillRect(square32.x, square32.y,40,40);
       g.setFont(title);
       g.drawString(message, 350, 300);
       g.drawString(message1, 350, 320);
       g.drawString(message2, 350, 340);
       for(int i = 0; i < points.size(); i++){
           icon = (Point)points.get(i);
           g.drawImage(pawn, icon.x, icon.y, 25, 25, this);
       }
       g.drawImage(pawn,175,135,25,25,this);
       g.drawImage(pawn,135,175,25,25,this);
       g.setFont(title);
       g.drawString("The Puzzle", 350, 50);
       g.setFont(title1);
       g.drawString("Two pawns are placed on the centre of a
chessboard.",350,70);
       g.drawString("Now place the remaining fourteen pawns (16 in
all)",350,90);
       g.drawString("So that no three shall be in a straight line in
any",350,110);
       g.drawString("possible direction.", 350, 130);
       g.setFont(title);
       g.drawString("Click A Square To Place A Pawn", 350, 170);
       g.setFont(title1);
       g.drawString("At any time during the game, you can click the
Undo.",350,190);
       g.drawString("button to rectify any mistakes.",350,210);
    }

//required for the interface

public void mouseDragged(MouseEvent e){}
public void mouseMoved(MouseEvent e){}
public void mouseClicked(MouseEvent event){}
public void mouseReleased(MouseEvent event){}
public void mouseEntered(MouseEvent event){}
public void mouseExited(MouseEvent event){}

//This ensures a user cannot place a pawn outwith the grid(board)
//It also starts the pawncount

 public void mousePressed(MouseEvent mouseevent){
       setBackground(new Color(210,255,210));
       x = mouseevent.getX();
       y = mouseevent.getY();
       if(x > 19 && x < 312 && y > 19 && y < 312){
           pawncount = pawncount + 1;
           points.add(mouseevent.getPoint());

//Keeps a count of the number of pawns entered and returns error
message if pawncount > 14

           if(pawncount > pawncount1){

           setBackground(Color.red);
           message = "Too Many Pawns!You can only place 14";
           message1 = "Hit Reset to play again";

           pawncount1 = 14;
           }

//This ensures that no pawn can be placed on the same line as the
starting 2 pawns
//It will give an error message as well as turning the background
red(stop)

           if(x >= 10 && x <= 50 && y >= 290 && y <= 330){

           setBackground(Color.red);
           message = "A Pawn Can't be placed here";
           message1 = "This line already has 2 Pawns";
           message2 = "Hit Reset to play again";
           pawncount = 15;
           }

           if(x >= 50 && x <= 90 && y >= 250 && y <= 290){

           setBackground(Color.red);
           message = "A Pawn Can't be placed here";
           message1 = "This line already has 2 Pawns";
           message2 = "Hit Reset to play again";
           pawncount = 15;
           }

           if(x >= 90 && x <= 130 && y >= 210 && y <= 250){

           setBackground(Color.red);
           message = "A Pawn Can't be placed here";
           message1 = "This line already has 2 Pawns";
           message2 = "Hit Reset to play again";
           pawncount = 15;
           }

           if(x >= 210 && x <= 250 && y >= 90 && y <= 130){

           setBackground(Color.red);
           message = "A Pawn Can't be placed here";
           message1 = "This line already has 2 Pawns";
           message2 = "Hit Reset to play again";
           pawncount = 15;
           }

           if(x >= 250 && x <= 290 && y >= 50 && y <= 90){

           setBackground(Color.red);
           message = "A Pawn Can't be placed here";
           message1 = "This line already has 2 Pawns";
           message2 = "Hit Reset to play again";
           pawncount = 15;
           }

           if(x >= 290 && x <= 350 && y >= 10 && y <= 50){

           setBackground(Color.red);
           message = "A Pawn Can't be placed here";
           message1 = "This line already has 2 Pawns";
           message2 = "Hit Reset to play again";
           pawncount = 15;
           }

//This is the solution for the puzzle based on points within the grid
that pawns are placed

           if(x >= 90 && x <= 130 && y >= 10 && y <= 50){

           if(x >= 250 && x <= 290 && y >= 10 && y <= 50){

           if(x >= 90 && x <= 130 && y >= 50 && y <= 90){

           if(x >= 170 && x <= 210 && y >= 50 && y <= 90){

           if(x >= 10 && x <= 50 && y >= 90 && y <= 130){

           if(x >= 50 && x <= 90 && y >= 90 && y <= 130){

           if(x >= 250 && x <= 290 && y >= 130 && y <= 170){

           if(x >= 50 && x <= 90 && y >= 170 && y <= 210){

           if(x >= 210 && x <= 250 && y >= 210 && y <= 250){

           if(x >= 290 && x <= 350 && y >= 210 && y <= 250){

           if(x >= 10 && x <= 50 && y >= 250 && y <= 290){

           if(x >= 130 && x <= 170 && y >= 250 && y <= 290){

           if(x >= 210 && x <= 250 && y >= 290 && y <= 350){

           if(x >= 290 && x <= 350 && y >= 290 && y <= 350){

           message = "Congratulations!";
           message1 = "You solved it";
           pawncount = 14;
           }
           }
           }
           }
           }
           }
           }
           }
           }
           }
           }
           }
           }
           }

       }

       repaint();
   }

public void drawBox(Graphics g){
    for(int i=10;i<=350;i+=40){
   g.drawLine(i,10,i,330);
   g.drawLine(10,i,330,i);
   }
  }
 }
Remon van Vliet - 05 Apr 2006 17:13 GMT
> The following is code I have written for an Applet. The code is very
> large and cumbersome (I am not that confident with Arrays as I am very
[quoted text clipped - 377 lines]
>   }
>  }

I strongly suggest you get comfortable with arrays then, a simple typo could
really cost you a lot of debugging time. Arrays arent that complicated to
grasp so if you read up on the topic you may find your problems fixing
themselves. Your code is a bit too long to read through though :)
Monique Y. Mudama - 05 Apr 2006 20:20 GMT
> I strongly suggest you get comfortable with arrays then, a simple
> typo could really cost you a lot of debugging time. Arrays arent
> that complicated to grasp so if you read up on the topic you may
> find your problems fixing themselves. Your code is a bit too long to
> read through though :)

Yes, so why did you quote it in its entirety when responding?

Signature

monique

Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html

Oliver Wong - 05 Apr 2006 21:13 GMT
> The following is code I have written for an Applet. The code is very
> large and cumbersome (I am not that confident with Arrays as I am very
[quoted text clipped - 3 lines]
> get this part working but with no success the following is the part
> that does not work:

[Snipped 382 lines of code]

   Okay... but what's wrong with it?

   - Oliver
CelticFC - 06 Apr 2006 00:23 GMT
The applet will not display the "Winner" message when the 14 pawns are
entered at the specified points. This is the part that does not work
correctly. The aim is when 14 pawns are placed at these specific points
the "Winner" message will show, in order to inform the user that the
puzzle has been solved.I am lost on how to code this part. If you look
at my first post the part that does not work is quoted before the very
LARGE code.

Hope you can help
CelticFC
Oliver Wong - 06 Apr 2006 15:54 GMT
> The applet will not display the "Winner" message when the 14 pawns are
> entered at the specified points. This is the part that does not work
[quoted text clipped - 3 lines]
> at my first post the part that does not work is quoted before the very
> LARGE code.

   Could it be because you wrote

<code>
message = "Congratulations!";
message1 = "You solved it";
</code>

instead of

<code>
message = "Winner";
message1 = "";
</code>
?

   I'm teasing, of course, but also trying to point out that as a
programmer, it's crucial to be precise about what you mean. A computer will
literally obey whatever you type, even if that's not what you meant.

   Anyway, I suspect the problem is here:

<code>
if(x >= 90 && x <= 130 && y >= 10 && y <= 50){
if(x >= 250 && x <= 290 && y >= 10 && y <= 50){
if(x >= 90 && x <= 130 && y >= 50 && y <= 90){
[etc.]
</code>

   There's no way that x can simultaneously be between 90 and 130, and also
between 250 and 290. I'm guessing instead of having a single variable 'x',
you intended to have 14 of them, each one representing the x-coordinate of a
different pawn.

   If you really hate arrays, then just declare 14 different variables,
e.g. x1, x2, x3, etc. Do the same for y (i.e. y1, y2, y3, etc.)

   - Oliver
CelticFC - 06 Apr 2006 16:09 GMT
Thanks Oliver. God how simple was that. Never even thought of that. Oh
well I will have to do a LOT more reading.
Once again thanks.
CelticFC.
Roedy Green - 06 Apr 2006 00:19 GMT
> if(x >= 90 && x <= 130 && y >= 10 && y <= 50){

rather than hard coding that you could
invent

Box[] boxes = new Box[] {  new Box( 90, 130, 10, 50), .... };
or read your data is from a CSV file 4 numbers per line. See
http://mindprod.com/jgloss/csv.html

then your code become

for ( Box box: boxes )
{
  if (box.x1 <= x && x <= box.x2 && box.y1 <= y && y <= box.y2) ...
}
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.