Friday, September 20, 2013

Java Flipbook with JLabel, Timer and Arrays

Images used in program

Flipbook video

SlideShow.java

Main Java class, JFrame that holds JPanel

package slideshow;

import javax.swing.JFrame;

public class SlideShow {
    
    public static void main(String[] args) {
        
        JFrame frame = new JFrame("Flipbook");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(new slildeShowPanel());
        frame.setResizable(false);
        frame.pack();
        frame.setVisible(true);
    }
}

slildeShowPanel.java

SlideShow Panel class, creates JPanel to hold components

package slideshow;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;

public class slildeShowPanel extends JPanel{
    
    ImageIcon[] imageArray = new ImageIcon[] {new ImageIcon("one.gif"), new ImageIcon("two.gif"), new ImageIcon("three.gif"), 
                                              new ImageIcon("four.gif"), new ImageIcon("five.gif"), new ImageIcon("six.gif") ,
                                              new ImageIcon("seven.gif")};
    public JLabel label;
    public Timer timer;
    public int counter;
    
    public slildeShowPanel()
    {
        timer = new Timer(100, new timerListener());
        timer.start();
        
        label = new JLabel();   
        
        counter = 0;

        add(label);    

        setPreferredSize(new Dimension(300, 150));
    }
    
    public class timerListener implements ActionListener
    {
        @Override
        public void actionPerformed(ActionEvent e)
        {
            counter++;
            
            if(counter >= imageArray.length)
            {
                counter = 0;
            }
            
            label.setIcon(imageArray[counter]);
        }        
    }    
}

Tuesday, September 10, 2013

Tic Tac Toe game with mouse listener


Main Java class (adds the JPanel and displays it)

package ttc;

import java.awt.Color;
import javax.swing.JFrame;

public class Ttc {

    public static void main(String[] args) {
        
        JFrame frame = new JFrame("Tic Tac Toe 2.0");
        frame.getContentPane().add(new ttcPanel());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setResizable(false);
        frame.setVisible(true);
        frame.setBackground(Color.BLACK);
        frame.pack();
        
    }
}

JPanel class (creates the JPanel and adds components to display)

package ttc;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JPanel;

public class ttcPanel extends JPanel{
    
    private int boxX, boxY, clickCounter;
    private Color box1, box2, box3, box4, box5, box6, box7, box8, box9;
          
    public ttcPanel()
    {       
        addMouseListener(new coordinateListener());
        
        clickCounter = 0;
        
        setPreferredSize(new Dimension(320, 410));
    }
    
    public class coordinateListener implements MouseListener
    {
        @Override
        public void mouseClicked(MouseEvent e)
        {
            boxX = e.getX();
            boxY = e.getY();
            
            clickCounter++;
        }
        
        @Override
        public void mousePressed(MouseEvent e) {}
        @Override
        public void mouseReleased(MouseEvent e) {}
        @Override
        public void mouseEntered(MouseEvent e) {}
        @Override
        public void mouseExited(MouseEvent e) {}
    }   

    @Override
        public void paintComponent(Graphics g)
    {
              
        if(clickCounter %2 != 0)
            {
                g.setColor(Color.RED);
            }
        else
            {
                g.setColor(Color.BLUE);
            }
        
        if(boxX >=10 && boxX <= 110 && boxY >=100 && boxY <=200)
        {
            g.fillRect(10, 100, 100, 100);
            box1 = g.getColor();
        }
        
        if(boxX >=110 && boxX <= 210 && boxY >=100 && boxY <=200)
        {
            g.fillRect(110, 100, 100, 100);
            box2 = g.getColor();
        }
        
        if(boxX >=210 && boxX <= 310 && boxY >=100 && boxY <=200)
        {
            g.fillRect(210, 100, 100, 100);
            box3 = g.getColor();
        }
        
        if(boxX >=10 && boxX <= 110 && boxY >=200 && boxY <=300)
        {
            g.fillRect(10, 200, 100, 100);
            box4 = g.getColor();
        }
        
        if(boxX >=110 && boxX <= 210 && boxY >=200 && boxY <=300)
        {
            g.fillRect(110, 200, 100, 100);
            box5 = g.getColor();
        }
        
        if(boxX >=210 && boxX <= 310 && boxY >=200 && boxY <=300)
        {
            g.fillRect(210, 200, 100, 100);
            box6 = g.getColor();
        }
        
        if(boxX >=10 && boxX <= 110 && boxY >=300 && boxY <=400)
        {
            g.fillRect(10, 300, 100, 100);
            box7 = g.getColor();
        }
        
        if(boxX >=110 && boxX <= 210 && boxY >=300 && boxY <=400)
        {
            g.fillRect(110, 300, 100, 100);
            box8 = g.getColor();
        }
        
        if(boxX >=210 && boxX <= 310 && boxY >=300 && boxY <=400)
        {
            g.fillRect(210, 300, 100, 100);
            box9 = g.getColor();
        }

        g.setColor(Color.WHITE);
               
        for(int x = 10; x<=210; x+=100)
        {
            for(int y = 100; y<=300; y+=100)
            {
                    g.drawRect(x, y, 100, 100);
            }        
        }     
        
        g.setFont(new Font("Helvetica", Font.PLAIN, 24));
        g.setColor(Color.GREEN);
        
        if(box1==Color.red && box2==Color.red && box3==Color.red)
        {
            g.drawString("The winner is: Red", 10, 50);
        }   
        else if(box4==Color.red && box5==Color.red && box6==Color.red)
        {
            g.drawString("The winner is: Red", 10, 50);
        }   
        else if(box7==Color.red && box8==Color.red && box9==Color.red)
        {
            g.drawString("The winner is: Red", 10, 50);
        }   
        else if(box1==Color.red && box4==Color.red && box7==Color.red)
        {
            g.drawString("The winner is: Red", 10, 50);
        }   
        else if(box2==Color.red && box5==Color.red && box8==Color.red)
        {
            g.drawString("The winner is: Red", 10, 50);
        }   
        else if(box3==Color.red && box6==Color.red && box9==Color.red)
        {
            g.drawString("The winner is: Red", 10, 50);
        }   
        else if(box1==Color.red && box5==Color.red && box9==Color.red)
        {
            g.drawString("The winner is: Red", 10, 50);
        }   
        else if(box3==Color.red && box5==Color.red && box7==Color.red)
        {
            g.drawString("The winner is: Red", 10, 50);
        }           
        else if(box1==Color.blue && box2==Color.blue && box3==Color.blue)
        {
            g.drawString("The winner is: blue", 10, 50);
        }   
        else if(box4==Color.blue && box5==Color.blue && box6==Color.blue)
        {
            g.drawString("The winner is: blue", 10, 50);
        }   
        else if(box7==Color.blue && box8==Color.blue && box9==Color.blue)
        {
            g.drawString("The winner is: blue", 10, 50);
        }   
        else if(box1==Color.blue && box4==Color.blue && box7==Color.blue)
        {
            g.drawString("The winner is: blue", 10, 50);
        }   
        else if(box2==Color.blue && box5==Color.blue && box8==Color.blue)
        {
            g.drawString("The winner is: blue", 10, 50);
        }   
        else if(box3==Color.blue && box6==Color.blue && box9==Color.blue)
        {
            g.drawString("The winner is: blue", 10, 50);
        }   
        else if(box1==Color.blue && box5==Color.blue && box9==Color.blue)
        {
            g.drawString("The winner is: blue", 10, 50);
        }   
        else if(box3==Color.blue && box5==Color.blue && box7==Color.blue)
        {
            g.drawString("The winner is: blue", 10, 50);
        }   
                           
        repaint();
    }          
}

See the program in action


Friday, September 6, 2013

Quadratic formula steps GUI program

This is just a practice problem I wrote that shows the steps to solving a quadratic equation

Main java class

package quadaraticformulagui;

import javax.swing.JFrame;

public class QuadaraticFormulaGui {

    public static void main(String[] args) {
        
        JFrame frame = new JFrame("Quadratic formula");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(new quadaraticFormulaGuiPanel());
        frame.pack();
        frame.setResizable(false);
        frame.setVisible(true);        
    }
}

The GUI ( Graphical User Interface )

package quadaraticformulagui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class quadaraticFormulaGuiPanel extends JPanel{
    
    private JLabel aLabel, bLabel, cLabel;
    private JTextField aText, bText, cText;
    private JButton calculate;
    private int aValue, bValue, cValue, oppositeB, bSquared, fourAC, twoA;
    
    public quadaraticFormulaGuiPanel()
    {    
        aLabel = new JLabel("A value:");
        aLabel.setForeground(Color.WHITE);
        bLabel = new JLabel("B value:");
        bLabel.setForeground(Color.WHITE);
        cLabel = new JLabel("C value:");
        cLabel.setForeground(Color.WHITE);
        
        aText = new JTextField(2);
        bText = new JTextField(2);
        cText = new JTextField(2);
        
        calculate = new JButton("Show the steps");
        calculate.addActionListener(new buttonListener());
        
        add(aLabel);
        add(aText);
        add(bLabel);
        add(bText);
        add(cLabel);     
        add(cText);
        
        add(calculate);

        setBackground(Color.BLACK);
        setPreferredSize(new Dimension(250, 450));
    }
    
        
    public class buttonListener implements ActionListener
    {
        @Override
        public void actionPerformed (ActionEvent e)
        {            
            aValue = Integer.parseInt(aText.getText());
            bValue = Integer.parseInt(bText.getText());
            cValue = Integer.parseInt(cText.getText());
            
            oppositeB = bValue - (bValue * 2);
            bSquared = bValue * bValue;
            fourAC = 4 * aValue * cValue;
            twoA = 2 * aValue;
        }
    }    
    
    @Override
    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        
        g.setFont(new Font("Serif", Font.PLAIN, 18));
        
        g.setColor(Color.WHITE);
        g.drawString("-(b) + ", 10, 90);
        g.drawString("-", 45, 100);
        g.drawString("_____________", 60, 68);
        g.drawString("|     2", 57, 84);
        g.drawString("| (b)   - 4 (a) (c)", 57, 100);
        g.drawString("__________________", 10, 110);
        g.drawString("2 (a)", 70, 140);
        g.drawString("( 1 )", 220, 110);
        
        g.drawString("-(" + bValue + ") + ", 10, 180);
        g.drawString("-", 45, 190);
        g.drawString("_____________", 60, 158);
        g.drawString("|     2", 57, 174);
        g.drawString("| (" + bValue + ")   - 4 (" + aValue + ")" + " (" + cValue + ")", 57, 190);
        g.drawString("__________________", 10, 200);
        g.drawString("2 (" + aValue + ")", 70, 230);
        g.drawString("( 2 )", 220, 200);
        
        g.drawString("" + oppositeB + " + ", 10, 270);
        g.drawString("-", 34, 280);
        g.drawString("_____________", 60, 248);
        g.drawString("|", 57, 264);
        g.drawString("| " + bSquared + " - " + fourAC, 57, 280);
        g.drawString("__________________", 10, 290);
        g.drawString("" + twoA, 70, 320);
        g.drawString("( 3 )", 220, 290);
        
        g.drawString("" + oppositeB + " + ", 10, 360);
        g.drawString("-", 34, 370);
        g.drawString("_____________", 60, 338);
        g.drawString("|", 57, 354);
        g.drawString("| " + (bSquared - fourAC) , 57, 370);
        g.drawString("__________________", 10, 380);
        g.drawString("" + twoA, 70, 410);
        g.drawString("( 4 )", 220, 380);

        g.drawString("Solve rest of equation . . . ", 10, 450);
               
        repaint();
    }

}

Click to watch the program


Thursday, September 5, 2013


Here are some free resource for Java.  Eclipse and NetBeans are the two common (free) compilers.  I prefer NetBeans over Eclipse.  You can also find free tutorials on YouTube, or just Google Java tutorials.  I'll also post some tutorials when I get the chance.

https://netbeans.org/


http://www.eclipse.org/


http://docs.oracle.com/javase/tutorial/java/


The compiler translates your Java code and converts it into machine language.  You need to have Java installed in your computer to compiler Java.  Most computer should come default with Java already installed, if its' not you can download it for free from Oracle.com.  The best part about Java is that everything you need is free.  Java was first written to write codes on appliances such as Microwave, refrigerator etc.  Java is a high level language.  

First Java project, creates a receipt for cashier

package project1;

import java.util.Scanner;

import java.util.Random;
import java.text.NumberFormat;

/**

* Tony Antony
* CSIT 210 - Project 1
* Overview: Gets franchise name and converts it to all uppercase, then displays a total of all  sales with a 9 digit confirmation code 
* Input: franchise name and number of each sizes sold
* Output: franchise name, number each size sold, prices of each size, total of each size, and final total of all sales
* Variables: CHILD_PRICE, MEDIUM_PRICE, LARGE_PRICE, EXTRA_LARGE_PRICE, childSales, mediumSales, largeSales, extraLargeSales, 
*            childTotal, mediumTotal, largeTotal, extraLargeTotal, finalTotal, confirmation_code, franchise
* Plan: 
* 1. Gets the franchise name as a String 
* 2. Gets the number of child, medium large and extra large sizes sold as integers
* 3. Converts the franchise name to all uppercase
* 4. calculates totals for each as doubles
* 5. displays the franchise name and receipt in currency format to screen
* @version 1.0 09.26.2013
*/

public class Project1 {


    public static void main(String[] args) {

        
        // declaring prices of each sizes as constants
        final double CHILD_PRICE, MEDIUM_PRICE, LARGE_PRICE, EXTRA_LARGE_PRICE;
        
        // declaring variables needed
        long confirmation_code;
        int childSales, mediumSales, largeSales, extraLargeSales;
        double childTotal, mediumTotal, largeTotal, extraLargeTotal, finalTotal;
        String franchise;
        
        // assigning values to each sizes
        CHILD_PRICE = 1.23;
        MEDIUM_PRICE = 2.34;
        LARGE_PRICE = 3.45;
        EXTRA_LARGE_PRICE = 4.56;  
       
        // starting new scanner and random objects
        Scanner scan = new Scanner(System.in);
        
        Random rand = new Random();
        
        // displays 9 dight random number between 100000000 and 900000000
        confirmation_code = rand.nextInt(900000000)+100000000;
        
        // currency number format option
        NumberFormat format = NumberFormat.getCurrencyInstance();     

        // getting user inputs for franchise name and how many each sizes sold

        System.out.print("Enter name of franchise: ");
        franchise = scan.nextLine();
        System.out.print("Enter child sizes sold: ");
        childSales = scan.nextInt();
        System.out.print("Enter medium sizes sold: ");
        mediumSales = scan.nextInt();
        System.out.print("Enter large sizes sold: ");
        largeSales = scan.nextInt();
        System.out.print("Enter extra large sizes sold: ");
        extraLargeSales = scan.nextInt();
        
        // adding extra blank line between user input and output
        System.out.println();
        
        // equation to find totals for each size
        childTotal = CHILD_PRICE * childSales;
        mediumTotal = MEDIUM_PRICE * mediumSales;
        largeTotal = LARGE_PRICE * largeSales;
        extraLargeTotal = EXTRA_LARGE_PRICE * extraLargeSales;        
        finalTotal = childTotal + mediumTotal + largeTotal + extraLargeTotal;
        
        // converts the franchise name to all uppercase and displays the bill in currency format
        System.out.println("\t\t\t    " + franchise.toUpperCase());
        System.out.println("\t\t\t//----//----~----\\\\----\\\\");
        System.out.println("\t\t# of Sales\tPrice\t\tTotal");
        System.out.println("Child\t\t" + childSales +"\t\t" + format.format(CHILD_PRICE) + "\t\t" + format.format(childTotal));
        System.out.println("Medium\t\t" + mediumSales +"\t\t" + format.format(MEDIUM_PRICE) + "\t\t" + format.format(mediumTotal));
        System.out.println("Large\t\t" + largeSales +"\t\t" + format.format(LARGE_PRICE) + "\t\t" + format.format(largeTotal));
        System.out.println("Extra Large\t" + extraLargeSales +"\t\t" + format.format(EXTRA_LARGE_PRICE) + "\t\t" + format.format(extraLargeTotal));
        System.out.println();
        System.out.println("\t\tTotal:\t\t\t\t" + format.format(finalTotal));
        System.out.println("\t\tConfirmation code: " + confirmation_code);        
        
    }
}







Monday, September 2, 2013

This program draws lines on screen using drawLine using random number generator


This is the main java file that gets the panel and puts it in the JFrame

package graph;

import javax.swing.JFrame;

public class Graph {

    public static void main(String[] args) {
        
        JFrame frame = new JFrame("Graph line between 2 points");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(new graphPanel());
        frame.setResizable(false);
        frame.pack();
        frame.setVisible(true);
        
    }
}

This is the graphPanel class

package graph;

import java.awt.*;
import javax.swing.*;
import java.util.Random;

public class graphPanel extends JPanel{

    private int p1X, p1Y, p2X, p2Y;  

    public graphPanel()
    {                     
        setPreferredSize(new Dimension(500,500));
        setBackground(Color.BLACK);
    }
           
    @Override
    public void paintComponent(Graphics g)
    {     
        super.paintComponent(g);
        
        g.setColor(Color.GREEN);
        
        g.drawLine(250, 0, 250, 500);
        g.drawLine(0, 250, 500, 250);
        
        g.drawString("Y", 275, 15);
        g.drawString("X", 485, 275);
        
        g.drawLine(240,25,260,25);
        g.drawLine(240,50,260,50);
        g.drawLine(240,75,260,75);
        g.drawLine(240,100,260,100);
        g.drawLine(240,125,260,125);
        g.drawLine(240,150,260,150);
        g.drawLine(240,175,260,175);
        g.drawLine(240,200,260,200);
        g.drawLine(240,225,260,225);
        
        g.drawLine(240,275,260,275);
        g.drawLine(240,300,260,300);
        g.drawLine(240,325,260,325);
        g.drawLine(240,350,260,350);
        g.drawLine(240,375,260,375);
        g.drawLine(240,400,260,400);
        g.drawLine(240,425,260,425);
        g.drawLine(240,450,260,450);
        g.drawLine(240,475,260,475);
        
        g.drawLine(25,240,25,260);
        g.drawLine(50,240,50,260);
        g.drawLine(70,240,70,260);
        g.drawLine(100,240,100,260);
        g.drawLine(125,240,125,260);
        g.drawLine(150,240,150,260);
        g.drawLine(175,240,175,260);
        g.drawLine(200,240,200,260);
        g.drawLine(225,240,225,260);
        
        g.drawLine(275,240,275,260);
        g.drawLine(300,240,300,260);
        g.drawLine(325,240,325,260);
        g.drawLine(350,240,350,260);
        g.drawLine(375,240,375,260);
        g.drawLine(400,240,400,260);
        g.drawLine(425,240,425,260);
        g.drawLine(450,240,450,260);
        g.drawLine(475,240,475,260);
        
        g.setColor(Color.BLUE);
        g.setFont(new Font("Helvetica", Font.PLAIN, 18));
        g.drawString("Point 1: ( " + p1X + ", " + p1Y + " )", 300, 400);
        g.drawString("Point 2: ( " + p2X + ", " + p2Y + " )", 300, 450);
                
        Random rand = new Random();
        p1X = rand.nextInt(500)+0;
        p1Y = rand.nextInt(500)+0;
        p2X = rand.nextInt(500)+0;
        p2Y = rand.nextInt(500)+0;
        
        g.setColor(Color.WHITE);
        g.drawLine(p1X, p1Y, p2X, p2Y);
     
        repaint();
    }           
}

Click below to see video of the program