Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: AWT Button Will Not Remove

  1. #1
    Junior Member
    Join Date
    May 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default AWT Button Will Not Remove

    I am having an issue removing 3 buttons from my applet. The part I find weird though is the fact that I removed a button using the remove() method just above. It almost appears as though the method fails to work when it is called after the first time. It is a simple Monopoly game that was going pretty easy until this issue arose. I'll be honest, I am relatively new to Java but love challenging myself with difficult stuff. Please if you have any suggestions then let me know!

    PS I call two custom classes externally if you need those as well.

    import java.util.*;
    import java.awt.*;
    import java.applet.*;
    //import javax.swing.*;
    import java.net.*;
    import javax.imageio.*;
    import java.io.IOException;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
     
    public class Monopoly extends Applet {
     
    	ArrayList<Player> players = new ArrayList<Player>();
    	Spaces[] property = new Spaces[40];
     
    	Dimension dim;
     
    	Graphics bufferGraphics;
     
    	Button playUp;
    	Button playDown;
    	Button startGame;
    	Button start;
    	Button roll;
     
    	int current;
    	int turn;
    	int round = -1;
    	int numPlay=1;
    	int ctr = 0;
     
    	Image logo;
    	Image rail;
    	Image community;
    	Image chance;
    	Image board;
     
    	public void init() {
     
    		board = createImage(getWidth(),getHeight());
     
    		bufferGraphics = board.getGraphics();
     
    		setLayout(null);
     
    		setProperty();
    		players.add(new Player(Color.RED));
    		dim = getSize();
     
    		try { 
            	logo = ImageIO.read(new URL(getCodeBase(), "logo.jpg"));
            	rail = ImageIO.read(new URL(getCodeBase(), "railroad.gif"));
            	chance = ImageIO.read(new URL(getCodeBase(), "chance.png"));
            	community = ImageIO.read(new URL(getCodeBase(), "community.png"));
     
            } catch (MalformedURLException ex) { ex.printStackTrace();  }	
    		catch (IOException e) { System.out.println("BAD FILE");		} 
     
    	}
     
    	public void paintSetup(){
     
    			bufferGraphics.setColor(Color.WHITE);
     
    			bufferGraphics.fillRect(0,0,620,670);
     
    			bufferGraphics.setColor(Color.BLACK);
     
    			bufferGraphics.drawImage(logo,199,0,null);
     
    			//top
    			bufferGraphics.drawRect(50,100,80,80);
    			bufferGraphics.drawRect(130,100,40,80);
    			bufferGraphics.drawRect(170,100,40,80);
    			bufferGraphics.drawRect(210,100,40,80);
    			bufferGraphics.drawRect(250,100,40,80);
    			bufferGraphics.drawRect(290,100,40,80);
    			bufferGraphics.drawRect(330,100,40,80);
    			bufferGraphics.drawRect(370,100,40,80);
    			bufferGraphics.drawRect(410,100,40,80);
    			bufferGraphics.drawRect(450,100,40,80);
    			bufferGraphics.drawRect(490,100,80,80);
     
     
    			//left
    			bufferGraphics.drawRect(50,180,80,40);
    			bufferGraphics.drawRect(50,220,80,40);
    			bufferGraphics.drawRect(50,260,80,40);
    			bufferGraphics.drawRect(50,300,80,40);
    			bufferGraphics.drawRect(50,340,80,40);
    			bufferGraphics.drawRect(50,380,80,40);
    			bufferGraphics.drawRect(50,420,80,40);
    			bufferGraphics.drawRect(50,460,80,40);
    			bufferGraphics.drawRect(50,500,80,40);
     
    			//right
    			bufferGraphics.drawRect(490,180,80,40);
    			bufferGraphics.drawRect(490,220,80,40);
    			bufferGraphics.drawRect(490,260,80,40);
    			bufferGraphics.drawRect(490,300,80,40);
    			bufferGraphics.drawRect(490,340,80,40);
    			bufferGraphics.drawRect(490,380,80,40);
    			bufferGraphics.drawRect(490,420,80,40);
    			bufferGraphics.drawRect(490,460,80,40);
    			bufferGraphics.drawRect(490,500,80,40);
     
    			//bottom
    			bufferGraphics.drawRect(50,540,80,80);
    			bufferGraphics.drawRect(130,540,40,80);
    			bufferGraphics.drawRect(170,540,40,80);
    			bufferGraphics.drawRect(210,540,40,80);
    			bufferGraphics.drawRect(250,540,40,80);
    			bufferGraphics.drawRect(290,540,40,80);
    			bufferGraphics.drawRect(330,540,40,80);
    			bufferGraphics.drawRect(370,540,40,80);
    			bufferGraphics.drawRect(410,540,40,80);
    			bufferGraphics.drawRect(450,540,40,80);
    			bufferGraphics.drawRect(490,540,80,80);
     
    			//writing
    			bufferGraphics.setFont(new Font("Bold",0,30));
    			bufferGraphics.drawString("GO",510,150);
     
    			bufferGraphics.setFont(new Font("Dialog",0,20));
    			bufferGraphics.drawString("Go to",60,135);
    			bufferGraphics.drawString("Jail",70,160);
     
    			bufferGraphics.drawString("Free",70,580);
    			bufferGraphics.drawString("Parking",55,595);
     
    			bufferGraphics.setColor(Color.LIGHT_GRAY);
    			bufferGraphics.fillRect(497,541,5,48);
    			bufferGraphics.fillRect(512,541,5,48);
    			bufferGraphics.fillRect(527,541,5,48);
    			bufferGraphics.setColor(Color.BLACK);
    			bufferGraphics.drawRect(490,540,50,50);
    			bufferGraphics.drawString("Jail",500,570);
     
    			//LOGOS
     
    			//green
    			bufferGraphics.setColor(new Color(0,100,0));
    			bufferGraphics.fillRect(131,160,39,20);
    			bufferGraphics.fillRect(171,160,39,20);
    			bufferGraphics.fillRect(251,160,39,20);
     
    			//dark blue
    			bufferGraphics.setColor(new Color(0,0,128));
    			bufferGraphics.fillRect(371,160,39,20);
    			bufferGraphics.fillRect(451,160,39,20);
     
    			//purple
    			bufferGraphics.setColor(new Color(102,0,153));
    			bufferGraphics.fillRect(491,181,20,39);
    			bufferGraphics.fillRect(491,261,20,39);
     
    			//light blue
    			bufferGraphics.setColor(new Color(100,149,237));
    			bufferGraphics.fillRect(491,381,20,39);
    			bufferGraphics.fillRect(491,461,20,39);
    			bufferGraphics.fillRect(491,501,20,39);
     
    			//magenta
    			bufferGraphics.setColor(Color.MAGENTA);
    			bufferGraphics.fillRect(331,541,39,20);
    			bufferGraphics.fillRect(371,541,39,20);
    			bufferGraphics.fillRect(451,541,39,20);
     
    			//orange
    			bufferGraphics.setColor(Color.ORANGE);
    			bufferGraphics.fillRect(131,541,39,20);
    			bufferGraphics.fillRect(171,541,39,20);
    			bufferGraphics.fillRect(251,541,39,20);
     
    			//red
    			bufferGraphics.setColor(Color.RED);
    			bufferGraphics.fillRect(110,381,20,39);
    			bufferGraphics.fillRect(110,421,20,39);
    			bufferGraphics.fillRect(110,501,20,39);
     
    			//yellow
    			bufferGraphics.setColor(Color.YELLOW);
    			bufferGraphics.fillRect(110,181,20,39);
    			bufferGraphics.fillRect(110,261,20,39);
    			bufferGraphics.fillRect(110,301,20,39);
     
    			//rail
    			bufferGraphics.drawImage(rail,510,340,null);
    			bufferGraphics.drawImage(rail,70,340,null);
    			bufferGraphics.drawImage(rail,290,560,null);
    			bufferGraphics.drawImage(rail,290,120,null);
     
    			//community
    			bufferGraphics.drawImage(community,210,120,null);
    			bufferGraphics.drawImage(community,210,560,null);
    			bufferGraphics.drawImage(community,510,220,null);
     
    			//chance
    			bufferGraphics.drawImage(chance,330,120,null);
    			bufferGraphics.drawImage(chance,510,420,null);
    			bufferGraphics.drawImage(chance,70,460,null);
     
    	}
    	public void paint(Graphics g) {
     
         	setBackground(Color.white);
         	if (ctr==0){
    			paintSetup();
    			ctr++;
    		}
    		g.drawImage(board,0,0,null);
    		for (int i=0;i<players.size();i++){
    			players.get(i).drawMe(g);
    		}
    		playGame(g);
    	}
     
    	public void playGame(Graphics g){
    		ButtonListener blisten = new ButtonListener();
     
    		if (round == -1){
    			startGame = new Button("Play");
     
    			startGame.addActionListener(blisten);
     
    			startGame.setBounds(880,330,100,30);
     
     
    			add(startGame);
     
    			g.setFont(new Font("Bold",0,40));
    			g.drawString("Welcome to Monopoly",750,250);
     
    			g.setFont(new Font("Bold",0,20));
    			g.drawString("By Connor Giles",850,300);
    		}
    		else if(round==0){
    			startGame.removeActionListener(blisten);
    			remove(startGame);
     
    			playUp = new Button("+");
    			playDown = new Button("-");
    			start = new Button("Start");
     
    			playUp.addActionListener(blisten);
    			playDown.addActionListener(blisten);
    			start.addActionListener(blisten);
     
    			playUp.setBounds(1020,325,40,30);
    			playDown.setBounds(1060,325,40,30);
    			start.setBounds(790,325,100,30);
     
    			add(playUp);
    			add(playDown);
    			add(start);
     
    			g.setFont(new Font("Bold",0,40));
    			g.drawString("How Many Players?",770,250);
     
    			g.setFont(new Font("Bold",0,30));
    			g.drawString(""+numPlay+" Player",900,350);
    		}
    		else{	
     
    			if(ctr==1){
    				remove(playUp);
    				remove(playDown);
    				remove(start);
    				ctr++;
    			}
    			//set space
    			players.get(current).setCurrent(1);
     
    			//sets players location on the board
    			players.get(current).setPosition(property[players.get(current).getCurrent()].getX(),
    			property[players.get(current).getCurrent()].getY());
     
    		}
    		validate();
    	}
    	public void setProperty(){
     
    		property[0]=new Spaces("Go",0,520,130,true,false,-200,null);
    		property[1]=new Spaces("Mediterranean Ave.",60,520,190,false,true,2,"Purple");
    		property[2]=new Spaces("Community Chest",0,520,230,false,false,0,"Community Chest");
    		property[3]=new Spaces("Baltic Ave.",60,520,270,false,true,4,"Purple");
    		property[4]=new Spaces("Income Tax",0,520,310,true,false,200,"Tax");
    		property[5]=new Spaces("Reading Railroad",200,520,350,false,true,25,"Railroad");
    		property[6]=new Spaces("Oriental Ave.",100,520,390,false,true,6,"Light-Blue");
    		property[7]=new Spaces("Chance",0,520,430,false,false,0,"Chance");
    		property[8]=new Spaces("Vermont Ave.",100,520,470,false,true,6,"Light-Blue");
    		property[9]=new Spaces("Connecticut Ave.",120,520,510,false,true,8,"Light-Blue");
    		property[10]=new Spaces("Jail",0,520,570,false,false,0,"Jail");
    		property[10]=new Spaces("St. Charles Place",140,460,570,false,true,10,"Violet");
    	//	property[11]=new Spaces("Electric Company",150,420,570,false,true,)
    	}
    	public class ButtonListener implements ActionListener {
    		public void actionPerformed(ActionEvent e) {
     
    			if (e.getActionCommand().equals("+")){
    				if(numPlay<6)
    					numPlay++;
    			}
     
    			if (e.getActionCommand().equals("-")){
    				if (numPlay>1)
    					numPlay--;
    			}
     
    			if (e.getActionCommand().equals("Start")){
    				round=round+1;
    			}
     
    			if (e.getActionCommand().equals("Play"))
    				round=round+1;	
     
     
    			repaint();
    		}
     
    	}
     
    }
    Last edited by connor.giles; May 30th, 2012 at 08:29 AM.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: AWT Button Will Not Remove

    That's a pretty wonky setup. Why are you adding components from paint code?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    May 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: AWT Button Will Not Remove

    As I said, I'm just learning the ropes. I'm assuming that is a fairly large "NO" in terms of program structure? Ahahah. I had originally thought of that to be a useful place to add them as I could simply call repaint in order to get it to remove and add components as need be. Would it be possible that this could be causing some issues?

    Thanks a lot for the help!

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: AWT Button Will Not Remove

    Yeah, you're doing a few weird things here. Don't do things like set the background inside painting code. This could lead to an infinite loop, or at the very least, strange behavior.

    Your painting code should contain one thing- painting code. No logic, no adding components. You have very little control over how often paint code is called, so it's a bad idea to do anything except fast painting.

    Also, is there a reason you're using Applet and Button instead of JApplet and JButton? I'd recommend extending JPanel and overriding the paintComponent() method, and calling super.paintComponent() as the first line in that method.

    Recommended reading: Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/Swing)
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Junior Member
    Join Date
    May 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: AWT Button Will Not Remove

    Ok thanks a lot! Once I moved some stuff around and cleaned it up as you suggested I was able to get it all to work. This was my first real stab at any form of GUI so I obviously have a lot of learning to do. Thanks again.

Similar Threads

  1. Adding add/remove button to add/remove tab
    By JMtrasfiero in forum What's Wrong With My Code?
    Replies: 6
    Last Post: March 27th, 2012, 11:24 AM
  2. How to remove the last comma
    By fride360 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 29th, 2011, 07:20 AM
  3. not able to remove from ArrayList
    By harsha_c in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 3rd, 2011, 03:28 AM
  4. JTree - Remove All Nodes
    By aussiemcgr in forum Java Theory & Questions
    Replies: 4
    Last Post: December 9th, 2010, 05:27 PM
  5. How to remove letters
    By noobish in forum Java Theory & Questions
    Replies: 13
    Last Post: October 3rd, 2009, 10:36 PM

Tags for this Thread