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.

Results 1 to 7 of 7

Thread: A Pixel Out Of Place

  1. #1
    Member tyeeeee1's Avatar
    Join Date
    Sep 2012
    Posts
    61
    Thanks
    31
    Thanked 2 Times in 2 Posts

    Default A Pixel Out Of Place

    Hey,

    I've been messing around with different parts of swing over the past few days and, thanks to two people on this forum, I've finally figured out how to give my JButtons custom normal, rollover and pressed looks. I was quite excited just to see the code I've written working so I didn't notice this one, strange, little pixel in a spot where it shouldn't be.

    Image of the Pixel:


    As you can see in the above image there is an odd black pixel to the right of the 'New Game' button, no matter what state (normal, rollover or pressed) the pixel always stays there. I know that none of the test images that I've created have anything outside of them so the pixel cannot be from the image, I then looked at my code for anything that looked like it might somehow create a single black pixel but I don't see anything.

    I'm just getting into Swing so I probably messed something up to cause the button. If anyone has a minute or two to check over my code for any mistake that could have caused the pixel, please do so.

    (The code for the button is in the 'main' method of the 'Game' class. The button shown in the above picture it newGameButton)
    import java.io.*;
    import javax.swing.*; //Needed for the swing class
    import java.awt.*;
     
    class SavingClass
    {
    	public static void saveGame() throws Exception //Change the return type (void) to something else later.
    	{
    		BufferedWriter saveFile = new BufferedWriter(new FileWriter("TextSave.txt")); 
     
    		saveFile.write("test1, test2"); //Saves the variables listed to the first line of the CSV file. If you wanted to make another line then you would end this one with \r\n so that a new line is created. Then you would make another write method
     
    		saveFile.close(); //Closes the file writer now that we're finished.
    	}
    }
     
    class LoadingClass
    {
     
    	public static void loadGame() throws Exception //Change the return type (void) to something else later.
    	{
    		String exampleString1;
    		String exampleString2;
     
    		BufferedReader saveFile = new BufferedReader(new FileReader("TextSave.txt"));
     
    		exampleString1 = saveFile.readLine();
    		exampleString2 = saveFile.readLine();
     
    		saveFile.close();
     
    		System.out.println(""+exampleString1+" "+exampleString2+"");
    	}
    }
     
    class CreateGameWindow extends JFrame
    {
    	//The below method is used for the creation of the game window.
    	public CreateGameWindow() //If there is a return type set for this method then the code below will not show.
    	{
    		setSize(640, 480); //Sets the size of the game window
    		setLocationRelativeTo(null); //Centers the window on the users screen.
    		setTitle("The Game - By Tyler MacEachern"); //Sets the title of the game window
    		setDefaultCloseOperation(EXIT_ON_CLOSE); //Basically this makes it so when you hit the red X the window and program will close.
    	}
    }
     
    class Game extends JFrame
    {
    	public static void main(String args[]) //This is the main menu of the game, the method has been called 'main' so it will be the first thing to run when the program is loaded.
    	{
     
    		CreateGameWindow gameWindow = new CreateGameWindow();
     
    		//Create the main menu's look with buttons and that
    		JPanel gamePanel = new JPanel(); //Creates a new JPanel to put everything on.
    		gameWindow.getContentPane().add(gamePanel); //Adds the previously created JPanel ontop of the gameWindow Frame.
     
    		gamePanel.setLayout(null); //Makes it so you can manually positon everything using XY coords.
    		Insets insets = gamePanel.getInsets(); //Gets the amount of space that the gameWindow's boarders take up so that you can use it to better position your JButtons and stuff.
     
    		JButton newGameButton = new JButton("New Game"); //Creates a button object for the New Game button on the main menu.
    		JButton loadGameButton = new JButton("Load Game"); //Creates a button object for the Load Game button on the main menu.
    		JButton optionsButton = new JButton("Options"); //Creates a button object for the Options button on the main menu.
     
    		gamePanel.add(newGameButton); //Adds the New Game button to the JPanel
    		newGameButton.setBounds(240 + insets.left, 150 + insets.top, 120, 30);
    		newGameButton.setBorder(null); //Disables the thin black box around the button
    		newGameButton.setFocusPainted(false); //Makes it so that when you select the button there wont be a little box around the words to signify that the button is currently selected.
    		newGameButton.setContentAreaFilled(false); //Gets rid of the grey fill when you select the button.
    		Icon newGameButtonIcon = new ImageIcon("testbutton.png"); //Creates a new ImageIcon object that will be used as the newGameButton.
    		newGameButton.setIcon(newGameButtonIcon); //Covers the newGameButton icon with the testbutton.png image.
    		Icon newGameButtonIconRollover = new ImageIcon("testbutton_rollover.png"); //Creates a new ImageIcon object that will be used as the rollover version of the newGameButton.
    		newGameButton.setRolloverIcon(newGameButtonIconRollover); //Tells the newGameButton to, when the mouse is over it, change to the rollover version of the newGamebutton.
    		Icon newGameButtonIconPressed = new ImageIcon("testbutton_pressed.png");
    		newGameButton.setPressedIcon(newGameButtonIconPressed);
    		//
    		gamePanel.add(loadGameButton); //Adds the Load Game button to the JPanel
    		loadGameButton.setBounds(240 + insets.left, 210 + insets.top, 120, 30);
    		loadGameButton.setBorder(null); //Disables the thin black box around the button
    		loadGameButton.setFocusPainted(false); //Makes it so that when you select the button there wont be a little box around the words to signify that the button is currently selected.
    		loadGameButton.setContentAreaFilled(false); //Gets rid of the grey fill when you select the button.
    		//
    		gamePanel.add(optionsButton); //Adds the Options Game button to the JPanel
    		optionsButton.setBounds(240 + insets.left, 270 + insets.top, 120, 30);
    		optionsButton.setBorder(null); //Disables the thin black box around the button
    		optionsButton.setFocusPainted(false); //Makes it so that when you select the button there wont be a little box around the words to signify that the button is currently selected.
    		optionsButton.setContentAreaFilled(false); //Gets rid of the grey fill when you select the button.
     
     
     
    		gameWindow.setVisible(true);
    	}
     
    	public static void loadingMenu() //Change the return type (void) to something else later.
    	{
    	}
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: A Pixel Out Of Place

    Where is the image displayed?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member tyeeeee1's Avatar
    Join Date
    Sep 2012
    Posts
    61
    Thanks
    31
    Thanked 2 Times in 2 Posts

    Default Re: A Pixel Out Of Place

    The image is right beneath where it says "Image of the Pixel:". Your browser might be blocking it or something, here is a direct link http://i49.tinypic.com/2hnniah.png

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: A Pixel Out Of Place

    When I execute the program there is an image to the left of the red button.
    There is no image there in the image of the GUI that you posted.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member tyeeeee1's Avatar
    Join Date
    Sep 2012
    Posts
    61
    Thanks
    31
    Thanked 2 Times in 2 Posts

    Default Re: A Pixel Out Of Place

    Do you see the words 'New Game' to the left of the red button, if so I think I've figured out what's happening.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: A Pixel Out Of Place

    The image is on the left, the red button with "New Game" is on the right.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member tyeeeee1's Avatar
    Join Date
    Sep 2012
    Posts
    61
    Thanks
    31
    Thanked 2 Times in 2 Posts

    Default Re: A Pixel Out Of Place

    I've figured out the cause, it took a bit of guess work but thanks to you I've narrowed it down. I know know that the line 'JButton newGameButton = new JButton("New Game");' was still being placed onto the JPlane even though I had though that by using 'newGameButton.setIcon(newGameButtonIcon);' would get rid of the words and put the iamge in their place. To solve my problem I had to change 'JButton newGameButton = new JButton("New Game");' to 'JButton newGameButton = new JButton("");' so that the one pixel of the words 'New Game' wouldn't be generated.

    The problem is solved, thanks for the help and I hope if anyone else hits this error they can benefit from what I just learned as well.

Similar Threads

  1. Retrieve pixel array after an image is taken
    By Yoyo_Guru in forum Android Development
    Replies: 0
    Last Post: July 27th, 2012, 02:28 PM
  2. Can not get individual pixel values using getRGB
    By maheshchavan in forum AWT / Java Swing
    Replies: 1
    Last Post: June 17th, 2012, 11:37 AM
  3. Pixel grid vs Hitbox (collision detection)
    By DOLZero in forum Java Theory & Questions
    Replies: 7
    Last Post: April 16th, 2012, 03:22 PM
  4. returning color of pixel in loop
    By Vergil333@gmail.com in forum Loops & Control Statements
    Replies: 2
    Last Post: March 6th, 2012, 09:14 AM
  5. Get Pixel
    By Ophe in forum What's Wrong With My Code?
    Replies: 7
    Last Post: January 27th, 2011, 04:24 PM