Results 1 to 12 of 12
  1. #1
    Join Date
    Mar 2014
    Posts
    72
    Rep Power
    0

    Default Multiple JPanels; paint not working.

    For some reason, nothing is painted.

    (Code slightly edited.
    Here's the code for the Main class:

    Java Code:
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.event.MouseEvent;
    import java.io.BufferedReader;
    import java.io.DataInputStream;
    import java.io.DataOutputStream;
    import java.io.InputStreamReader;
    import java.io.PrintWriter;
    import java.net.InetAddress;
    import java.net.ServerSocket;
    import java.net.Socket;
    import java.util.ArrayList;
    import java.util.Random;
    
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    
    @SuppressWarnings("serial")
    public class Main extends JPanel implements Runnable {
    
    	public static final int WIN_WIDTH = 1200;
    	public static final int WIN_HEIGHT = 800;
    
    	public static final int GRID_X = 9;
    	public static final int GRID_Y = 5;
    
    	@SuppressWarnings("unused")
    	public static final int TILE_SIZE = (WIN_WIDTH / (GRID_X + 2) >= WIN_HEIGHT / GRID_Y) ? WIN_HEIGHT / GRID_Y : WIN_WIDTH / (GRID_X + 2);
    	public static final int ICON_SIZE = TILE_SIZE / 4;
    
    	public static Card KING[] = new Card[2];
    
    
    	public static JFrame frame;
    
    	public Main() {
    		setFocusable(true);
    
    		// PLAYER SET-UP
    		KING[0] = new Card(300, 200, 100, 100, 0, 1);
    
    	public void run() {
    		/*System.out.println("Thread running.");
    		while (true) {
    			repaint();
    			try {
    				Thread.sleep(17);
    			} catch (InterruptedException e) {
    				e.printStackTrace();
    			}
    		}*/
    	}
    
    	public static void main(String[] args) {
    		frame = new JFrame("Kings of War");
    		frame.add(new Main());
    		frame.setSize(WIN_WIDTH, WIN_HEIGHT);
    		frame.setResizable(false);
    		frame.setLocationRelativeTo(null);
    		frame.setVisible(true);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	}
    Here's the code for the Object class:

    Java Code:
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Rectangle;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.awt.event.MouseMotionListener;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    
    public class Card extends JPanel implements Runnable {
    	public static int x, y;
    
    	public int cardID;
    	public int playerID;
    	private boolean turnOn;
    
    	public Card(int x, int y, int width, int height, int cardID, int playerID) {
    		System.out.println("Creating new Card().");
    		System.out.println(x + " / " + y);
    		Thread thread2 = new Thread(this);
    		thread2.start();
    		Main.frame.add(this);
    	}
    
    	@Override
    	public void run() {
    		System.out.println("Card.run() "+ (this));
    		while (turnOn) {
    			this.repaint();
    			try {
    				Thread.sleep(17);
    			} catch (InterruptedException e) {
    				e.printStackTrace();
    			}
    		}
    	}
    
    	@Override
    	protected void paintComponent(Graphics g) {
    		g.setColor(Color.PINK);
    		g.fillOval(this.x, this.y, 200, 200);
    	}
    }

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,882
    Rep Power
    8

    Default Re: Multiple JPanels; paint not working.

    You're adding multiple components to your frame:

    Main.frame.add(this);
    frame.add(new Main());

    You either need to use a layout manager (the default is BorderLayout, which means you're adding both components to the same location, which won't work) or decide which one of your components you actually need.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Join Date
    Mar 2014
    Posts
    72
    Rep Power
    0

    Default Re: Multiple JPanels; paint not working.

    But the Main class doesn't have a paint function so shouldn't i see what the Main.frame.add(this) paints?
    Also I tried adding: "frame.setLayout(null);" right after Frame frame = new Frame(" "); but it didn't work... :(

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,882
    Rep Power
    8

    Default Re: Multiple JPanels; paint not working.

    Quote Originally Posted by supremegrandruler View Post
    But the Main class doesn't have a paint function so shouldn't i see what the Main.frame.add(this) paints?
    What makes you believe this?

    Quote Originally Posted by supremegrandruler View Post
    Also I tried adding: "frame.setLayout(null);" right after Frame frame = new Frame(" "); but it didn't work... :(
    You should almost never use a null layout. Recommended reading: How to Use BorderLayout (The Java™ Tutorials > Creating a GUI With JFC/Swing > Laying Out Components Within a Container)
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Join Date
    Mar 2014
    Posts
    72
    Rep Power
    0

    Default Re: Multiple JPanels; paint not working.

    Quote Originally Posted by KevinWorkman View Post
    What makes you believe this?
    Because nothing is painted, no? But I removed the "frame.add(Main());" and it still didn't paint anything. :(
    I don't really need a layout right now because I don't plan on having my frame be resizeable (for the moment).

  6. #6
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,882
    Rep Power
    8

    Default Re: Multiple JPanels; paint not working.

    Just because you don't want to resize your JFrame does not mean you shouldn't use a layout manager. In fact, you already *are* using a layout manager, and your problems are at least partially due to improperly using it.

    What do you expect to be drawn? What are the values of x and y in your paintComponent() method? What is the background color of that JPanel? What happens if you change the background color? Where is you call to super.paintComponent()?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    Join Date
    Mar 2014
    Posts
    72
    Rep Power
    0

    Default Re: Multiple JPanels; paint not working.

    Why do I need a layout manager if I don't plan on resizing my frame?
    Also, what about the other part of the problem?

    Actually now, only the first time an object is created it paints it, but not after that.

    The x / y are about 300 / 100 respectively and just a filled circle should be drawn.
    No background colors on either JPanel.
    I added super.paintCoponent(), thanks to you. :)

    EDIT: I think adding "setBounds" fixed it.
    Last edited by supremegrandruler; 06-17-2014 at 06:11 PM.

  8. #8
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,882
    Rep Power
    8

    Default Re: Multiple JPanels; paint not working.

    Quote Originally Posted by supremegrandruler View Post
    Why do I need a layout manager if I don't plan on resizing my frame?
    Because layout managers are also responsible for managing multiple components within a container. This is at least part of your problem.

    Quote Originally Posted by supremegrandruler View Post
    Also, what about the other part of the problem?
    It's unclear what you expect to be drawn. You have x and y variables inside your Card class that are static for some reason, but then you use non-static x and y variables in your paintComponent() method. Those x and y variables depend on the position of the component, which depends on your *layout manager*.

    Quote Originally Posted by supremegrandruler View Post
    But I fixed it and I have no idea what I did. :/ ??? I tried breaking it but I can't break it. :(

    The x / y are about 300 / 100 each.
    Are you saying your problem is gone? Removing the second component certainly helped.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  9. #9
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,749
    Rep Power
    5

    Default Re: Multiple JPanels; paint not working.

    Quote Originally Posted by supremegrandruler View Post
    Why do I need a layout manager if I don't plan on resizing my frame?
    That's like asking "Why would I fish with bait when I can just hook the fish in the gut?". Yes you can, but using bait is waaaaaaaaaaaaaaaaaaaaaaay easier once you properly learn how to handle a fishing rod.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  10. #10
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,415
    Rep Power
    5

    Default Re: Multiple JPanels; paint not working.

    You should also put a super.paintComponent(g) statement as the first statement in your paintComponent method. And you should get in the habit of declaring your shared boolean flags (and other variables as required) as volatile. Otherwise, the compiler could "optimize" your code so it wouldn't work.

    Regards,
    Jim
    Last edited by jim829; 06-18-2014 at 01:55 AM.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  11. #11
    Join Date
    Mar 2014
    Posts
    72
    Rep Power
    0

    Default Re: Multiple JPanels; paint not working.

    Quote Originally Posted by KevinWorkman View Post
    Are you saying your problem is gone? Removing the second component certainly helped.
    Seems like it's gone but I actually didn't remove it. I commented it, then it worked, then I uncommented it, and it still worked.

    Talking about layouts, I read all about the different layouts, and please tell me if I'm wrong, but all the components are based on "grid" except null layout, right? Like gridbag layout, card layout, etc. They are all grid based, no?
    Also, if I want to FORCE columns and rows in my layout so that component are not placed automatically in the next available grid tile, I have to add empty components in the grid tiles I want unused, right?

    EDIT: Problem continues when I change the "fillOval(this.x, this.y, 200, 200)" to "fillOval(this.x, this.y, this.width, this.height);".
    Last edited by supremegrandruler; 06-18-2014 at 03:21 AM.

  12. #12
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,882
    Rep Power
    8

    Default Re: Multiple JPanels; paint not working.

    Quote Originally Posted by supremegrandruler View Post
    EDIT: Problem continues when I change the "fillOval(this.x, this.y, 200, 200)" to "fillOval(this.x, this.y, this.width, this.height);".
    Have you followed the advice you've been given? Can you post an MCVE with your updated code?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. multiple JPanels on a Jframe
    By floris in forum AWT / Swing
    Replies: 4
    Last Post: 04-20-2012, 01:08 PM
  2. Multiple JPanels in Swing?
    By zzuum in forum New To Java
    Replies: 11
    Last Post: 06-02-2011, 02:43 AM
  3. Multiple JPanels within a JFrame
    By horndinkle in forum New To Java
    Replies: 5
    Last Post: 01-09-2011, 07:51 PM
  4. Two JPanels and paint(Graphics)
    By ingfy in forum New To Java
    Replies: 3
    Last Post: 04-27-2010, 08:51 PM
  5. Multiple JPanels Not Working
    By jgezau in forum AWT / Swing
    Replies: 5
    Last Post: 04-17-2009, 04:52 AM

Posting Permissions

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