Anda di halaman 1dari 30

Introduction to Semester 1 Showcase Project The control panel file for the project consists of objects of every individual

program, a GUI menu for accessing each of the programs, and an action listener to launch each program based on the action input of the user. The construction of the GUI is made by adding an item for each of the subcategories. The MATH menu consists of a factorial, grade conversion, exponential, and harmonic summation program. The ALGORITHM menu consists of a bubble sorting, integer swapping, string swapping, and binary conversion program. The GRAPHICS menu only contains the mouse drawing program. The factorial program converts the input number to a factorial and gives the equivalent to that factorial. The grade conversion program takes the input numerical grade and changes it to a letter grade based on the Woodstock letter grade system. The exponential program takes an input number and an input exponent and gives a result equal to the first input raised by an exponent equal to the second input. The harmonic series summation program takes an input and gives the summation of the harmonic series up to the input number in the series. The bubble sorting program takes inputs and bubble sorts them that is, the program arranges them from least to greatest in a fashion that is efficient so as to save CPU usage. The integer swapping program swaps two integers in text fields. The string swapping program swaps two strings in text fields. The binary conversion program takes an input binary number and converts it to an integer. The mouse drawing program is simple version of MS Paint. The user clicks and/or drags the mouse to create an image in the GUI. To view the .jar file click here: https://skydrive.live.com/redir.aspx? cid=5712cd16979c1a36&resid=5712CD16979C1A36!121&parid=5712CD16979C1A36! 119 To view the source code click here: https://skydrive.live.com/ redir.aspx?cid=5712cd16979c1a36&resid=5712CD16979C1A36! 120&parid=5712CD16979C1A36!119

MATH

Start Math

Harmonic Series

Grades

Factorials

Powers

Input for the series is taken

Input for the grade is taken

Input for the factorial is taken

Input for the number and exponent is taken

When enter is pressed, the program parses the input into a numerical value and passes it to the harmonic series method

When enter is pressed, the program parses the input into a numerical value and passes it to the grade conversion method

When enter is pressed, the program parses the input into a numerical value and passes it to the factorial method

When enter is pressed in either field, the program passes the numbers to the numberto-a-power method

The harmonic series method creates the mathematical series for one divided by n! where n is the input

In the grade conversion method, the input is changed into a letter grade by passing the number through an if else group that has beginnings and endings according to the grading system The method then sends the changed input back and parses it back

The factorial method uses a for loop to multiply a starting number by each number incremented up until the number is being multiplied by is equal to the input

The power method uses the exponent input as the limiting number in a for loop so that the first number is multiplied by itself as many times as the exponent demands.

The method then sends the changed input back and parses it back

The method then sends the changed input back and parses it back

The method then sends the result of the number and exponent back and parses it back

The summation is then displayed in a JLabel

The letter grade is then displayed in a JLabel

The factorial is then displayed in a JLabel

The number is then displayed in a Jlabel

The program will continue to run until it is terminated or closed

Algorithm

Parse input

JTextField In 1-3

Jbuttons 1-3

Count items, Construct a string[] element for every separator, stop when reach end of input string Int[] a Char[] a

Add features for more separators and trimming: s.charAt(i) == ',' || s.charAt(element) !=' ' ||s.charAt(element) !='-' And For(int I = 0; i<p.values.lenght; i++){ p.values[i].trim() ; }

String[] s JTextField Out 1-3 Jbutton Event handler Parse output

Construct output string in a loop

Add features for more types of input for sorting:

Integer = Change the data type of the storage object into integer. Double = Change the data type of the storage object into double. Character = Change the data type of the storage object into character.

BubbleSort 3 overloaded methods

GRAPHICS

Graphics Start

Mouse Drawing

The menu item is clicked, triggering an event that starts up the mouse drawing program

The size of the shape drawn is increased when the ENTER key is pressed

The size text field number is changed.

User Action

The mouse is clicked

A shape designated by the authors is drawn at that location in the designated color. The repaint method is called so that the spot is shown

The user seeks to close the program

The window is closed

The program is terminated

End Graphics

/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package GUI.showcase; import java.awt.event.*; import java.awt.*; import javax.swing.*; /** * * @author michaelrodda */ public class sem1Showcase extends JFrame{ private JMenuBar programMenu; private JMenu mathMenu, algorithmMenu, graphicMenu; private JMenuItem factorial; private JMenuItem harmonic; private JMenuItem powers; private JMenuItem grades; private JMenuItem binarytoDecimal; private JMenuItem bouncingBall; private JMenuItem bubbleSort; private JMenuItem parser; private JMenuItem swapString; private JMenuItem mouseDraw; private JMenuItem swapint; private static myWinHandler w; private Gui_MousePanel mPan; private Container c = getContentPane(); private BinaryToDecimal btd; private BouncingBall bb; private BubbleSort_GUI bs; private Gui_MouseDraw2 mdraw; private SwapString ss; private HarmonicNumber hNum; private powersOf pwrsof; private GradeInput grdinpt; private Factorial fact; private SwapInt si; public sem1Showcase(){ super("Learning programs in Semester 1"); menuHandler h = new menuHandler(); programMenu = new JMenuBar(); mathMenu = new JMenu("Math"); factorial = new JMenuItem("Factorial"); harmonic = new JMenuItem("Harmonic Number"); powers = new JMenuItem("Powers");

grades = new JMenuItem("Grades"); harmonic.addActionListener(h); powers.addActionListener(h); grades.addActionListener(h); factorial.addActionListener(h); mathMenu.add(grades); mathMenu.add(powers); mathMenu.add(factorial); mathMenu.add(harmonic); programMenu.add(mathMenu); binarytoDecimal = new JMenuItem("Convert Binary to Decimal"); bouncingBall = new JMenuItem("Bouncing Ball"); parser = new JMenuItem("Parser"); swapString = new JMenuItem("Swap a String"); mouseDraw = new JMenuItem("Mouse Drawing"); swapint = new JMenuItem("Swap an Integer"); bubbleSort = new JMenuItem("Bubble Sort"); algorithmMenu = new JMenu("Algorithms"); binarytoDecimal.addActionListener(h); swapint.addActionListener(h); bubbleSort.addActionListener(h); swapString.addActionListener(h); algorithmMenu.add(swapint); algorithmMenu.add(binarytoDecimal); algorithmMenu.add(swapString); algorithmMenu.add(bubbleSort); programMenu.add(algorithmMenu); graphicMenu = new JMenu("Graphics"); mouseDraw.addActionListener(h); bouncingBall.addActionListener(h); graphicMenu.add(mouseDraw); graphicMenu.add(bouncingBall); programMenu.add(graphicMenu); c.add(programMenu, BorderLayout.NORTH);

w= new myWinHandler(); setSize(200,100); setVisible(true); } private class menuHandler implements ActionListener{ public void actionPerformed(ActionEvent event){ if (event.getSource() == harmonic){

hNum = new HarmonicNumber(); } else if(event.getSource() == factorial){ fact = new Factorial(); } else if(event.getSource() == powers){ pwrsof = new powersOf(); } else if(event.getSource()== grades){ grdinpt = new GradeInput(); } else if(event.getSource() == binarytoDecimal){ btd = new BinaryToDecimal(); } else if (event.getSource() == swapString){ ss = new SwapString(); } else if (event.getSource() == mouseDraw){ mdraw = new Gui_MouseDraw2(); } else if (event.getSource() == bouncingBall){ bb = new BouncingBall(); } else if (event.getSource() == swapint){ si = new SwapInt(); } else if (event.getSource() == bubbleSort){ bs = new BubbleSort_GUI(); } } } public static void main(String[] args){ sem1Showcase sc1 = new sem1Showcase(); sc1.addWindowListener(w); } private class myWinHandler extends WindowAdapter{ public void windowClosing (WindowEvent event){ System.exit(0); } } }

/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package GUI.showcase; import javax.swing.*; import java.awt.*; import java.awt.event.*; /** * Factorial JFrame Program * @author michaelrodda */ public class Factorial extends JFrame{ // declared variables for GUI private JLabel text; private Container c; private JTextField input; //constuctor is made public Factorial(){ super ("Factorial of a Number"); text = new JLabel("Answer"); c = getContentPane(); TextFieldHandler h = new TextFieldHandler(); input = new JTextField("Enter Number Here."); input.addActionListener(h); c.add(text, BorderLayout.NORTH); c.add(input, BorderLayout.SOUTH);

setSize(200,100); setVisible(true); }

//The factorial for the number input is found public static double findFactorial(int input) { double result = 1; for (int i = 1; i <= input; i++) { result *= ((double)i); } return result; } // event handler is written here and handles the conversion // of the input to the factorial private class TextFieldHandler implements ActionListener{

public void actionPerformed(ActionEvent event){ String s = ""; int q = 0; double r = 0; if (event.getSource()==input){ s = input.getText(); q = Integer.parseInt(s); r = findFactorial(q); s = String.valueOf(r); text.setText(s); } } } //main method public static void main(String[] args){ Factorial app = new Factorial(); windowHandler wl = new windowHandler(); app.addWindowListener(wl); } // window event handler that closes the program if the window is exited private static class windowHandler extends WindowAdapter{ public void windowClosing(WindowEvent event){ System.exit(0); } } }

/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package GUI.showcase; import java.awt.event.*; import java.awt.*; import javax.swing.*; /** * * @author michaelrodda */ // class to apply a letter grade based on a percentage input public class GradeInput extends JFrame{ private JLabel grade; private JLabel gradeOut; private JTextField input; private Container c; private ActionHandler action; private static myWinHandler w; // the constructor instantiates variables and adds them to the visible frame public GradeInput(){ super("Grade Converter"); c = getContentPane(); action = new ActionHandler(); w = new myWinHandler(); grade = new JLabel("Grade:"); gradeOut = new JLabel(""); input = new JTextField("Enter the Numerical Grade Here."); input.addActionListener(action); c.add(grade, BorderLayout.WEST); c.add(gradeOut, BorderLayout.EAST); c.add(input, BorderLayout.SOUTH); setSize(200,100); setVisible(true); } // changes the given input into a letter grade public String findGrade(int a){ String gradereturn = ""; if (a>=87){ gradereturn = "A+"; } else if (a>=82){

gradereturn = "A"; } else if (a>=80){ gradereturn = "A-"; } else if (a>=77){ gradereturn = "B+"; } else if (a>=73){ gradereturn = "B"; } else if (a>=70){ gradereturn = "B-"; } else if (a>=67){ gradereturn = "C+"; } else if (a>=63){ gradereturn = "C"; } else if (a>=60){ gradereturn = "C-"; } else if (a>=57){ gradereturn = "D+"; } else if (a>=53){ gradereturn = "D"; } else if (a>=50){ gradereturn = "D-"; } else{ gradereturn = "F"; } return gradereturn; } //formats the input into an int then processes it using the findGrade method private class ActionHandler implements ActionListener{ public void actionPerformed(ActionEvent event){ int q = 0; String s = ""; String r = ""; r = input.getText(); q = Integer.parseInt(r); s = findGrade(q); gradeOut.setText(s); } }

public static void main(String[] args){ GradeInput app = new GradeInput(); app.addWindowListener(w); } private static class myWinHandler extends WindowAdapter{ public void windowClosing (WindowEvent event){ System.exit(0); } } }

/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package GUI.showcase; import javax.swing.*; import java.awt.*; import java.awt.event.*; /** * * @author michaelrodda */ // raises an input number to another input exponent public class powersOf extends JFrame{ private JLabel answer; private JTextField input; private JTextField input1; private JLabel answerLabel; private Container c; private ActionHandler action; private static myWinHandler w; private FlowLayout fl = new FlowLayout(); public powersOf(){ super("Find the Power of a Number"); c = getContentPane(); action = new ActionHandler(); w = new myWinHandler(); answerLabel = new JLabel("Answer:"); answer = new JLabel(""); input = new JTextField("Enter the number to be raised to an exponent here.",26); input1 = new JTextField("Enter the exponent here.", 26); input.addActionListener(action); input1.addActionListener(action); c.setLayout(fl); c.add(answerLabel, fl); c.add(answer, fl); c.add(input, fl); c.add(input1, fl); setSize(350,150); setVisible(true); } // finds the answer based on the number and the exponent input

public String findPower(int a, int b){ int returnInfo = 1; String returnActual; for(int i=1;i<=b;i++){ returnInfo *= a; } returnActual = String.valueOf(returnInfo); return returnActual; } //formats the inputs into ints then sends them to findPowers then formats back private class ActionHandler implements ActionListener{ public void actionPerformed(ActionEvent event){ if (event.getSource() == input || event.getSource() == input1){ int q = 0; int y = 0; String x = ""; String s = ""; String r = ""; x = input1.getText(); r = input.getText(); y = Integer.parseInt(x); q = Integer.parseInt(r); s = findPower(q,y); answer.setText(s); } } }

public static void main(String[] args){ powersOf app = new powersOf(); app.addWindowListener(w); } private static class myWinHandler extends WindowAdapter{ public void windowClosing (WindowEvent event){ System.exit(0); } } }

/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package GUI.showcase; import javax.swing.*; import java.awt.*; import java.awt.event.*; /** * * @author michaelrodda */ // gives the summation of a harmonic series based on input public class HarmonicNumber extends JFrame{ private JLabel text; private Container c; private JTextField input; //constructor instantiates variables and adds objects to the frame public HarmonicNumber(){ super ("Harmonic Number"); text = new JLabel("Answer"); //text.setEditable(false); c = getContentPane(); TextFieldHandler h = new TextFieldHandler(); input = new JTextField("Enter Number Here."); input.addActionListener(h); c.add(text, BorderLayout.NORTH); c.add(input, BorderLayout.SOUTH);

setSize(200,100); setVisible(true); } // finds the harmonic series summation for the input number public static double findHarmonic(int input) { double result = 0; for (int i = 1; i <= input; i++) { result += (1/(double) i); } return result; } // formats the inputs into ints then back into strings to be output after findHarmonic processes it

private class TextFieldHandler implements ActionListener{ public void actionPerformed(ActionEvent event){ String s = ""; int q = 0; double r = 0; if (event.getSource()==input){ s = input.getText(); q = Integer.parseInt(s); r = findHarmonic(q); s = String.valueOf(r); text.setText(s); } } } public static void main(String[] args){ HarmonicNumber app = new HarmonicNumber(); windowHandler wl = new windowHandler(); app.addWindowListener(wl); } private static class windowHandler extends WindowAdapter{ public void windowClosing(WindowEvent event){ System.exit(0); } } }

/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package GUI.showcase; import java.awt.*; import java.awt.event.*; import javax.swing.*; /** * * @author jonathanwu */ // bubble sorting class that uses the parser inner methods the sort several // types of data types within arrays public class BubbleSort { // bubble sorts an array of integers by comparing numerical value or worth public void bubbleSort(int[] a){ int n=a.length; //temp is used to hold the place of a variable being switched int temp=a[0]; // the for loop here is used to go through the loop several times // and switch variables based on value; each time, the loop goes // through the array exempting one more variable from the end // to save CPU usage because the variables at the end are already // proven to be higher. for (int pass=1; pass<n; pass++){ for (int i=0; i<n-pass; i++){ if (a[i]>a[i+1]){ temp= a[i]; a[i]=a[i+1]; a[i+1] = temp; } } } } // bubble sorts an array of doubles by comparing numerical value or worth public void bubbleSort(double[] a){ int n=a.length; double temp=a[0]; for (int pass=1; pass<n; pass++){ for (int i=0; i<n-pass; i++){ if (a[i]>a[i+1]){ temp= a[i]; a[i]=a[i+1]; a[i+1] = temp; } } }

} // bubble sorts an array of characters based on the order of the alphaber, //where a<b public void bubbleSort(char[] a){ int n=a.length; char temp; for (int pass=1; pass<n; pass++){ for (int i=0; i<n-pass; i++){ if (a[i] >a[i+1]){ temp= a[i]; a[i]=a[i+1]; a[i+1] = temp; } } } } // bubble sorts an array of strings based on the first letter's order in the //alphabet where a<b public void bubbleSort(String[] ss){ String temp=ss[0]; for (int pass=1; pass<ss.length; pass++){ for (int i=0; i<ss.length-pass; i++){ if ((ss[i].compareTo(ss[i+1]))>0){ temp= ss[i]; ss[i]=ss[i+1]; ss[i+1] = temp; } } } } } // the class used to create a visible frame for the bubble sorting GUI class BubbleSort_GUI extends JFrame { JButton Integer, Character, string, Double; JTextField Input; JLabel Output; BubbleSort b= new BubbleSort(); Parser_e p= new Parser_e(); String out="",s=""; // the constructor here instantiates variables and adds them to the frame BubbleSort_GUI() { super("Bubble Sort"); setLayout(new FlowLayout()); Input= new JTextField("Input List here"); Output= new JLabel("Sorted List will be displayed here");

Integer= new JButton("Sort Integer Values"); Character= new JButton("Sort Character Values"); string= new JButton("Sort String Values"); Double = new JButton("Sort Double Values");

add(Input); add(Integer); add(Character); add(string); add(Double); add(Output); Action a= new Action(); Integer.addActionListener(a); Character.addActionListener(a); string.addActionListener(a); Double.addActionListener(a); setSize(500,500); setVisible(true); } // the action handling class decides which method is to be used // based on the button pressed and calls appropriate // parser and bubble sorts it accordingly. class Action implements ActionListener { public void actionPerformed(ActionEvent e) { if(e.getSource()== Integer) { s=Input.getText(); p.parseIn(s); p.convertInt(p.values); b.bubbleSort(p.int_values); out= p.convertInt_String(p.int_values); Output.setText(out); } else if(e.getSource()== Character) { s=Input.getText(); p.parseIn(s); p.convertChar(p.values); b.bubbleSort(p.char_values); out= p.convertChar_String(p.char_values); Output.setText(out); } else if(e.getSource()==string) {

s=Input.getText(); p.parseIn(s); b.bubbleSort(p.values); out= p.convertStringArray_String(p.values); Output.setText(out); } else if(e.getSource()== Double) { s=Input.getText(); p.parseIn(s); p.convertDouble(p.values); b.bubbleSort(p.Double_values); out= p.convertDouble_String(p.Double_values); Output.setText(out); } } } }

/* * To change this template, choose Tools | Templates * and open the template in the editor. */ /* * To change this template, choose Tools | Templates * and open the template in the editor. */ package GUI.showcase; /** * * @author jonathanwu */ public class Parser_e { public int count=1; public String[] values; public int[] int_values; public double[] Double_values; public char[] char_values; public void parseIn(String s){ int element=0; for (int i=0; i<s.length(); i++)// counts number of commas { if (s.charAt(i) == ',') count++; } values = new String[count];// makes a new string array for(int i=0; i<count; i++){ String number_string = new String(); try{ while (s.charAt(element) !=','){ number_string +=s.charAt(element); if(element>=s.length()-1) break; element++; }} catch(StringIndexOutOfBoundsException e){} values[i]=number_string.toString(); element++; } } public void convertInt(String[] ss){ int_values = new int[count];

for(int i=0; i<count; i++){ int_values[i] = Integer.parseInt(values[i]); } } public void convertDouble(String[] ss){ Double_values = new double[count]; for(int i=0; i<count; i++){ Double_values[i] = Double.parseDouble(values[i]); } } public void convertChar(String[] ss){ char_values = new char[count]; for(int i=0; i<count; i++){ char_values[i] = values[i].charAt(0); } } public String convertInt_String(int[] int_vals){ String output=""; for(int i=0; i<count; i++){ output+=int_vals[i]+","; } return output; } public String convertDouble_String(double[] Double_vals){ String output=""; for(int i=0; i<count; i++){ output+=Double_vals[i]+","; } return output; } public String convertChar_String(char[] char_vals){ String output=""; for(int i=0; i<count; i++){ output+=char_vals[i]+","; } return output; } public String convertStringArray_String(String[] str_vals){ String output=""; for(int i=0; i<count; i++){ output+=str_vals[i]+","; } return output; } }// end class

/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package GUI.showcase; import java.awt.* ; import java.awt.event.* ; import javax.swing.* ; /** * * @author pratanabanthoon */ public class SwapInt extends JFrame{ public static JTextField textField1, textField2 ; public SwapInt(){ super("swapping integer"); //create container and add both textfields for input and output Container container = getContentPane(); container.setLayout(new FlowLayout()); //textfield for input textField1 = new JTextField(10); container.add(textField1); //textfield for output textField2 = new JTextField(10); container.add(textField2); //call on event handler TextFieldHandler handler = new TextFieldHandler(); textField1.addActionListener(handler); textField2.addActionListener(handler); setSize(500, 100); setVisible(true); } private void swapping(int i, int j ){ //swapping integer i and j int t=i; i=j; j=t; //instantiate i and j to a and b respectively textField1.setText(i+"") ; textField2.setText(j+"") ;

private class TextFieldHandler implements ActionListener{ public void actionPerformed(ActionEvent event){ //handle to swapping of integers swapping(Integer.parseInt(textField1.getText()), Integer.parseInt(textField2.getText())); } } }

/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package GUI.showcase; import java.awt.* ; import java.awt.event.* ; import javax.swing.* ; /** * * @author pratanabanthoon */ public class SwapString extends JFrame{ public static JTextField textField1, textField2 ; public static String a, b ; public SwapString(){ super("swapping"); //create container and add both textfields for input and output Container container = getContentPane(); container.setLayout(new FlowLayout()); //textfield for input textField1 = new JTextField(10); container.add(textField1); //textfield for output textField2 = new JTextField(10); container.add(textField2); //call on event handler TextFieldHandler handler = new TextFieldHandler(); textField1.addActionListener(handler); textField2.addActionListener(handler); setSize(500, 100); setVisible(true); } private void swapping(String i, String j ){ //swapping Strings i and j String t=i; i=j; j=t; //instantiate i and j to a and b respectively Character a = i.charAt(0) ; Character b = j.charAt(0) ; textField1.setText(a+""); textField2.setText(b+"");

} private class TextFieldHandler implements ActionListener{ public void actionPerformed(ActionEvent event){ //handle to swapping of Strings swapping(textField1.getText(), textField2.getText()); } } }

package GUI.showcase; import java.awt.*; import java.awt.event.*; import javax.swing.*; /** * * @author jonathanwu */ // converts a binary input to a decimal public class BinaryToDecimal extends JFrame{ JTextField txt1, txt2; JButton btn1, btn2; BinaryToDecimal() { super("Binary To Decimal"); setLayout(new FlowLayout()); //----------Input and Output Text Fields--------------------// txt1= new JTextField("Enter Number Here"); txt2= new JTextField("The Result Value displayed here "); txt2.setEditable(false); add(txt1); add(txt2); //----------------------------------------------------------// //----------Decimal To Binary Components--------------------// btn1= new JButton("Binary --> Decimal"); btn2= new JButton("Decimal --> Binary"); add(btn1); add(btn2); //------------------------------------------------------// //-----------Adding Action Listeners-------------// Action a= new Action(); btn1.addActionListener(a); btn2.addActionListener(a); //-----------------------------------------------// setSize(500,500); setVisible(true); } public String BinToInt(int i) { String result; result = Integer.toBinaryString(i);// method in the Integer class that converts Integer values to Binary return result; } public String IntToBin(String str)

{ long num = Long.parseLong(str); long rem; while(num > 0) { rem = num % 10; num = num / 10; if(rem != 0 && rem != 1)// checks to see if input is binary { JOptionPane.showMessageDialog(null,"This is not a binary number. /n Please Try again"); } } int i= Integer.parseInt(str,2); String s= "" +i; return s; } class Action implements ActionListener { public void actionPerformed(ActionEvent e) { if(e.getSource()==btn1) { txt2.setText(BinToInt(Integer.parseInt(txt1.getText()))); } else if(e.getSource()==btn2) { txt2.setText(IntToBin(txt1.getText())); } } } }

/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package GUI.showcase; import java.awt.*; import java.awt.event.*; import javax.swing.*; /** * * @author RaveenVasudeva */ public class Gui_MousePanel extends JPanel{ private int xval=-10,yval=-10; public Gui_MousePanel(){ setBackground(Color.orange); MousePainter m = new MousePainter(); addMouseMotionListener(m); } public void paintComponent(Graphics g){ //super.paintComponents(g); g.setColor(Color.red); g.fillOval(xval, yval, 5, 5); } private class MousePainter extends MouseMotionAdapter{ public void mouseDragged(MouseEvent m_event){ xval=m_event.getX(); yval=m_event.getY(); repaint(); } }

/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package GUI.showcase; import java.awt.*; import java.awt.event.*; import javax.swing.*; /** * * @author RaveenVasudeva */ //constructs a visual frame using the methods and variables from Gui_MousePanel public class Gui_MouseDraw2 extends JFrame{ private JLabel lbl_Instructions; private Gui_MousePanel MousePanel; // the constructor creates an object of the mousepanel and a label to describe it public Gui_MouseDraw2(){ super ("Drawing with a mouse"); Container c = getContentPane(); c.setLayout(new BorderLayout()); lbl_Instructions=new JLabel("Drag mouse to draw"); lbl_Instructions.setFont(new Font("Serif", Font.BOLD, 20)); c.add(lbl_Instructions,BorderLayout.SOUTH); MousePanel = new Gui_MousePanel(); c.add(MousePanel, BorderLayout.CENTER); setSize(300,300); setVisible(true); } public static void main(String[] args){ Gui_MouseDraw2 mouse_app1 = new Gui_MouseDraw2(); // mouse_app1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); MyWinHandler mWinH1 = new MyWinHandler(); mouse_app1.addWindowListener(mWinH1); } private static class MyWinHandler extends WindowAdapter{ public void windowClosing (WindowEvent w_event){ System.exit(0); } } }

Anda mungkin juga menyukai