Results 1 to 3 of 3
  1. #1
    xlacrimx is offline Member
    Join Date
    Nov 2010
    Posts
    1
    Rep Power
    0

    Default New to Java GUI. Can't get this to work.

    I'm only trying to create Pong. I need a JFrame that has a black background. What am I doing wrong? Here's my code so far...

    import javax.swing.JFrame;
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.geom.*;

    public class Pong extends JFrame{

    public Pong(){
    int width = 800;
    int height = 600;

    setSize(width,height);
    setTitle("Wacky Pong 380");
    setBackground(Color.BLACK);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );
    setVisible(true);

    }

    public void draw(Graphics g){
    g.setColor(Color.BLACK);
    g.fillRect(0,0,getWidth(),getHeight());
    }

    public static void main(String[] args){

    new Pong();

    }

    }

  2. #2
    berkeleybross's Avatar
    berkeleybross is offline Senior Member
    Join Date
    Feb 2010
    Location
    England
    Posts
    187
    Rep Power
    5

    Default

    the JFrame background is covered by the content pane.

    also, if you want to do drawing in a JFrame, then you should put a JPanel into the JFrame, and override its paintComponent method.

  3. #3
    doomsword2001 is offline Member
    Join Date
    Nov 2010
    Posts
    15
    Rep Power
    0

    Default this will get you started

    Java Code:
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.geom.*;
    
    public class Pong extends JFrame{
    private DrawPanel drawPanel;
    private Container container;
    
    private static int  width = 800, height = 600;
    
    public Pong(){
    
    container = this.getContentPane();
    container.setPreferredSize(new Dimension(width,height));
    setTitle("Wacky Pong 380");
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );
    setVisible(true);
    drawPanel = new DrawPanel();
    container.add(drawPanel);
    
    pack();
    
    }
    
    public class DrawPanel extends JPanel {
    	
    	public DrawPanel(){
    		
    	}
        public  void paintComponent(Graphics g)
       {
        g.setColor(Color.black);
        g.fillRect(0,0,width,height);
        
        g.setColor(Color.yellow);
        g.fillRect(width/3,height/3,width/4,height/4);
        }
    }
    
    
    }
    Last edited by doomsword2001; 11-08-2010 at 01:22 AM.

Similar Threads

  1. Out of memory work around for a java application (please help!)
    By javameanslife in forum Advanced Java
    Replies: 9
    Last Post: 02-02-2010, 01:52 PM
  2. Trying to get JAVA to work with SQLite
    By mark8569 in forum JDBC
    Replies: 3
    Last Post: 04-25-2009, 01:42 AM
  3. Just how do I get Java to actually work?
    By MickY G in forum New To Java
    Replies: 5
    Last Post: 11-19-2008, 03:50 AM
  4. What are applet and how its work in java?
    By pawankumarom in forum New To Java
    Replies: 7
    Last Post: 09-05-2008, 05:26 PM
  5. Ho to work with enumerations in java
    By zizou147 in forum Advanced Java
    Replies: 0
    Last Post: 03-22-2008, 12:53 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
  •