Sign in to follow this  

[java] I am developing a stick map editor/game in Java. Question

This topic is 2855 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, My name is CoderRyne, and I am currently developing a Java applet, which will soon be a map editor and game. I have run into a problem, however. Basically, when you drag the mouse around, or simply click, no new shapes come out. The entire time, no matter what you do, only one shape appears in the applet. I need the shapes you chose to create to go into the applet every time you click. If you look at public void mouseDragged(MouseEvent me){, near the bottom, that is most likely where the code needs to be edited to make this work. I don't really see why it is not working, so I would appreciate any help. Here is the code: ////////////////START////////////////
import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
import javax.swing.*;

public class stickmin extends Applet implements MouseMotionListener, ActionListener, ItemListener
{
    private int x = -200;
    private int y = -200;
    private int height = 50;
    private int width = 50;
    private Button blackpenButton;
    private Button redpenButton;
    private Button greenpenButton;
    private Button bluepenButton;
    private Button brownpenButton;
    private Button eraserButton;
    private Button clearButton;
    private Button firstsaveButton;
    private int which;
    Choice shape;
    int shapeNbr = 1;
    Choice size;
    Choice filledin;
    Choice collidable;
    
    public void init()
    { 
    resize(500, 500);
    blackpenButton = new Button("Black Pen");
    add(blackpenButton);
    blackpenButton.addActionListener(this);
    
    redpenButton = new Button("Red Pen");
    add(redpenButton);
    redpenButton.addActionListener(this);
    
    greenpenButton = new Button("Green Pen");
    add(greenpenButton);
    greenpenButton.addActionListener(this);
    
    bluepenButton = new Button("Blue Pen");
    add(bluepenButton);
    bluepenButton.addActionListener(this);
    
    brownpenButton = new Button("Brown Pen");
    add(brownpenButton);
    brownpenButton.addActionListener(this);
    
    eraserButton = new Button("Eraser");
    add(eraserButton);
    eraserButton.addActionListener(this);
    
    clearButton = new Button("Clear All");
    add(clearButton);
    clearButton.addActionListener(this);
    
    firstsaveButton = new Button("Save Map");
    add(firstsaveButton);
    firstsaveButton.addActionListener(this);
    which = 0;
    
    addMouseMotionListener(this);
       
       shape = new Choice();
       shape.addItem("Square");
       shape.addItem("Rectangle");
       shape.addItem("Circle");
       shape.addItem("Oval");
       shape.addItemListener(this);
       add(shape);
       
       size = new Choice();
       size.addItem("10");
       size.addItem("25");
       size.addItem("50");
       size.addItem("100");
       size.addItem("150");
       size.addItem("200");
       size.addItem("250");
       size.addItem("300");
       size.addItem("350");
       size.addItem("400");
       size.addItem("450");
       size.addItem("500");
       size.addItemListener(this);
       add(size);
       
       filledin = new Choice();
       filledin.addItem("Fill In Shape");
       filledin.addItem("Outline Shape Only");
       filledin.addItemListener(this);
       add(filledin);
       
       collidable = new Choice();
       collidable.addItem("Collidable");
       collidable.addItem("Non-Collidable");
       collidable.addItemListener(this);
       add(collidable);  
    
    }
    
        public void actionPerformed(ActionEvent ae){
        Object source = ae.getSource();
        repaint();
        if (source.equals(blackpenButton))
        {
            which = 1;
        }
        else if (source.equals(redpenButton))
        {
            which = 2;
        }
        else if (source.equals(greenpenButton))
        {
            which = 3;
        }
    
        else if (source.equals(bluepenButton))
        {
            which = 4;
        }
        
        else if (source.equals(brownpenButton))
        {
            which = 5;
        }
        
        else if (source.equals(eraserButton))
        {
            which = 6;
        }
        else if (source.equals(clearButton))
        {
            which = 7;
        }
        else if (source.equals(firstsaveButton))
        {
            which = 8;
        }
        
    }
    
    public void paint(Graphics g)
    {
    
     if (which == 1) 
    {
        blackpen(g);
    }
    
     if (which == 2) 
    {
        redpen(g);
    }
    
     if (which == 3) 
    {
        greenpen(g);
    }
    
     if (which == 4) 
    {
        bluepen(g);
    }  
    
    if (which == 5) 
    {
        brownpen(g);
    } 
    
    if (which == 6) 
    {
        eraser(g);
    } 
    if (which == 7) 
    {
        clear(g);
    }
    if (which == 8) 
    {
        firstsave(g);
    }
}

       public void itemStateChanged(ItemEvent ie)
       {
       if (shape.getSelectedItem().equals("Square"))
       {
            shapeNbr = 1;   
       }
       if (shape.getSelectedItem().equals("Square"))
       {
           shapeNbr = 2;
       }
       else if (shape.getSelectedItem().equals("Rectangle"))
       {
           shapeNbr = 3;
       }
       else if (shape.getSelectedItem().equals("Circle"))
       {
           shapeNbr = 4;
       }
       else if (shape.getSelectedItem().equals("Oval"))
       {
           shapeNbr = 5;
       }    
       if (size.getSelectedItem().equals("10"))
       {
           width = 10;
           height = 10;
       }
       if (size.getSelectedItem().equals("25"))
       {
           width = 25;
           height = 25;
       }
       if (size.getSelectedItem().equals("50"))
       {
           width = 50;
           height = 50;
       }
       else if (size.getSelectedItem().equals("100"))
       {
           width = 100;
           height = 100;
       }
       else if (size.getSelectedItem().equals("150"))
       {
           width = 150;
           height = 150;
       }
       else if (size.getSelectedItem().equals("200"))
       {
           width = 200;
           height = 200;
       }
       else if (size.getSelectedItem().equals("250"))
       {
           width = 250;
           height = 250;
       }
       else if (size.getSelectedItem().equals("300"))
       {
           width = 300;
           height = 300;
       }
       else if (size.getSelectedItem().equals("350"))
       {
           width = 350;
           height = 350;
       }
       else if (size.getSelectedItem().equals("400"))
       {
           width = 400;
           height = 400;
       }
       else if (size.getSelectedItem().equals("450"))
       {
           width = 450;
           height = 450;
       }
       else if (size.getSelectedItem().equals("500"))
       {
           width = 500;
           height = 500;
       }
    }
    
    public void mouseMoved(MouseEvent me) 
    { 

    }
    
    //THIS IS THE POINT THAT NEEDS TO BE EDITED MOST LIKELY.
    public void mouseDragged(MouseEvent me)
    {
       //draw the shapes as the mouse is being dragged
       x = me.getX();
       y = me.getY();
       repaint();
    }
    //END OF POINT THAT NEEDS TO BE EDITED.
    public void decideWhichShape(Graphics g)
   {
       switch (shapeNbr)
       {   
           case 1:  //square
               g.fillRect(x, y, width, height);
               
               break;
           case 2:  //rectangle
               g.fillRect(x, y, width, height / 2);
                
               break;
           case 3:  //circle
                g.fillOval(x, y, width ,height);
                 
               break;
           case 4:  //oval
               g.fillOval(x, y, width, height / 2);
                   
                break;
            }
        }
       
    public void blackpen(Graphics g) 
    {
        if (shape.getSelectedItem().equals("Square"))
        {
            if (filledin.getSelectedItem().equals("Fill In Shape"))
            {
                g.fillRect(x, y, width, height);
            }
            if (filledin.getSelectedItem().equals("Outline Shape Only"))
            {
                g.drawRect(x, y, width, height);
            }
        }
        if (shape.getSelectedItem().equals("Rectangle"))
        {
            if (filledin.getSelectedItem().equals("Fill In Shape"))
            {
                g.fillRect(x, y, width, height / 2);
            }
            if (filledin.getSelectedItem().equals("Outline Shape Only"))
            {
                g.drawRect(x, y, width, height / 2);
            } 
        }
        if (shape.getSelectedItem().equals("Circle"))
        {
            if (filledin.getSelectedItem().equals("Fill In Shape"))
            {
                g.fillOval(x, y, width, height);
            }
            if (filledin.getSelectedItem().equals("Outline Shape Only"))
            {
                g.drawOval(x, y, width, height);
            }
        }
        if (shape.getSelectedItem().equals("Oval"))
        {
            if (filledin.getSelectedItem().equals("Fill In Shape"))
            {
                g.fillOval(x, y, width, height / 2);
            }
            if (filledin.getSelectedItem().equals("Outline Shape Only"))
            {
                g.drawOval(x, y, width, height / 2);
            }
       }

    }
    
    public void redpen(Graphics g)
    {
        g.setColor(Color.RED);
        blackpen(g);
    }
    
    public void greenpen(Graphics g)
    {
        g.setColor(Color.GREEN);
        blackpen(g);
    }
    
    public void bluepen(Graphics g)
    {
        g.setColor(Color.BLUE);
        blackpen(g);
    }
    
    public void brownpen(Graphics g)
    {
        g.setColor(Color.GRAY);
        blackpen(g);
    }
    
    public void eraser(Graphics g)
    { 
    }
    
    public void clear(Graphics g)
    {
      g.clearRect(0, 0, 5000, 5000);  
    }
    
    public void firstsave(Graphics g)
    {
        remove(blackpenButton);
        remove(redpenButton);
        remove(greenpenButton);
        remove(bluepenButton);
        remove(brownpenButton);
        remove(eraserButton);
        remove(clearButton);
        remove(firstsaveButton);
        remove(shape);
        remove(size);
        remove(filledin);
        remove(collidable);
        repaint();
    }
}

////////////////END//////////////// I labeled the part in the code that may need editing. Once again, the problem is, when you hold the mouse down, or even click, it will not create multiple shapes like it is supposed to. The entire time, only *ONE* shape is drawn. Thank you, and feel free to test the code in your compiler! -CoderRyne [Edited by - CoderRyne on February 13, 2010 7:51:18 AM]

Share this post


Link to post
Share on other sites
You need to rethink how you're storing your shape data. I see one instance each of the x, y, width, and height variables. As soon as you assign a value to one of those, it overwrites the previous value - that only serves to move or resize a shape, not create a new one. I suggest encapsulating your shape data in a class and storing instances of that class in a list.

Share this post


Link to post
Share on other sites

This topic is 2855 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this