Results 1 to 15 of 15
  1. #1
    Join Date
    Jul 2015
    Posts
    11
    Rep Power
    0

    Default JButton or graphics

    Hi. I'm new to java and am making a game. I have this code in one class:
    Java Code:
    public Window(int width, int height, String title, Game game)
    	{	
    		frame = new JFrame(title);
    		Dimension size = new Dimension(width, height);
    		
    		frame.setPreferredSize(size);
    		frame.setMaximumSize(size);
    		frame.setMinimumSize(size);
    		
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setResizable(false);
    		frame.setLocationRelativeTo(null);
    		
    		start = new JButton("Start");
    		Dimension startSize = new Dimension(300, 75);
    		JPanel panel = new JPanel();
    		
    		start.setFont(new Font("TimesNewRoman", Font.BOLD, 25));
    		start.setLocation(0, 0);
    		start.setVisible(true);
    		start.setPreferredSize(startSize);
    		
    	    panel.add(start);
    		
    		frame.add(panel, BorderLayout.CENTER);
    		
    		frame.add(game);
    		
    		frame.setVisible(true);
    		game.start();
    	}
    And this in another:
    Java Code:
    BufferStrategy bs = getBufferStrategy();
    		
    		if(bs == null)
    		{
    			createBufferStrategy(3);
    			return;
    		}
    		
    		gs.render();
    		
    		for(int i = 0; i < pixels.length; i++)
    		{
    			pixels[i] = gs.pixels[i];
    		}
    		
    		Graphics g = bs.getDrawGraphics();
    		
    		g.setColor(Color.BLACK);
    		g.fillRect(0, 0, width/2, height/2);
    		g.fillRect(width/2, height/2, width/2, height/2);
    		g.fillRect(width/2, 0, width/2, height/2);
    		g.fillRect(0, height/2, width/2, height/2);
    		g.drawImage(img, 0, 0, width, height, null); 
    		drawColoredRect(g, Color.RED, 153, 191, 641, 214);
    		drawColoredRect(g, Color.BLUE, 154, 192, 642, 215);
    		drawColoredRect(g, Color.GREEN, 155, 193, 643, 216);
    		drawColoredRect(g, Color.YELLOW, 156, 194, 644, 217);
    		drawColoredRect(g, Color.ORANGE, 157, 195, 645, 218);
    		drawColoredRect(g, Color.MAGENTA, 158, 196, 646, 219);
    		drawColoredRect(g, Color.WHITE, 159, 197, 647, 220);
    		g.setFont(new Font("TimesNewRoman", Font.BOLD, 50));
    		g.setColor(Color.RED);
    		g.drawString("Cool Game", 350, 312);
    		
    		g.dispose();
    		bs.show();
    The problem is, if i do (In the first code snippet)
    Java Code:
    frame.add(game)
    first, like it is, it does only the game. If i do
    Java Code:
    frame.add(panel, BorderLayout.CENTER);
    it adds the JButton and lots of blank space. How do I add them both? Please help .

  2. #2
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,995
    Rep Power
    9

    Default Re: JButton or graphics

    If you want to use a BorderLayout, you'd have to specify that in the frame:
    Java Code:
    frame.setLayout( new BorderLayout());
    Then if you want to add different components to different areas, you have to specify those different areas:
    Java Code:
    frame.add(panel, BorderLayout.NORTH);
    frame.add(game, BorderLayout.CENTER);
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  3. #3
    Join Date
    Jul 2015
    Posts
    11
    Rep Power
    0

    Default Re: JButton or graphics

    Ok thanks. It works with both, but I have 2 problems. 1. I want the button in the center, but if I don't do
    Java Code:
    frame.add(game, BorderLayout.CENTER);
    then the game doesn't work. 2. The graphics to the left and right of the JButton are just plain white. How would I fix this?

  4. #4
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,995
    Rep Power
    9

    Default Re: JButton or graphics

    With only a small portion of your code visible, that's hard to tell.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: JButton or graphics

    Have you checked out the tutorials in my signature. They provide examples of most of the layout managers.
    Click on big index and scroll down until you see them listed. You may even find one that better suits your
    purpose.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  6. #6
    Join Date
    Jul 2015
    Posts
    11
    Rep Power
    0

    Default Re: JButton or graphics

    I'm not entirely certain what else to show. I've already showed the graphics code and window code in my first post...

  7. #7
    Join Date
    Jul 2015
    Posts
    11
    Rep Power
    0

    Default Re: JButton or graphics

    Ok thanks I'll try!

  8. #8
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,995
    Rep Power
    9

    Default Re: JButton or graphics

    Quote Originally Posted by ZachAttack1170 View Post
    I'm not entirely certain what else to show. I've already showed the graphics code and window code in my first post...
    We need code that we can copy and paste in an editor to see what goes wrong and how to fix. I am not going to add random stuff to make it work.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  9. #9
    Join Date
    Jul 2015
    Posts
    11
    Rep Power
    0

    Default Re: JButton or graphics

    Ok... I can give you the entire code:

    Game Class:
    Java Code:
    package com.Zach.Game;
    
    import java.awt.Canvas;
    import java.awt.Color;
    import java.awt.Font;
    import java.awt.Graphics;
    import java.awt.image.BufferStrategy;
    import java.awt.image.BufferedImage;
    import java.awt.image.DataBufferInt;
    
    import com.Zach.Graphics.TitleScreenGraphics;
    
    public class Game extends Canvas implements Runnable
    {	
    	private static final long serialVersionUID = -6808428148573621796L;
    	
    	public static int width = 950, height = 650, frames;
    	
    	private Thread thread;
    	
    	private boolean running = true;
    	
    	private TitleScreenGraphics gs;
    	
    	private BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    	
    	private int[] pixels = ((DataBufferInt) img.getRaster().getDataBuffer()).getData();
    	
    	public Game()
    	{	
    		gs = new TitleScreenGraphics(width, height);
    		new Window(width, height, "2D Game", this);
    	}
    	
    	public static void main(String[] args) 
    	{	
    		new Game();
    	}
    	
    	public synchronized void start()
    	{
    		thread = new Thread(this, "Display");
    		thread.start();
    		running = true;
    	}
    	
    	public synchronized void stop()
    	{
    		try
    		{
    			thread.join();
    		} 
    		catch(InterruptedException e)
    		{
    			e.printStackTrace();
    		}
    	}
    	
    	public void run() 
    	{
    		long lastTime = System.nanoTime();
    		final double ns = 1000000000.0 / 60.0;
    		double delta = 0;
    		
    		while(running)
    		{
    			long now = System.nanoTime();
    			delta += (now - lastTime) / ns;
    			
    			while(delta >= 1)
    			{
    				tick();
    				delta--;
    			}
    			render();
    			
    		}
    		stop();
    	}
    	
    	public void tick()
    	{
    		
    	}
    	
    	public void render()
    	{
    		BufferStrategy bs = getBufferStrategy();
    		
    		if(bs == null)
    		{
    			createBufferStrategy(3);
    			return;
    		}
    		
    		gs.render();
    		
    		for(int i = 0; i < pixels.length; i++)
    		{
    			pixels[i] = gs.pixels[i];
    		}
    		
    		Graphics g = bs.getDrawGraphics();
    		
    		g.setColor(Color.BLACK);
    		g.fillRect(0, 0, width/2, height/2);
    		g.fillRect(width/2, height/2, width/2, height/2);
    		g.fillRect(width/2, 0, width/2, height/2);
    		g.fillRect(0, height/2, width/2, height/2);
    		g.drawImage(img, 0, 0, width, height, null); 
    		drawColoredRect(g, Color.RED, 153, 191, 641, 214);
    		drawColoredRect(g, Color.BLUE, 154, 192, 642, 215);
    		drawColoredRect(g, Color.GREEN, 155, 193, 643, 216);
    		drawColoredRect(g, Color.YELLOW, 156, 194, 644, 217);
    		drawColoredRect(g, Color.ORANGE, 157, 195, 645, 218);
    		drawColoredRect(g, Color.MAGENTA, 158, 196, 646, 219);
    		drawColoredRect(g, Color.WHITE, 159, 197, 647, 220);
    		g.setFont(new Font("TimesNewRoman", Font.BOLD, 50));
    		g.setColor(Color.RED);
    		g.drawString("Cool Game", 350, 312);
    		
    		g.dispose();
    		bs.show();
    	}
    
    	private void drawColoredRect(Graphics g, Color color, int x, int y, int width, int height)
    	{
    		g.setColor(color);
    		g.drawRect(x, y, width, height);
    	}
    }
    Window Class:
    Java Code:
    package com.Zach.Game;
    
    import java.awt.BorderLayout;
    import java.awt.Canvas;
    import java.awt.Dimension;
    import java.awt.Font;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    
    public class Window extends Canvas
    {
    	private static final long serialVersionUID = -4523552041505200021L;
    	
    	public static JFrame frame;
    	
    	public static JButton start;
    	
    	public Window(int width, int height, String title, Game game)
    	{	
    		frame = new JFrame(title);
    		Dimension size = new Dimension(width, height);
    		
    		frame.setPreferredSize(size);
    		frame.setMaximumSize(size);
    		frame.setMinimumSize(size);
    		
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setResizable(false);
    		frame.setLocationRelativeTo(null);
    		
    		start = new JButton("Start");
    		Dimension startSize = new Dimension(300, 75);
    		JPanel panel = new JPanel();
    		
    		start.setFont(new Font("TimesNewRoman", Font.BOLD, 25));
    		start.setVisible(true);
    		start.setPreferredSize(startSize);
    		
    	    panel.add(start);
    		
    	    frame.setLayout( new BorderLayout());
    		frame.add(panel, BorderLayout.SOUTH);	
    		frame.add(game, BorderLayout.CENTER);
    		
    		frame.setVisible(true);
    		game.start();
    	}
    }
    TitleScreenGraphics (The little red lines moving across the screen):
    Java Code:
    package com.Zach.Graphics;
    
    import java.util.Random;
    
    public class TitleScreenGraphics
    {
    	@SuppressWarnings("unused")
    	private int width, height;
    	
    	public int[] pixels;
    	
    	int down = 0, right = 0, up = 628, left = 949;
    	
    	Random r = new Random();
    	public int rNum = 1 + r.nextInt(8);
    	
    	public TitleScreenGraphics(int width, int height)
    	{
    		this.width = width;
    		this.height = height;
    		pixels = new int[width * height];
    	}
    	
    	public void clear() //clears screen
    	{
    		for(int i = 0; i < pixels.length; i++)
    		{
    			pixels[i] = 0;
    		}
    	}
    	
    	public void render()
    	{	
    		for(int y = 198; y < 410; y++) //first part of loop = coords where starts, last = coords where ends
    		{
    			for(int x = 160; x < 800; x++)
    			{
    				switch(rNum)
    				{
    					case 1:
    						pixels[x + y * width] = 0xFF00FF;
    						break;
    					case 2:	
    						pixels[x + y * width] = 0xFFFFFF;
    						break;
    					case 3:
    						pixels[x + y * width] = 0x00FFFF;
    						break;
    					case 4:
    						pixels[x + y * width] = 0x00FF33;
    						break;
    					case 5:
    						pixels[x + y * width] = 0xFF00FF;
    						break;
    					case 6:
    						pixels[x + y * width] = 0x009900;
    						break;
    					case 7:
    						pixels[x + y * width] = 0xFF00FF;
    						break;
    					case 8:
    						pixels[x + y * width] = 0x0033CC;
    						break;
    					case 9:
    						pixels[x + y * width] = 0x000000;
    						break;
    				}
    				//pixels[0 + down * height] looks cool
    				if(down <= 627) pixels[0 + down * width] = 0xCC0033; //On title screen makes a dot that goes down
    				if(right <= 949) pixels[right + 627 * width] = 0xCC0033; //makes dot that goes right
    				if(up <= 628) pixels[949 + up * width] = 0xCC0033; //makes dot that goes up
    				if(up <= 949) pixels[left + 0 * width] = 0xCC0033; //makes dot that goes left
    			}
     		} //TODO Try to make this faster 
    		
    		down++;
    		right++;
    		if(up != 0) up--;
    		if(left != 0) left--;
    	}
    }
    OK, that's all the code... It's the beginning of a game that I started a few days ago, not much...

  10. #10
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,995
    Rep Power
    9

    Default Re: JButton or graphics

    When I run this, I can see the title screen and the button "Start". Respectively in CENTER and SOUTH. Seems to work fine.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  11. #11
    Join Date
    Jul 2015
    Posts
    11
    Rep Power
    0

    Default Re: JButton or graphics

    The graphics to the left and right of the button are just white, not what I set for them in the render method in the Game class. Also, I want the button in the center, I just put South because if I move the game to the North or South or anything else, it doesn't work. It only works in center, and if I put 2 things in center, it doesn't work. How would I fix these problems?

  12. #12
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: JButton or graphics

    What exactly is TitleScreenGraphics supposed to do? Why do you go to all the trouble to populate a pixel array for a simple rectangle in a BufferedImage. Why not just use setColor and fillRect?

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  13. #13
    Join Date
    Jul 2015
    Posts
    11
    Rep Power
    0

    Default Re: JButton or graphics

    The red dot moves. This game is supposed to be an example, I don't remember the word, but if I want to do something in a game, I'll have this to refer too. So if I ever want to do some animation thingy and don't remember how, I can refer back to here...

  14. #14
    Join Date
    Jul 2015
    Posts
    11
    Rep Power
    0

    Default Re: JButton or graphics

    OK let me rephrase what I have been asking. I have figured out that the white space is the JPanel I have, and I know how to set it invisible. But, if I set it invisible, the button which is inside the JPanel also goes invisible. How would I set the button visible and the Panel invisibible at the same time?
    Last edited by ZachAttack1170; 11-29-2015 at 08:54 PM.

  15. #15
    Join Date
    Jul 2015
    Posts
    11
    Rep Power
    0

    Default Re: JButton or graphics

    Thanks for the help, but I got rid of a Button and just used a Rectangle...

Similar Threads

  1. Centering graphics on the graphics window
    By janyc71876 in forum New To Java
    Replies: 5
    Last Post: 11-25-2013, 07:25 PM
  2. JButton takes graphics
    By maronski in forum New To Java
    Replies: 0
    Last Post: 04-27-2013, 07:03 AM
  3. Replies: 1
    Last Post: 01-15-2013, 06:44 PM
  4. ActionListener for JButton after changing Button to JButton
    By ravi.joshi53 in forum Java Applets
    Replies: 2
    Last Post: 10-07-2011, 07:35 AM
  5. Drawing a graphics onto another Graphics ?
    By Ziden in forum Java Applets
    Replies: 0
    Last Post: 01-08-2011, 07:30 PM

Tags for this Thread

Posting Permissions

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