Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lesson 31: Drawing in a frame – Java GUI. The Calculator In this example we will start using –JLabels –the GridLayout –Typecasting to double from a text.

Similar presentations


Presentation on theme: "Lesson 31: Drawing in a frame – Java GUI. The Calculator In this example we will start using –JLabels –the GridLayout –Typecasting to double from a text."— Presentation transcript:

1 Lesson 31: Drawing in a frame – Java GUI

2 The Calculator In this example we will start using –JLabels –the GridLayout –Typecasting to double from a text input With use of JButtons, JFrame, Containers, Listeners and JTextFields we will create a calculator java application using the GUI components in the swing library RECAP

3 // MiniCalc.java - demo gridlayout import java.awt.*; import javax.swing.*; class MiniCalc{ public static void main (String[] args){ JFrame frame = new JFrame("MiniCalc"); Container pane = frame.getContentPane(); //Creating the major components JTextField firstNumber= new JTextField(20); JTextField secondNumber= new JTextField(20); JTextField result= new JTextField(20); JButton addButton = new JButton("Add"); JButton subButton = new JButton("Subtract"); pane.setLayout(new GridLayout(4,2)); pane.add(new JLabel("Enter a number")); pane.add(firstNumber); pane.add(new JLabel("Enter a number")); pane.add(secondNumber); pane.add(new JLabel("Result")); pane.add(result); pane.add(addButton); pane.add(subButton); DoMath listener = new DoMath(firstNumber, secondNumber, result); subButton.addActionListener(listener); addButton.addActionListener(listener); frame.pack(); frame.show(); } RECAP

4 // DoMath.java import javax.swing.*; import java.awt.event.*; class DoMath implements ActionListener{ DoMath(JTextField first, JTextField second, JTextField result){ inputOne = first; inputTwo = second; output = result; } public void actionPerformed(ActionEvent e){ double first, second; first = Double.parseDouble(inputOne.getText().trim()); second = Double.parseDouble(inputTwo.getText().trim()); if (e.getActionCommand().equals("Add")) output.setText(String.valueOf(first+second)); else output.setText(String.valueOf(first-second)); } private JTextField inputOne, inputTwo, output; } RECAP

5 Running the application

6 Drawing in a frame 1.We will use the graphics and the Jframe together with a simple loop that creates references for a line. 2.The line needs a starting x,y coordinate and an ending ending x,y coordiante 3.We will use the class Star for definition and the class StartTest for executing our star

7 Drawing in a frame // StarTest.java - display a starburst import java.awt.*; import javax.swing.*; class StarTest{ public static void main (String[] args){ JFrame frame = new JFrame("StartTest"); Container pane = frame.getContentPane(); Star star = new Star(); pane.add(star); frame.pack(); frame.show(); } Importing the libraries Creating a class called StartTest Creating a main section Creating a frame named frame and labeled StarTest

8 Drawing in a frame // StarTest.java - display a starburst import java.awt.*; import javax.swing.*; class StarTest{ public static void main (String[] args){ JFrame frame = new JFrame("StartTest"); Container pane = frame.getContentPane(); Star star = new Star(); pane.add(star); frame.pack(); frame.show(); } Creating an object named star based on the definition of Star (our class we will cover later) Creating a container named pane, and associating it with our frame called frame. Adding our star to the container for our frame

9 Drawing in a frame // StarTest.java - display a starburst import java.awt.*; import javax.swing.*; class StarTest{ public static void main (String[] args){ JFrame frame = new JFrame("StartTest"); Container pane = frame.getContentPane(); Star star = new Star(); pane.add(star); frame.pack(); frame.show(); } Showing the frame Packing the frame to smallest size

10 The Star Class // Star.java - draws a star import javax.swing.*; import java.awt.*; class Star extends JComponent{ public void paint (Graphics g){ double x1, x2, y1, y2; for (double angle=0; angle

11 Drawing in a frame // Star.java - draws a star import javax.swing.*; import java.awt.*; class Star extends JComponent{ public void paint (Graphics g){ double x1, x2, y1, y2; for (double angle=0; angle

12 Drawing in a frame // Star.java - draws a star import javax.swing.*; import java.awt.*; class Star extends JComponent{ public void paint (Graphics g){ double x1, x2, y1, y2; for (double angle=0; angle

13 Drawing in a frame // Star.java - draws a star import javax.swing.*; import java.awt.*; class Star extends JComponent{ public void paint (Graphics g){ double x1, x2, y1, y2; for (double angle=0; angle

14 // Star.java - draws a star import javax.swing.*; import java.awt.*; class Star extends JComponent{ public void paint (Graphics g){ double x1, x2, y1, y2; for (double angle=0; angle

15 Drawing in a frame

16 1.We used the graphics and the Jframe together with a simple loop that creates references for a line. 2.The line needs a starting x,y coordinate and an ending ending x,y coordiante 3.We used the class Star for definition and the class StartTest for executing our star 4.We manipulated the container manager


Download ppt "Lesson 31: Drawing in a frame – Java GUI. The Calculator In this example we will start using –JLabels –the GridLayout –Typecasting to double from a text."

Similar presentations


Ads by Google