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 6 of 6

Thread: Invisible Button turns White when moving my Mouse

  1. #1
    Junior Member
    Join Date
    May 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Invisible Button turns White when moving my Mouse

    Hello guys

    I got the following problem:

    When I start my GUI, the Window displays as it should. But as soon as I move my Mouse over the invisible Button it turns white and stays like that until I close the window.

    Two pictures to show you the problem:

    Ship.normal.pngShip.wrong.png

    NetBeansProjects.rar
    NetBeansProjects2.rar
    NetBeansProjects3.rar


    Tip:
    The invisible Button is called bMute

    I want the button to be transparent --> I want the window to stay the way it is in Picture Nr1.
    I programmed the Button to be transparent, so I don't get why he turns white...



    package battleship;
     
    import java.awt.BasicStroke;
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Font;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.image.BufferedImage;
    import java.awt.geom.AffineTransform;
    import java.awt.image.AffineTransformOp;
    import java.io.File;
    import java.io.IOException;
    import javax.imageio.ImageIO;
    import javax.swing.*;
    import java.awt.event.*;
     
     
    public class GUI extends JFrame implements ActionListener{
     
        /** Width of the JFrame **/
        public final int WIDTH = 1000;
        /** Height of the JFrame **/
        public final int HEIGHT = 600;
        /** Stroke of the Grid **/
        public final int STROKE = 2;
        /** Size of the Grid **/
        public final int GRID_SIZE = 400;
        /** Backgroundimage from the Grid **/
        private BufferedImage imgGrid;
        /** Images from the Ships **/
        private BufferedImage[] imgShips;
        /** Graphics to paint **/
     
        private BufferedImage imgContent;
        private Graphics2D gImage;
        /** Buttons **/
        JButton bMute = new JButton();
        JButton bExit = new JButton();
     
        public GUI(){ 
            super("Battleship");
     
            try{
                imgGrid = ImageIO.read(new File("src/battleship/images/grid.png"));
                imgShips = new BufferedImage[4];
                for(int i = 0; i < imgShips.length; i++){
                    imgShips[i] = ImageIO.read(new File("src/battleship/images/ship_" + (i+2) + ".png"));
                }
            }catch(IOException e){
                System.out.println(e.getMessage());
            }
     
            this.setSize(WIDTH, HEIGHT);
            this.setResizable(false);
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            this.setIconImage(imgShips[0]);
     
            this.setVisible(true);
     
            //Unvisible Buttons
            this.setLayout(null);
            this.add(bMute);
            bMute.setBounds(300, 510,100,60);        
            bMute.setBorderPainted(false);
            bMute.setOpaque(false);
            bMute.setContentAreaFilled(false);
     
     
            // Listener register
            bMute.addActionListener(this);
        }
     
        private BufferedImage paintBKG(){
            try{
                return ImageIO.read(new File("src/battleship/images/bkg_2.jpg"));
            }catch(IOException e){ 
                System.out.println(e.getMessage());
                return null;
            }
        } 
     
        private void paintGrids(Graphics2D g){
            gImage = imgGrid.createGraphics();
            gImage.setColor(new Color(0,0,0,150));
            gImage.setStroke(new BasicStroke(STROKE));
            gImage.drawRect(GRID_SIZE/20, GRID_SIZE/20, GRID_SIZE, GRID_SIZE);
            for(int i=1; i<=9; i++){
                gImage.fillRect((GRID_SIZE/20 + STROKE/2), 
                        (GRID_SIZE/20 - STROKE/2)+(i*GRID_SIZE/10), 
                        GRID_SIZE - STROKE, 
                        STROKE);
                gImage.fillRect((GRID_SIZE/20 - STROKE/2)+(i*GRID_SIZE/10), 
                        (GRID_SIZE/20 + STROKE/2), 
                        STROKE, 
                        GRID_SIZE - STROKE);
            }
            g.drawImage(imgGrid, 40, 100, null);
            g.drawImage(imgGrid, 520, 100, null);
        }
     
        public void paintText(Graphics2D g){
            Font font = new Font("28 Days Later", 4, 35); //Font, Options, Size
            //g.setColor(new Color(255,255,255,150));
            g.setFont(font);
            g.drawString("Your turn", 30, 80);
            g.drawString("Mute", WIDTH/3, HEIGHT-HEIGHT/40); 
            g.drawString("Exit",WIDTH-WIDTH/3, HEIGHT-HEIGHT/40); 
        }
     
        public void paintShips(Graphics2D g, int[] acoordinate, int isize, boolean bhorizontal){
            if(bhorizontal){
                g.drawImage(imgShips[isize-2], 170, 100, null);
            }else{
                g.drawImage(rotateImage(imgShips[isize-2]), 20, 10, null);
            }
        }
     
        @Override
        public void paint(Graphics g){
            imgContent = paintBKG();
            paintGrids(imgContent.createGraphics());
            paintText(imgContent.createGraphics());
     
            gImage.drawImage(imgShips[2], 90, 100, null);
            gImage.drawImage(rotateImage(imgShips[1]), 20, 10, null);
     
     
            //Test
           // gImage.drawImage(imgShips[1], 170, 100, null);
            g.drawImage(imgContent, 0, 0, null);
        } 
     
        public void target_positiv(int x, int y){
            gImage = imgGrid.createGraphics();
            gImage.setColor(new Color(255,0,0,120));
            gImage.fillRect(GRID_SIZE/20+STROKE/2+x*GRID_SIZE/10,GRID_SIZE/20+STROKE/2+y*GRID_SIZE/10,GRID_SIZE/10-STROKE,GRID_SIZE/10-STROKE);
            this.getGraphics().drawImage(imgGrid, 520, 100, null);
        } 
     
         public void target_negativ(int x, int y)
        {
            gImage = imgGrid.createGraphics();
            gImage.setColor(new Color(0,0,0,180));
            gImage.fillRect(GRID_SIZE/20+STROKE/2+x*GRID_SIZE/10,GRID_SIZE/20+STROKE/2+y*GRID_SIZE/10,GRID_SIZE/10-STROKE,GRID_SIZE/10-STROKE);
            this.getGraphics().drawImage(imgGrid, 520, 100, null);
        } 
     
        /**
         * Rotates an Image 90 degrees
         * @param src
         * @return 
         */
        private BufferedImage rotateImage(BufferedImage src){
            AffineTransform tx = new AffineTransform();
     
            // last, width = height and height = width :)
            tx.translate(src.getHeight() / 2,src.getWidth() / 2);
            tx.rotate(Math.PI / 2);
            // first - center image at the origin so rotate works OK
            tx.translate(-src.getWidth() / 2,-src.getHeight() / 2);
     
            AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_BILINEAR);
     
            // new destination image where height = width and width = height.
            BufferedImage rotatetImage =new BufferedImage(src.getHeight(), src.getWidth(), src.getType());
            op.filter(src, rotatetImage);
     
            return rotatetImage;
        }
     
        @Override
         public void actionPerformed(ActionEvent e)
        {
            if (e.getSource() == bMute){
                System.out.println("Mute Button Check");
            }
            if (e.getSource() == bExit){
                 System.out.println("Exit Button Check");
            }
     
        }
     
     
     
       /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            GUI bs = new GUI();
            bs.target_positiv(2, 2);
            bs.target_negativ(7, 2);
            bs.target_negativ(8, 6);
     
            int[] array = new int[2];
            array[0] = 0;
            array[1] = 1;
            bs.paintShips(imgContent.createGraphics(),test,3,true);
     
        }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,623
    Thanks
    45
    Thanked 1,958 Times in 1,932 Posts

    Default Re: Invisible Button turns White when moving my Mouse

    Can you explain what the problem is? You describe what the code does when it is executed, but don't say what you want it to do.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    May 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Invisible Button turns White when moving my Mouse

    Oh sure, I want the button to be transparent --> I want the window to stay the way it is in Picture Nr1.
    I programmed the Button to be transparent, so I don't get why he turns white...

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,623
    Thanks
    45
    Thanked 1,958 Times in 1,932 Posts

    Default Re: Invisible Button turns White when moving my Mouse

    The posted code does not compile without compiler errors. The errors need to be fixed before the code can be executed for testing.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    May 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Invisible Button turns White when moving my Mouse

    Is just one class of a bigger Project.

    Give me 5 mins, Ill try to upload the whole NetBeans Folder of the Project

    /edit:
    Added
    Unpack and put it in your Netbeans Folder and it should be working

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,623
    Thanks
    45
    Thanked 1,958 Times in 1,932 Posts

    Default Re: Invisible Button turns White when moving my Mouse

    Please make a small, complete program that compiles, executes and shows the problem.
    Do not post the code for the whole project. I don't use an IDE for testing code.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Alternate turns using Polymorphism and abstract classes
    By dalizardking427 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: April 13th, 2013, 07:48 PM
  2. counting mouse button clicks
    By bluewhale in forum What's Wrong With My Code?
    Replies: 5
    Last Post: June 8th, 2011, 09:51 AM
  3. Move button with mouse pressed
    By Stavros in forum What's Wrong With My Code?
    Replies: 0
    Last Post: May 10th, 2010, 08:45 AM