Results 1 to 3 of 3
Thread: Code works but nothing shows
- 01-21-2013, 04:11 PM #1
Member
- Join Date
- Jan 2013
- Posts
- 7
- Rep Power
- 0
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; } }
- 01-21-2013, 05:05 PM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Re: Code works but nothing shows
Where in that code are you drawing the pacman?
Where are you calling the drawPac method?Please do not ask for code as refusal often offends.
- 01-21-2013, 06:03 PM #3
Member
- Join Date
- Jan 2013
- Posts
- 7
- Rep Power
- 0
Similar Threads
-
Code compiles but fails to run (works in NetBeans)
By swilliams236 in forum New To JavaReplies: 2Last Post: 11-07-2011, 09:05 PM -
my code works fine but don't know if its the right way
By mike28 in forum Java AppletsReplies: 3Last Post: 10-29-2011, 12:39 AM -
JMF works fine with javac BUT same code has problems with NetBeans 6.9.1
By WACman in forum NetBeansReplies: 1Last Post: 11-03-2010, 08:49 AM -
Please explain how this bit of code works.
By Allspark in forum New To JavaReplies: 4Last Post: 09-03-2010, 03:56 AM -
Can someone please help fix this code so the program works? (2D arrays)
By busdude in forum New To JavaReplies: 2Last Post: 11-18-2008, 10:44 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks