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.

Page 3 of 3 FirstFirst 123
Results 51 to 72 of 72

Thread: Mutiple classfile, JPanel nothing shows up

  1. #51
    Member
    Join Date
    Jun 2012
    Location
    Québec
    Posts
    41
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default Re: Mutiple classfile, JPanel nothing shows up

    Quote Originally Posted by Norm View Post
    Break the statement up into single steps.

    setBounds() is a void method. You can not add(void) to a container.
    Ok I got it but... I need to make the layout translucent... I changed my code a bit :

    I Created objects of class ball and pad I don't really know if it's a good idea tho!

     import java.awt.*;
    import java.awt.Color;
    import java.awt.event.*;
     
     
    import javax.swing.*;
     
     
    public class arkanoidGame extends JFrame {
    	FlowLayout layout = new FlowLayout();
    	ball theBall = new ball();
    	pad thePad = new pad();
    	JButton startButton = new JButton("Start Game");
     
     
    	private static final long serialVersionUID = 1L;
     
     
    	public arkanoidGame(String name) {	
    		super(name);
     
    	}
    	public void addComponentsToPane(final Container pane) {
     
    		final JPanel panel = new JPanel();	
    		JPanel controls = new JPanel();
    		controls.setLayout(new FlowLayout());
    		panel.setLayout(null);		
    		controls.add(startButton);
     
    		startButton.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				panel.validate();
    				panel.repaint();
     
    			}
    		});
    		//pane.add(controls,BorderLayout.SOUTH);
    		pane.add(theBall);
    		theBall.setBounds(0,0,800,600);
    		pane.add(thePad);
    		thePad.setBounds(0,500,800,100);
    	}
     
     
    	private static void createAndShowGui() {
    		arkanoidGame frame = new arkanoidGame("Arkanoid Game");
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);					
    		frame.pack();
    		frame.setSize(800,600);
    		frame.setBounds(0,0,800,600);	
    		frame.addComponentsToPane(frame.getContentPane());	
    		frame.setFocusable(true);
    		frame.setFocusTraversalKeysEnabled(false);
    		frame.setVisible(true);		
     
    	}
     
    	public static void main(String[] args) {
    		try {
                //UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
                UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
            } catch (UnsupportedLookAndFeelException ex) {
                ex.printStackTrace();
            } catch (IllegalAccessException ex) {
                ex.printStackTrace();
            } catch (InstantiationException ex) {
                ex.printStackTrace();
            } catch (ClassNotFoundException ex) {
                ex.printStackTrace();
            }
    		  UIManager.put("swing.boldMetal", Boolean.FALSE);
     
    	        javax.swing.SwingUtilities.invokeLater(new Runnable(){
    	            public void run() {
    	                createAndShowGui();
     
    	            }	
     
    	        });
    	}
    }

  2. #52
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Mutiple classfile, JPanel nothing shows up

    I Created objects of class ball and pad I don't really know if it's a good idea
    Yes that's good.
    The question is: should they extend a component or should they not extend any component and use the play area's paintComponent() method call their drawing methods so they can all draw on the same surface.
    Last edited by Norm; June 25th, 2012 at 01:29 PM.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #53
    Member
    Join Date
    Jun 2012
    Location
    Québec
    Posts
    41
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default Re: Mutiple classfile, JPanel nothing shows up

    Quote Originally Posted by Norm View Post
    They should the play area's paintComponent() method call their drawing methods so they can all draw on the same surface.
    Yes all draw on the same surface

  4. #54
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Mutiple classfile, JPanel nothing shows up

    That will remove the problems with setBounds().
    If you don't understand my answer, don't ignore it, ask a question.

  5. #55
    Member
    Join Date
    Jun 2012
    Location
    Québec
    Posts
    41
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default Re: Mutiple classfile, JPanel nothing shows up

    Quote Originally Posted by Norm View Post
    That will remove the problems with setBounds().
    I need to set the background Translucent.. And I have no method in theBall object to do it.

  6. #56
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Mutiple classfile, JPanel nothing shows up

    Is that for a component? Or for a shape that you are drawing?
    What is beneath it that you want to see?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #57
    Member
    Join Date
    Jun 2012
    Location
    Québec
    Posts
    41
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default Re: Mutiple classfile, JPanel nothing shows up

    Quote Originally Posted by Norm View Post
    Is that for a component? Or for a shape that you are drawing?
    What is beneath it that you want to see?

    Well theBall has to go anywhere on the screen so the bounds are 0,0,800,600 which takes all the screen so I can't see the thePad

  8. #58
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Mutiple classfile, JPanel nothing shows up

    You missed the point of post#52.
    The playing surface is the whole screen on which are drawn the ball and pad. There is one component: the playing surface. The ball and pad are not components added to a container. The have drawMe methods that are called by the playing surface's paintComponent() method.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #59
    Member
    Join Date
    Jun 2012
    Location
    Québec
    Posts
    41
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default Re: Mutiple classfile, JPanel nothing shows up

    Quote Originally Posted by Norm View Post
    You missed the point of post#52.
    The playing surface is the whole screen on which are drawn the ball and pad. There is one component: the playing surface. The ball and pad are not components added to a container. The have drawMe methods that are called by the playing surface's paintComponent() method.

    I understand this... The playing surface is the JPanel or the FlowLayout?
    even if I do something like this :

    panel.setBackground(Color.TRANSLUCENT) or
    layout.setBackground(Color.TRANSLUCENT)

    None of those are Component so I get an error.

  10. #60
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Mutiple classfile, JPanel nothing shows up

    Please post the full text of the error messages.

    Why do you need a translucent component?
    If you don't understand my answer, don't ignore it, ask a question.

  11. #61
    Member
    Join Date
    Jun 2012
    Location
    Québec
    Posts
    41
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default Re: Mutiple classfile, JPanel nothing shows up

    Quote Originally Posted by Norm View Post
    Please post the full text of the error messages.

    Why do you need a translucent component?
    Well you right I don't need a translucent component. I got my two object showing right now I changed the bound for theBall.setBounds(0,0,200,200) and the pad showed up because it was underneath it.

    Now If I want movement for them... If I want the pad to move

  12. #62
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Mutiple classfile, JPanel nothing shows up

    The ball (and pad) class should not extend a component and should not cover anything.

    If you want a shape (pad) to move, change the x,y vaues for the location where it is being drawn.
    If you don't understand my answer, don't ignore it, ask a question.

  13. #63
    Member
    Join Date
    Jun 2012
    Location
    Québec
    Posts
    41
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default Re: Mutiple classfile, JPanel nothing shows up

    Quote Originally Posted by Norm View Post
    The ball (and pad) class should not extend a component and should not cover anything.
    So why if the bounds of the ball is the full screen it hides the pad?

  14. #64
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Mutiple classfile, JPanel nothing shows up

    The ball object (and pad) is not a component. It does not hide anything. The ball and pad are represented by shapes drawn on the playing surface.
    If you don't understand my answer, don't ignore it, ask a question.

  15. #65
    Member
    Join Date
    Jun 2012
    Location
    Québec
    Posts
    41
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default Re: Mutiple classfile, JPanel nothing shows up

    Quote Originally Posted by Norm View Post
    The ball (and pad) class should not extend a component and should not cover anything.

    If you want a shape (pad) to move, change the x,y vaues for the location where it is being drawn.
    Well in my class pad some methods are in there to make it move but they are not being called

  16. #66
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Mutiple classfile, JPanel nothing shows up

    If you don't call them, what are they for?
    If you don't understand my answer, don't ignore it, ask a question.

  17. #67
    Member
    Join Date
    Jun 2012
    Location
    Québec
    Posts
    41
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default Re: Mutiple classfile, JPanel nothing shows up

    Quote Originally Posted by Norm View Post
    If you don't call them, what are they for?
    I created a new method in arkanoidGame

    public void actionPerformed(ActionEvent e) {
    thePad.actionPerformed(e);
    System.out.println("The Pad is being Called");
    }


    doesn't print anything

  18. #68
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Mutiple classfile, JPanel nothing shows up

    Sorry, I have no idea what you are trying to do or how those few lines of code you posted fit with the rest of the program.
    If you don't understand my answer, don't ignore it, ask a question.

  19. #69
    Member
    Join Date
    Jun 2012
    Location
    Québec
    Posts
    41
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default Re: Mutiple classfile, JPanel nothing shows up

    arkanoidGame.java
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
     
     
     
    import javax.swing.*;
     
     
    public class arkanoidGame extends JFrame implements ActionListener {
    	FlowLayout layout = new FlowLayout();
    	ball theBall = new ball();
    	pad thePad = new pad();
     
     
    	private static final long serialVersionUID = 1L;
     
     
    	public arkanoidGame(String name) {	
    		super(name);
     
    	}
    	public void addComponentsToPane(final Container pane) {
     
    		final JPanel panel = new JPanel();	
    		panel.setLayout(null);	
    		pane.add(theBall);
    		theBall.setBounds(50,50,75,75);
    		pane.add(thePad);
    	}
     
    	public void actionPerformed(ActionEvent e) {
    		thePad.actionPerformed(e);
    		System.out.println("The Pad is being Called");
    	}
     
     
    	private static void createAndShowGui() {
    		arkanoidGame frame = new arkanoidGame("Arkanoid Game");
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);					
    		frame.pack();
    		frame.setSize(800,600);
    		frame.setBounds(0,0,800,600);	
    		frame.addComponentsToPane(frame.getContentPane());	
    		frame.setFocusable(true);
    		frame.setFocusTraversalKeysEnabled(false);
    		frame.setVisible(true);
    	}
     
    	public static void main(String[] args) {
    		try {
                //UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
                UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
            } catch (UnsupportedLookAndFeelException ex) {
                ex.printStackTrace();
            } catch (IllegalAccessException ex) {
                ex.printStackTrace();
            } catch (InstantiationException ex) {
                ex.printStackTrace();
            } catch (ClassNotFoundException ex) {
                ex.printStackTrace();
            }
    		  UIManager.put("swing.boldMetal", Boolean.FALSE);
     
    	        javax.swing.SwingUtilities.invokeLater(new Runnable(){
    	            public void run() {
    	                createAndShowGui();
     
    	            }	
     
    	        });
    	}
     
    }

    pad.java
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import java.awt.geom.Rectangle2D;
    import javax.swing.JPanel;
    import javax.swing.Timer;
     
     
    public class pad extends JPanel implements KeyListener, ActionListener {
     
    	Timer t = new Timer(5, this);
     
    	double rectX = 15;
    	double rectY = 535;
    	int rectWidth = 80;
    	int rectHeight = 20;
    	double rectVeloX = 0.0;
    	double rectVeloY = 0.0;
     
    	private static final long serialVersionUID = 1L;
     
     
    	public void paint(Graphics draw2D){			
    		super.paint(draw2D);		
    		Graphics2D draw = (Graphics2D) draw2D;
    		draw.setColor(Color.red);
    		draw.fill(new Rectangle2D.Double(rectX,rectY, rectWidth, rectHeight));
     
    		}
     
     
    	public void actionPerformed (ActionEvent e) {
    	   	repaint();
    		rectX += rectVeloX;
    		rectY += rectVeloY;
     
    	}
     
    	public void left() {
    		if (rectX <= 0) 
    		{
    			rectVeloX = 0;
    			rectX = 0;
    		}
    		else {
    			 rectVeloX = -1.5;
    			 rectVeloY = 0;
    		}		
    	}
     
    	public void right() {
    		   if ((rectX+rectWidth) >= 800)
    		   {
    			   rectVeloX = 0;
    			   rectX = 800-rectWidth;
    		   }
    		   else {
    			   rectVeloX = 1.5;
    			   rectVeloY = 0;
    		   }		
    	}
     
     
    	public void keyPressed(KeyEvent e) {
    		int key = e.getKeyCode();
     
    		   if (key == KeyEvent.VK_RIGHT ) {
    			   right();
     
    	    }  if (key == KeyEvent.VK_LEFT ) {
    	    		left();
    	    }
     
    	}
     
    	public void keyReleased(KeyEvent e) {
    		int key = e.getKeyCode();
     
    			if (key == KeyEvent.VK_RIGHT) {
    				rectVeloX = 0;
    				rectVeloY = 0;
     
    			}
     
    			if (key == KeyEvent.VK_LEFT) {
    				rectVeloX = 0;
    				rectVeloY = 0;
    			}
    	}
     
    	public void keyTyped(KeyEvent e) {}		
     
    }

    ball.java
    import javax.swing.*;
    import java.awt.*;
     
     
    public class ball extends JPanel implements Runnable{
     
    	private static final long serialVersionUID = 1L;
    	int ballX = 50;
    	int ballY = 50;
    	int ballWH = 25;
     
     
    	public void init() { 
    		setBackground(Color.BLUE);
    	}
     
    	public void start() {
    		// define a new thread 
    		Thread th = new Thread (this); 
    		// start this thread 
    		th.start (); 
    		} 	
     
    	public void stop() { } 	
    	public void destroy() { } 
     
     
     
    	public void paint (Graphics g) {
    		super.paint(g);
    		g.setColor(Color.blue);
    		g.fillOval(ballX,ballY,ballWH,ballWH);
    		System.out.println("ball bounds = " + getBounds());
    	} 	
     
    	public void run() {
    		// lower ThreadPriority 
    		Thread.currentThread().setPriority(Thread.MIN_PRIORITY); 
     
    		// run a long while (true) this means in our case "always" 
    		while (true) 
    		{ 
    			ballX++;
    			ballY++;
    			repaint(); 
     
    		try 
    		{
    		// Stop thread for 20 milliseconds 
    		Thread.sleep (20); 
    		} 
    		catch (InterruptedException ex) 
    		{
    		// do nothing 
    		} 
     
    		// set ThreadPriority to maximum value 
    		Thread.currentThread().setPriority(Thread.MAX_PRIORITY); 
     
    		}
     
     
    	}
     
    }

  20. #70
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Mutiple classfile, JPanel nothing shows up

    Can you explain what the code in post#67 is supposed to do?

    What problems are you trying to solve now?

    I see that ball and pad still extend JPanel. That was supposed to have been removed.
    If you don't understand my answer, don't ignore it, ask a question.

  21. #71
    Member
    Join Date
    Jun 2012
    Location
    Québec
    Posts
    41
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default Re: Mutiple classfile, JPanel nothing shows up

    Quote Originally Posted by Norm View Post
    Can you explain what the code in post#67 is supposed to do?
    Well I was trying to move the pad somehow

    What problems are you trying to solve now?

    I see that ball and pad still extend JPanel. That was supposed to have been removed.
    I removed the JPanel and now a loads of error popup

    Exception in thread "AWT-EventQueue-0" java.lang.Error: Unresolved compilation problems:
    The method add(Component) in the type Container is not applicable for the arguments (ball)
    The method setBounds(int, int, int, int) is undefined for the type ball
    The method add(Component) in the type Container is not applicable for the arguments (pad)

  22. #72
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Mutiple classfile, JPanel nothing shows up

    Those two classes are not components now so you need to remove all the component class's methods and usages.
    If you don't understand my answer, don't ignore it, ask a question.

Page 3 of 3 FirstFirst 123

Similar Threads

  1. [SOLVED] Pesky <JPanel>.getWidth() and <JPanel>.getHeight() Methods...
    By snowguy13 in forum AWT / Java Swing
    Replies: 1
    Last Post: December 31st, 2011, 03:35 PM
  2. Table with CustomModel when clicked shows old entry
    By Nesh108 in forum AWT / Java Swing
    Replies: 6
    Last Post: November 9th, 2011, 06:38 AM
  3. Mutiple JFrames Problems
    By mDennis10 in forum AWT / Java Swing
    Replies: 8
    Last Post: September 3rd, 2011, 10:24 PM
  4. Replies: 2
    Last Post: January 7th, 2011, 09:10 PM
  5. Creating and displaying a JPanel inside another JPanel
    By JayDuck in forum AWT / Java Swing
    Replies: 1
    Last Post: April 7th, 2009, 08:02 AM