Results 1 to 3 of 3
Like Tree1Likes
  • 1 Post By Tolls

Thread: Code works but nothing shows

  1. #1
    Hagelslag is offline Member
    Join Date
    Jan 2013
    Posts
    7
    Rep Power
    0

    Default Code works but nothing shows

    I'm fooling around with java and am trying to make a simple pacman game.
    but after changing some code(i dont know when) panman is invisible.
    i cant find out why and have tried everything to my knowledge, i hope someone on this forum can tell me what im doing wrong.



    Java Code:
    import javax.swing.*; 
    import java.awt.*; 
    import java.awt.Color;
    
    public class spelApp extends JFrame
    
    {
    	public spelApp()	
    	{
    		pac p = new pac(30, 30);
    		setSize( 500, 500 );
    		setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    		setTitle( "pacman" );
    		setLocation( 200, 100 );
    		setBackground(Color.RED);
    		spelPaneel hoofdPaneel = new spelPaneel();
    		knoppenBalkPaneel knoppenPaneel = new knoppenBalkPaneel(hoofdPaneel);//knoppenBalkPaneel are the four buttons to move with
    		add(hoofdPaneel, BorderLayout.CENTER);
    		add(knoppenPaneel, BorderLayout.SOUTH);
    
    		setVisible( true );
    	}
    
    	public static void main( String[] args )
    	{
    		new spelApp();
    	}
    }
    Java Code:
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JPanel;
    import java.util.Random;
    
    public class spelPaneel extends JPanel implements ActionListener
    {
    	javax.swing.Timer timer = new javax.swing.Timer(500, this);//1000 is 1 second
    	Random Timer = new Random();
    	public pac[] pacs = new pac[3];
    	public spelPaneel()
    	{
    		pacs[0] = new pac 		(100,100);
    		pacs[1] = new pacMan 	(100,100);
    		pacs[2] = new pacVrouw  (100,100);
    		timer.start();
    	}
    
    	public void paintComponent( Graphics g )
    	{
    		super.paintComponent( g );
    		g.setColor(new Color(124, 252, 0));//the area where pacs is allowed to move
    		g.fillRect(50, 50, 400, 400);
    
    	}
    
    	public void actionPerformed(ActionEvent e) 
    	{
    		for(int i = 0; i<3; i++)
    
    			if (e.getSource() == timer)
    			{
    				if (pacs[i].get_y() > 100 && pacs[i].get_y() < 400)
    				{
    					pacs[i].set_y(pacs[i].get_y() + Timer.nextInt(40)-20);
    					System.out.println("between Y 100&400");
    				}
    				else
    				{
    					pacs[i].set_y(250);// put pac back in the middle
    					System.out.println("back to Y middle");
    				}
    				
    				if (pacs[i].get_x() > 100 && pacs[i].get_x() < 400)
    				{
    					pacs[i].set_x(pacs[i].get_x() + Timer.nextInt(40)-20);
    					System.out.println("between X 100&400");
    				}
    				else
    				{
    					pacs[i].set_x(250);// put pac back in the middle
    					System.out.println("back to X middle");
    				}
    				System.out.println("repaint");
    				repaint();
    			}
    	}
    }
    Java Code:
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JPanel;
    
    public class knoppenBalkPaneel extends JPanel implements ActionListener
    {
    	private JButton leftButton, rightButton, upButton, downButton;
    
    	spelPaneel hoofdPaneel;
    
    	public knoppenBalkPaneel(spelPaneel hoofdPaneel)
    	{
    		this.hoofdPaneel = hoofdPaneel;
    
    		leftButton = new JButton("left");
    		leftButton.addActionListener(this);
    		add(leftButton);
    
    		rightButton = new JButton("right");
    		rightButton.addActionListener(this);
    		add(rightButton);
    
    		upButton = new JButton("up");
    		upButton.addActionListener(this);
    		add(upButton);
    
    		downButton = new JButton("down");
    		downButton.addActionListener(this);
    		add(downButton);
    	}
    
    	public void actionPerformed(ActionEvent e) 
    	{
    		for(int i = 0; i<3; i++)
    			if (e.getSource() == leftButton)
    			{
    				hoofdPaneel.pacs[i].status_Left();
    				hoofdPaneel.repaint();
    			}
    		for(int i = 0; i<3; i++)
    			if (e.getSource() == rightButton)
    			{
    				hoofdPaneel.pacs[i].status_Right();
    				hoofdPaneel.pacs[i].set_y(500);
    				hoofdPaneel.repaint();
    			}
    		for(int i = 0; i<3; i++)
    			if (e.getSource() == upButton)
    			{
    				hoofdPaneel.pacs[i].status_Up();
    				hoofdPaneel.repaint();
    			}
    		for(int i = 0; i<3; i++)
    			if (e.getSource() == downButton)
    			{
    				hoofdPaneel.pacs[i].status_Down();
    				hoofdPaneel.repaint();
    			}
    	}
    
    }
    Java Code:
    import java.awt.Color;
    import java.awt.Graphics;
    
    public class pac 
    {
    	protected final int LEFT	= 1;
    	protected final int RIGHT	= 2;
    	protected final int UP 	= 3;
    	protected final int DOWN = 4;
    	protected int x, y ;
    	protected int Status;
    
    	public pac( int X, int Y)
    	{
    		this.x = X;
    		this.y = Y;
    		Status = 1;
    	}
    
    	public void drawPac (Graphics g){
    		if (Status == LEFT)
    		{
    			g.setColor(Color.BLACK);
    			g.fillOval(x+100, y+100, 70, 70);
    		}
    
    		if (Status == RIGHT)
    		{
    			g.setColor(Color.BLACK);
    			g.fillOval(x+100, y+100, 70, 70);
    		}
    
    		if (Status == UP)
    		{
    			g.setColor(Color.BLACK);
    			g.fillOval(x+100, y+100, 70, 70);
    		}
    
    		if (Status == DOWN)
    		{
    			g.setColor(Color.BLACK);
    			g.fillOval(x+100, y+100, 70, 70);
    		}
    	}
    	public void status_Left()
    	{
    		setStatus1(LEFT);
    	}
    	public void setStatus1(int nieuw_Status)
    	{
    		Status = nieuw_Status;
    	}
    	public int getStatus1()
    	{
    		return Status;
    	}
    
    
    	public void status_Right()
    	{
    		setStatus1(RIGHT);
    	}
    	public void setStatus2(int nieuw_Status)
    	{
    		Status = nieuw_Status;
    	}
    	public int getStatus2()
    	{
    		return Status;
    	}
    
    
    	public void status_Up()
    	{
    		setStatus1(UP);
    	}
    	public void setStatus3(int nieuw_Status)
    	{
    		Status = nieuw_Status;
    	}
    	public int getStatus3()
    	{
    		return Status;
    	}
    
    
    	public void status_Down()
    	{
    		setStatus1(DOWN);
    	}
    	public void setStatus4(int nieuw_Status)
    	{
    		Status = nieuw_Status;
    	}
    	public int getStatus4()
    	{
    		return Status;
    	}	
    
    
    	public void set_y(int nieuw_y)
    	{
    		y = nieuw_y;
    	}	
    	public int get_y()
    	{
    		return y;
    	}
    	public void set_x(int nieuw_x)
    	{
    		x = nieuw_x;
    	}	
    	public int get_x()
    	{
    		return x;
    	}
    
    	
    	public void setStatus(int nieuw_Status)
    	{
    		Status = nieuw_Status;
    	}
    	public int getStatus()
    	{
    		return Status;
    	}
    }

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: Code works but nothing shows

    Where in that code are you drawing the pacman?
    Where are you calling the drawPac method?
    Hagelslag likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    Hagelslag is offline Member
    Join Date
    Jan 2013
    Posts
    7
    Rep Power
    0

    Default Re: Code works but nothing shows

    Thank you!
    i had deletet the drawPac in the paincomponent in spelPaneel!
    put pacs[0].drawPac(g); in there and now it works

Similar Threads

  1. Code compiles but fails to run (works in NetBeans)
    By swilliams236 in forum New To Java
    Replies: 2
    Last Post: 11-07-2011, 09:05 PM
  2. Replies: 3
    Last Post: 10-29-2011, 12:39 AM
  3. Replies: 1
    Last Post: 11-03-2010, 08:49 AM
  4. Please explain how this bit of code works.
    By Allspark in forum New To Java
    Replies: 4
    Last Post: 09-03-2010, 03:56 AM
  5. Replies: 2
    Last Post: 11-18-2008, 10:44 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •