SEARCH W7R

Showing posts with label JavaTutorials. Show all posts
Showing posts with label JavaTutorials. Show all posts

Tuesday, July 03, 2012

Java GUI Tutorials Archive

All You Need in One Location

Bookmark this resource before you forget too! This useful post contains a link to every Java GUI tutorial post on W7R Tech. Even if you have completed all of the W7R Java GUI tutorials, you can use this post refresh and expand your understanding of Java GUIs. Enjoy!



Don't forget to bookmark this resource! And make sure to subscribe to W7R Tech!

Sunday, July 01, 2012

Java GUI Tutorial 6 - JPanel as a Container

w7r.blogspot.com



JPanel as a Container

In this tutorial, a new class (extension of JPanel), MainPanel.java has been added to the source files and is going to act as a common container for all instances of the ButtonPanel we create. The ButtonPanels are able to operate independently of one another.


Friday, May 25, 2012

Simple Animations with JPanel’s Paint Method

Any instance or subclass of JPanel contains a repaint and paint method. The paint method can be used to draw content such as rectangles on to the JPanel. When you paint or draw objects on the JPanel you specify information such as the location and the width and height by means of parameters. For example to draw a rectangle you may use the syntax, graphics.drawRect(x,y, width, height);

Locations of objects are managed by having variables to describe them. The variable x, and y can store coordinates of the upper right hand point of an object we would draw/paint on the JPanel. The object location variables are important because they can be altered independantly of the rest of the panel. Changes to an object or shapes location are not displayed until paint or similar methods are called. Repainting of a JPanel can be triggered by minimizing and restoring the window that contains the JPanel.

By incrementing x or y variables the position at which a component is drawn will change. The old image is not wiped clean by the repaint or paint method implicitly. To repaint only the new image at its new location without leaving an imprint of the old one, you must repaint the background like so:
  1. Update x and y variables
  2. Repaint background
  3. Repaint the object with its new x and y values
In the following program two shapes, one rounded rectangle, and one oval are repainted to simulate movement on the computer screen. In this program sample the past images of the shapes are not removed. I did this to show you that cleaning of old drawings is not implicit with the repaint and paint method.
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;

/*
 * W7R.blogspot.com
 * Brian R. H.
 */

public class MyPanel extends JPanel implements ActionListener{
 int ovalX=400,ovalY=400,ovalWidth=50,ovalHeight=30;
 int rectX = 200, rectY = 200, rectWidth = 20, rectHeight = 10;
 int rectDx=10,  rectDy=0; //change in Y
 int ovalDx=-20, ovalDy=0; 

 //        1 tick/second
 Timer clock = new Timer(1000,this);

 MyPanel(){
  setSize(800,600);
  clock.start();
 }
 @Override
 public void paint(Graphics g){
  //  paint occurs when the method repaint() is called (see actionPerformed method )
  Graphics2D g2d = (Graphics2D) g;
  g2d.drawRoundRect(rectX, rectY, rectWidth, rectHeight, 10, 10);
  g2d.setColor(Color.blue);
  g2d.drawOval(ovalX, ovalY, ovalWidth, ovalHeight);
 }
 public static void main(String[] args) {
  JFrame j = new JFrame("Title");
  JPanel p = new MyPanel();
  j.setContentPane(p);
  j.setSize(800,600);
  j.setVisible(true);
  p.setVisible(true);
 }
 private void updateVectors() {
  rectX+=rectDx;
  rectY+=rectDy;
  ovalX+=ovalDx;
  ovalY+=ovalDy;
 }
 @Override
 public void actionPerformed(ActionEvent arg0) {
  repaint();
  updateVectors();
 }
}

Thursday, May 10, 2012

The Substring Method of Java

Substring is a method/function used to select smaller strings in the String it was called on.

For example, "CatDog" is a string that contains two smaller strings, "Cat" and "Dog"
To break this "CatDog" string into the two seperate strings, "Cat" and "Dog" one would use the substring method.
Example #1:
//declaration and initialization of string1
String string1 = "CatDog";
 //prints out result of substring(2) 
System.out.println(string1.substring(2));
After running the code above you it is easy to realize we have a problem. Your console should read "tDog" which means we have not split the string1 up as we had planned. The reason is because the parameter we assigned to be 2 should have been 3. Below is the code for what we truly wanted. Goes to show that print statements are a good way to test/debug your programs!
String string1 = "CatDog";    //declaration and initialization of string1
System.out.println(string1.substring(2));    //prints result of substring(2) 
The first and only parameter in example one specified the first character of the string to start the substring from and then because there was no second integer parameter the method returned that first letter and all the characters that followed it in the string.
Example #2:
String quote = "code the gnar";
String word = quote.substring(1); 
System.out.println("original value: "+quote);
System.out.println("final value: "+word);
It is important to realize that there are more than 1 ways of calling for a substring() on a string. In the following example you will see an example using the substring(int,int) syntax. See if you can figure out what the 1st and 2nd integer paremeters are doing!
Example #3:
String alphabet = "abcdefghijklmnopqrstuvwxyz";
String letters = alphabet.substring(5,0); 
System.out.println("original value: "+alphabet);
System.out.println("final value: "+letters);

Click here to see the role of the 1st integer.
Starting index for the string

Click here to see the role of the 2nd integer.
How far after starting index to span