160 likes | 176 Views
Learn how to create a Java calculator using JLabels, JTextFields, and JButtons within a JFrame. Discover how to draw geometric shapes in a frame using Java graphics.
E N D
Lesson 31: Drawing in a frame – Java GUI
The Calculator RECAP • 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
// 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
RECAP // 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; }
Drawing in a frame • We will use the graphics and the Jframe together with a simple loop that creates references for a line. • The line needs a starting x,y coordinate and an ending ending x,y coordiante • We will use the class Star for definition and the class StartTest for executing our star
Drawing in a frame Importing the libraries // 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 a class called StartTest Creating a main section Creating a frame named frame and labeled StarTest
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 a container named pane, and associating it with our frame called frame. Creating an object named star based on the definition of Star (our class we will cover later) Adding our star to the container for our frame
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(); } } Packing the frame to smallest size Showing the frame
The Star Class Importing the libraries Creating a derived class based on the class called JComponents from the library // 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<Math.PI; angle = angle + Math.PI / 16) { x1=Math.cos(angle) * RADIUS + RADIUS; y1=Math.sin(angle) * RADIUS + RADIUS; x2=Math.cos(angle + Math.PI) * RADIUS + RADIUS; y2=Math.sin(angle + Math.PI) * RADIUS + RADIUS; g.drawLine((int)x1, (int)y1,(int)x2, (int)y2); } } public Dimension getMinimumSize(){ return new Dimension(2 * RADIUS, 2 * RADIUS); } public Dimension getPreferredSize(){ return new Dimension(2 * RADIUS, 2 * RADIUS); } private static final int RADIUS = 100; } Creating a public class named paint using the Graphics features
Drawing in a frame Creating four variables: x1 = starting coordinate for the line y1 = starting coordinate for the line x2 = ending coordinate for the line y2 = ending coordinate for the line // 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<Math.PI; angle = angle + Math.PI / 16) { x1=Math.cos(angle) * RADIUS + RADIUS; y1=Math.sin(angle) * RADIUS + RADIUS; x2=Math.cos(angle + Math.PI) * RADIUS + RADIUS; y2=Math.sin(angle + Math.PI) * RADIUS + RADIUS; g.drawLine((int)x1, (int)y1,(int)x2, (int)y2); } } public Dimension getMinimumSize(){ return new Dimension(2 * RADIUS, 2 * RADIUS); } public Dimension getPreferredSize(){ return new Dimension(2 * RADIUS, 2 * RADIUS); } private static final int RADIUS = 100; }
Drawing in a frame A loop for drawing 16 lines // 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<Math.PI; angle = angle + Math.PI / 16) { x1=Math.cos(angle) * RADIUS + RADIUS; y1=Math.sin(angle) * RADIUS + RADIUS; x2=Math.cos(angle + Math.PI) * RADIUS + RADIUS; y2=Math.sin(angle + Math.PI) * RADIUS + RADIUS; g.drawLine((int)x1, (int)y1,(int)x2, (int)y2); } } public Dimension getMinimumSize(){ return new Dimension(2 * RADIUS, 2 * RADIUS); } public Dimension getPreferredSize(){ return new Dimension(2 * RADIUS, 2 * RADIUS); } private static final int RADIUS = 100; } Fancy math using the Math class for finding the endpoints and start points of the line to be drawn
Drawing in a frame Drawing the line. This requires 4 points and they need to be integers, so we cast these variables as integers (they are actually doubles) // 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<Math.PI; angle = angle + Math.PI / 16) { x1=Math.cos(angle) * RADIUS + RADIUS; y1=Math.sin(angle) * RADIUS + RADIUS; x2=Math.cos(angle + Math.PI) * RADIUS + RADIUS; y2=Math.sin(angle + Math.PI) * RADIUS + RADIUS; g.drawLine((int)x1, (int)y1,(int)x2, (int)y2); } } public Dimension getMinimumSize(){ return new Dimension(2 * RADIUS, 2 * RADIUS); } public Dimension getPreferredSize(){ return new Dimension(2 * RADIUS, 2 * RADIUS); } private static final int RADIUS = 100; }
The class JComponent includes definitions of these methods which by default returns the value of 0. Here we redefine those methods so that the layout manager provides an area large enough for the whole star to show. The class Dimension is a standard java class that simple encapsulates two integers treated as height and width. // 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<Math.PI; angle = angle + Math.PI / 16) { x1=Math.cos(angle) * RADIUS + RADIUS; y1=Math.sin(angle) * RADIUS + RADIUS; x2=Math.cos(angle + Math.PI) * RADIUS + RADIUS; y2=Math.sin(angle + Math.PI) * RADIUS + RADIUS; g.drawLine((int)x1, (int)y1,(int)x2, (int)y2); } } public Dimension getMinimumSize(){ return new Dimension(2 * RADIUS, 2 * RADIUS); } public Dimension getPreferredSize(){ return new Dimension(2 * RADIUS, 2 * RADIUS); } private static final int RADIUS = 100; } When the method pack is called for a JFrame the layoutmanager of the content pane will call one of the methods getMinimumSize() or getPreferredSize(). The call depends on the Layout manager and other factors. The value returned is used to determine the location and size.
Drawing in a frame • We used the graphics and the Jframe together with a simple loop that creates references for a line. • The line needs a starting x,y coordinate and an ending ending x,y coordiante • We used the class Star for definition and the class StartTest for executing our star • We manipulated the container manager