Quantcast
Channel: flounderCode()
Viewing all articles
Browse latest Browse all 10

Beginner GUI and Mouse input

$
0
0

So after finishing the TIC TAC TOE game I decided that I was going to go out and try and find a list of coding challenges that other developers thought would be good to test your skills with. I found several lists and the one I stuck with listed Conway’s Game of Life as a next step. I learned about the game and looked at a bunch of code but I came to the realization that I did not have the strength in GUI’s to be able to complete and understand the code. So, I decided to take on a few basic GUI challenges in hopes of building my strengths here. Below is a simple code that utilizes several packages such as awt and swing. As I am still in my infancy as far as understanding the capabilities of these packages, I attempted to put some good documentation for the reader in the code.

//BTG 1.29.14

/*
 *This is a simple program that displays a JFrame canvas and uses a MouseListener
 *to implement changes.  A red box will display and upon user Mouse click, the box changes
 *to green and gives the exact location of the user click.
 *See code for detailed comments .
 */

//Abstract Window Toolkit uses components and containers to display GUI
import java.awt.*;
import javax.swing.JFrame;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;

//This code extends the main so that the new class(mouse) inherits from class Canvas.  Canvas
//provides a blank rectangular area that can trap user input
public class mouse extends Canvas implements MouseListener
{
    
            
        private Color currentColor;
        private String currentMessage;
        private String clickLocation;
        
            
            //initial variable settings
            public mouse()
            {
                
                addMouseListener(this);
                
                currentColor = Color.RED;
                currentMessage = "The square is red.";
                clickLocation = "Click for location.";
           
            }
            
            
            //Initial graphics settings
            public void paint(Graphics g)
            {
                
                g.setColor(Color.BLACK);
                g.setFont(Font.decode("sansserif-ITALIC-BOLD-10"));
                g.drawString(currentMessage, 100, 100);
                
                g.setColor(currentColor);
                g.fillRect(250, 100, 100, 100);
                
                g.setColor(Color.BLACK);
                g.setFont(Font.decode("sansserif-ITALIC-BOLD-10"));
                g.drawString(clickLocation, 100, 115);
                
            }
            
            
            
            public void mouseClicked(MouseEvent evt)
            {
                

                currentMessage = "The square is green."; 
                currentColor = Color.GREEN;
                clickLocation = "You clicked at (" + evt.getX() + "," + evt.getY() + ")";
                
                //calls the paint method again with the new above variables
                repaint();
                
                

               
                
            }

            public void mousePressed( MouseEvent evt )
            {
            }
            
            public void mouseReleased( MouseEvent evt )
            {
            }
            
            public void mouseEntered( MouseEvent evt )
            {
                
            }
            
            public void mouseExited( MouseEvent evt )
            {
                
            }

            public static void main(String[] args)
            {
                
                // You can change the title or size here if you want.
                JFrame win = new JFrame("MouseDemo");
                win.setSize(400,300);
                win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                win.add( new mouse() );
                win.setVisible(true);
                
            }//main
    
    
}//class


Viewing all articles
Browse latest Browse all 10

Trending Articles