Java-Gaming.org Java4K winners: [ by our judges | by the community ]         
Featured games (67)
games approved by the League of Dukes
Games in Showcase (∞)
games submitted by our members



News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  Print  
  JFrames and JPanels  (Read 356 times)
0 Members and 1 Guest are viewing this topic.
Offline Fingerz

JGO n00b
*

Posts: 2



« on: 2012-01-13 20:18:09 »

I have been pulling my hair for days with this now. I simply want to create a black background and then add a JPanel on top of that with a green bg. New to Java and Swing obvisiouly. Over all I want to draw a football field in the panel, that displays 30 yards at a time and the background will scroll up and down following the location of the football. I figured with a panel I could draw the entire field and the panel would clip the remainder of the field. Problem is I cant get the second panel to even show. This is what I have.

public static void main(String[] args) {

        drawPracticeField addField = new drawPracticeField();

        //Create frame


        JFrame backGroundWindow = new JFrame();
        
            backGroundWindow.setSize(760, 760);
            backGroundWindow.setUndecorated(true);
            backGroundWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            //Utilizing apsolute positioning
            backGroundWindow.setLayout(null);
            //make Visible
            backGroundWindow.setVisible(true);

            
            //JPanel Background color component

            JPanel backGroundPanel =  new JPanel();
            backGroundPanel.setSize(760,760);
            backGroundPanel.setBackground(Color.black);

            // add panel to container
            backGroundWindow.add(backGroundPanel);
            // addNewField JPanel
            backGroundWindow.add(addField).setLocation(50, 40);

public class drawPracticeField extends JPanel{

    public drawPracticeField() {

        
        
        JPanel newField = new JPanel();
        Dimension d = new Dimension();
        d.setSize(600, 600);
        newField.setPreferredSize(d);
        
    }
    
        /*
        Draw outside boarders
        Draw hash marks
        Draw 10 yard lines
        Add Line numbers Line of scrimage to 40 yard line
         */

    

    protected void paintComponent(Graphics g) {

       super.paintComponent(g);
      
       Color greenField = new Color (45, 96, 32);
       g.setColor(greenField);
       g.drawRect(50,40,600,600);
       g.fillRect(50,40,600,600);

      


    }

Thank you for any info you can give me.
Offline EddieRich

JGO n00b
*

Posts: 21



« Reply #1 on: 2012-01-13 20:58:21 »

You have 3 different JPanels, and none have been added to the JFrame.

backGroundPanel is created and never added to a JFrame, so it will never appear.

newField is created inside your drawPracticeField constructor, but never added to anything. Therfore, when the constructor exits, new field gets destroyed.

addField gets created, but never gets added to a JFrame, so it will never appear.

Could you describe what you think each panel is supposed to do ?
Offline Fingerz

JGO n00b
*

Posts: 2



« Reply #2 on: 2012-01-13 21:39:00 »

Sorry that few lines of code where I added the Panels to the frame
Games published by our own members! Go get 'em!
Offline ra4king

JGO Kernel
*****

Posts: 3156
Medals: 196


I'm the King!


« Reply #3 on: 2012-01-14 00:05:30 »

You never set the size of the "drawPraticeField" panel. It's default size is 0,0 Wink

Offline CaptainJester

JGO Neuromancer
****

Posts: 1138
Medals: 8


Make it work; make it better.


« Reply #4 on: 2012-01-14 10:33:41 »

Don't try to use multiple panels for a single drawing area. Here is what you can try:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Insets;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

public class Test extends JFrame {
    public Test() {
        setLayout(new BorderLayout());
        JScrollPane scroll = new JScrollPane(new FootballField());
        add(scroll, BorderLayout.CENTER);
        setSize(500, 500);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        //for anything to have a size you need to make it visible
       setVisible(true);
        Insets frameMargin = getInsets();
        Insets scrollMargin = scroll.getInsets();
        int frameWidth = 490 + frameMargin.left + frameMargin.right + scrollMargin.left + scrollMargin.right + scroll.getVerticalScrollBar().getWidth();
        //resize the frame so the entire football field can be seen
       setSize(frameWidth, 500);
    }
    public static void main(String... args) {
        Test t = new Test();
    }
}

class FootballField extends JPanel {
    Dimension size = new Dimension(490, 980);
   
    public void paintComponent(Graphics g) {
        g.setColor(Color.BLACK);
        g.fillRect(0, 0, 490, 980);
        g.setColor(Color.GREEN);
        g.fillRect(70, 70, 350, 840);
    }
   
    //override getPreferredSize so any container will know what size this component is
   public Dimension getPreferredSize() {
        return size;
    }
}

Pages: [1]
  Print  
 
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.16 | SMF © 2011, Simple Machines Valid XHTML 1.0! Valid CSS!
Page created in 0.073 seconds with 20 queries.