Studyon Minte9.com
Head First (Java)




Swing GUI



	// Frame, Button / JFrame, JButton
	// Listener / button.addListener(this)
	// Graphics / g.fillRectangle()
	// Layout / frame.getContentPane().add(BorderLayout.SOUTH, button)

* Frame, Button

	A JFrame is me object that represents a window on the screen. It's where you put all the interface 
	things like buttons, checkboxes, text fields, and so on.

	%java
		package testingapp;
		import javax.swing.*;

		public class TestingApp {
		    public static void main(String[] args) {
		     
			JFrame frame = new JFrame(); 
			    // make a frame
			JButton button = new JButton("click me"); 
			    // make a button
			
			frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			    // when close window, the program will stop running
			
			frame.getContentPane().add(button); 
			    // add button to frame
			frame.setSize(300, 300);
			    // frame size (pixels)
			frame.setVisible(true);
			    // make frame visible
		    }
		}


* Listener

	The Swing GUI components are event sources. In Java terms, an event source is an object that can 
	turm user actions (click a mouse, type a key, close a window) into events. An event source (like a 
	button) creates an event object when the user does something that matters (like click the button).

	%java

		package testingapp;
		import javax.swing.*;
		import java.awt.event.*; // new import statement for events

		public class TestingApp {
		    public static void main(String[] args) {
			
			SimpleGui gui = new SimpleGui();
			gui.run();
		    }
		}

		class SimpleGui implements ActionListener {
			// implements the listener interface
		    
		    JFrame frame = new JFrame(); 
		    JButton button = new JButton("click me");
			
		    @Override
		    public void actionPerformed(ActionEvent event) {
			button.setText("I've been clicked!");
				// On button click the text is changed
		    }
		    
		    public void run() {

			button.addActionListener(this);
			    // Add object to button list of listeners
			    // The argument must be an object from class that implements ActionListener
			
			frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        
			frame.getContentPane().add(button); 
			frame.setSize(300, 300);
			frame.setVisible(true);
		    }
		}



* Graphics

	If you want to put your own graphics on the screen, your best bet is to make your own paintable 
	widget. You plop that widget on the frame, just like a button or any other widget, but when it shows up 
	it will have your images on it.
	

	* Draw a rectangle

	%java
		package testingapp;
		import java.awt.*;
		import javax.swing.*;

		public class TestingApp {
		    public static void main(String[] args) {

			JFrame frame = new JFrame(); 
			
			 MyDrawPanel panel = new MyDrawPanel();
			
			frame.getContentPane().add(panel); 
			frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   
			frame.setSize(300, 300);
			frame.setVisible(true);
		    }
		}

		class MyDrawPanel extends JPanel {
		    
		    @Override
		    public void paintComponent(Graphics g) {
			    // The system calls Graphics, a drawing surface that you may paint
			
			g.setColor(Color.orange);
			g.fillRect(20,50,100,100);
		    }
		}

	
	* Image

	%java
		package testingapp;
		import java.awt.*;
		import javax.swing.*;

		public class TestingApp {
		    public static void main(String[] args) {

			JFrame frame = new JFrame(); 
			
			 MyDrawPanel panel = new MyDrawPanel();

			frame.getContentPane().add(panel); 
			frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   
			frame.setSize(300, 300);
			frame.setVisible(true);
		    }
		}

		class MyDrawPanel extends JPanel {
		    
		    @Override
		    public void paintComponent(Graphics g) {
		    
			Image image = new ImageIcon("einstein.jpg").getImage();
			g.drawImage(image, 5, 5, this);
				// image on project root (on level with build, src)
		    }
		}

	

	* Circle, Gadient

	%java
		package testingapp;
		import java.awt.*;
		import javax.swing.*;

		public class TestingApp {
		    public static void main(String[] args) {

			JFrame frame = new JFrame(); 
			
			 MyDrawPanel panel = new MyDrawPanel();

			frame.getContentPane().add(panel); 
			frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   
			frame.setSize(300, 300);
			frame.setVisible(true);
		    }
		}

		class MyDrawPanel extends JPanel {
		    
		    @Override
		    public void paintComponent(Graphics g) {
		    
			Graphics2D g2d = (Graphics2D) g;
			GradientPaint gradient = new GradientPaint(70,70,Color.blue,150,150,Color.orange);
			    // 70,70 starting point
			
			g2d.setPaint(gradient);
			g2d.fillOval(70, 70, 100, 100);
			    // fill oval with whatever is loaded on your paintbrush (gradient)
		    }
		}



* Layout
	
	By default, a frame has five regions you can add to. You can add only onething to each region of a 
	frame, but don't panic! That one thing might be a panel that holds three other things including a panel 
	that holds two more things, etc.

	%java
		package testingapp;
		import javax.swing.*;
		import java.awt.*;

		public class TestingApp {
		    public static void main(String[] args) {

			SimpleGui gui = new SimpleGui();
			gui.run();
		    }
		}

		class SimpleGui {

		    JFrame frame = new JFrame(); 
		    JButton button = new JButton("click me");
		    MyDrawPanel drawPanel = new MyDrawPanel();

		    public void run() {

			frame.getContentPane().add(BorderLayout.SOUTH, button);
			frame.getContentPane().add(BorderLayout.CENTER, drawPanel); 
			    // Add the two widget to the two region of the frame
			
			frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
			frame.setSize(300, 300);
			frame.setVisible(true);
		    }
		}

		class MyDrawPanel extends JPanel {
				    
		    @Override
		    public void paintComponent(Graphics g) {

			g.setColor(Color.orange);
			g.fillOval(70,70,100,100);
		    }
		}