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.

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.
{
}
}

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.