Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
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)); RECAP 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 // 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; } Running the application 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 Drawing in a frame Importing the libraries // StarTest.java - display a starburst import java.awt.*; import javax.swing.*; Creating a class called StartTest 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 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(); Creating an object named star based on the definition of Star (our class we will cover later) Star star = new Star(); pane.add(star); frame.pack(); frame.show(); } } Creating a container named pane, and associating it with our frame called frame. 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 // Star.java - draws a star import javax.swing.*; import java.awt.*; The Star Class 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; } Importing the libraries Creating a derived class based on the class called JComponents from the library Creating a public class named paint using the Graphics features // Star.java - draws a star import javax.swing.*; import java.awt.*; Drawing in a frame 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) { 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 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; } // Star.java - draws a star import javax.swing.*; import java.awt.*; Drawing in a frame 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) { A loop for drawing 16 lines 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 // Star.java - draws a star import javax.swing.*; import java.awt.*; Drawing in a frame 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) { Drawing the line. This requires 4 points and they need to be integers, so we cast these variables as integers (they are actually doubles) 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; } // 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) { 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. 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); When the method pack is called for } } 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; } 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 Drawing in a frame 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