Presentation is loading. Please wait.

Presentation is loading. Please wait.

Java Applet Presented by Fitsum Okubu. Introduction Introduction Graphics Graphics Methods and Variables Methods and Variables Events Events Decision.

Similar presentations


Presentation on theme: "Java Applet Presented by Fitsum Okubu. Introduction Introduction Graphics Graphics Methods and Variables Methods and Variables Events Events Decision."— Presentation transcript:

1 Java Applet Presented by Fitsum Okubu

2 Introduction Introduction Graphics Graphics Methods and Variables Methods and Variables Events Events Decision – if clause Decision – if clause Creating and using of buttons and textfields. Creating and using of buttons and textfields. Repetition – while, for and do Repetition – while, for and do Objects and Classes Objects and Classes

3 Java applet and application –Java programs are of two distinct types: applications applications applets applets Both types are programs. Both types are programs. The difference is that an application is a completely free- standing program. That means it is not part of any other program and it doesn’t need any other program to launch it. In contrast an applet is invoked as part of a Web page and therefore needs either a Web browser or an applet viewer to invoke it. The difference is that an application is a completely free- standing program. That means it is not part of any other program and it doesn’t need any other program to launch it. In contrast an applet is invoked as part of a Web page and therefore needs either a Web browser or an applet viewer to invoke it.

4 Graphics Graphics Applet can be used to display animated images Applet can be used to display animated images Java graphics are based on pixels. Java graphics are based on pixels. Each pixel is identified by a pair of numbers (its coordinates), starting from zero. Each pixel is identified by a pair of numbers (its coordinates), starting from zero. –The horizontal position is, often referred to as x in mathematics (and in java documentation) – this value increases from left to right. –The vertical postion, often referred to as y – increases downwards. Java provides us with facilities for drawing: drawRect drawRect –the horizontal value of the top left corner. –the vertical value of the top left corner. –the with of the rectangle. –the height of the rectangle. drawOval –Imagine the oval squeezed inside a rectangle.  the horizontal value of the top left corner of the rectangle.  the vertical value of the top left corner of the rectangle.  the width of the rectangle.  the height of the rectangle.

5 Example1 Example1 Example1 import java.awt.*; import java.applet.Applet; public class Example1 extends Applet{ public void paint (Graphics g){ g.drawRect(30,30,80,40); g.drawOval(120,30, 50,50); g.setColor(Color.blue); g.fillRect(30,100, 80,40); g.setColor(Color.orange);g.fillOval(120,100,50,50);g.drawLine(30,160,130,170);}} Example1 Example1 <applet code="Example1.class" width= 300 height=200> width= 300 height=200>

6 Methods and Parameters Methods and Parameters Large programs can be complex, with the result that they can be difficult to understand and debug. The most significant technique for reducing complexity is to split a into isolated sections called methods. Large programs can be complex, with the result that they can be difficult to understand and debug. The most significant technique for reducing complexity is to split a into isolated sections called methods.Example2 import java.awt.*; import java.applet.Applet; public class Example2 extends Applet{ public void paint(Graphics g){ drawHouse(g, 50,50,70,30); drawHouse(g, 100,50,60,20); } private void drawTriangle(Graphics g, int bottomX, int bottomY, int base, int height){ int base, int height){ g.drawLine(bottomX, bottomY, bottomX+base, bottomY); g.drawLine(bottomX+base, bottomY, bottomX + base/2, bottomY-height); g.drawLine(bottomX+base/2, bottomY-height,bottomX, bottomY); } private void drawHouse(Graphics g, int bottomX, int bottomY, int width, int height){ int width, int height){g.setColor(Color.cyan); g.drawRect(bottomX, bottomY-height, width, height); g.drawRect(bottomX, bottomY-height, width, height);g.setColor(Color.orange); drawTriangle(g, bottomX, bottomY-height, width, height/2); }} HTML code to invoke the program Example2 Example2 <applet code="Example2.class" width= 300 height=200> width= 300 height=200>

7 Events Events In programming an event is (in most cases) an action taken by the user. Scrollbar events - AdjustmentListener Scrollbar events - AdjustmentListener First we need to state that we are going to implement a listener for scrollbar events, which are classified in AdjustmentListener. We then register our program as a listener by using the addAdjustmentListener method, which is in effect causes our simple program, our adjustmentValueChanged method simply fetches the new value from the scrollbar, and arranges for it to be painted on the screen. First we need to state that we are going to implement a listener for scrollbar events, which are classified in AdjustmentListener. We then register our program as a listener by using the addAdjustmentListener method, which is in effect causes our simple program, our adjustmentValueChanged method simply fetches the new value from the scrollbar, and arranges for it to be painted on the screen.

8 Example3(Creating scrollbars) Example3(Creating scrollbars) import java.awt.*; import java.applet.Applet;import java.awt.event.*; public class Colors extends Applet implements AdjustmentListener { private Scrollbar red; private Scrollbar green; private Scrollbar red; private Scrollbar green; private Scrollbar blue; private Scrollbar blue; private int redValue = 0; private int redValue = 0; private int greenValue = 0; private int greenValue = 0; private int blueValue = 0; private int blueValue = 0; public void init() { public void init() { Label l1, l2, l3; Label l1, l2, l3; l1 = new Label("Red:"); l1 = new Label("Red:"); l2 = new Label("Green:"); l2 = new Label("Green:"); l3 = new Label("Blue:"); l3 = new Label("Blue:"); red = new Scrollbar(Scrollbar.VERTICAL, 0, 0, 0, 256); red = new Scrollbar(Scrollbar.VERTICAL, 0, 0, 0, 256); green = new Scrollbar(Scrollbar.VERTICAL, 0, 0, 0, 256); green = new Scrollbar(Scrollbar.VERTICAL, 0, 0, 0, 256); blue = new Scrollbar(Scrollbar.VERTICAL, 0, 0, 0, 256); blue = new Scrollbar(Scrollbar.VERTICAL, 0, 0, 0, 256); add(l1); add(l1); add(red); add(red); add(l2); add(l2); add(green); add(green); add(l3); add(l3); add(blue); add(blue); red.addAdjustmentListener(this); red.addAdjustmentListener(this); green.addAdjustmentListener(this); green.addAdjustmentListener(this); blue.addAdjustmentListener(this); blue.addAdjustmentListener(this); } public void adjustmentValueChanged(AdjustmentEvent e) { public void adjustmentValueChanged(AdjustmentEvent e) { Color c1; Color c1; redValue = red.getValue(); redValue = red.getValue(); greenValue = green.getValue(); greenValue = green.getValue(); blueValue = blue.getValue(); blueValue = blue.getValue(); showStatus("Red is: " + redValue + "; Green is: " + greenValue + "; Blue is: " + blueValue); showStatus("Red is: " + redValue + "; Green is: " + greenValue + "; Blue is: " + blueValue); c1 = new Color(redValue, greenValue, blueValue); c1 = new Color(redValue, greenValue, blueValue); setBackground(c1); setBackground(c1);}} //html code Example3 //html code Example3

9 Decisions – if (Example4) Decisions – if (Example4) import java.awt.*; import java.applet.Applet; import java.awt.event.*; public class Example4 extends Applet implements AdjustmentListener{ private Scrollbar tom, jerry; private int tomValue= 0, jerryValue = 0; public void init(){ Label toms = new Label("Tom"); add(toms); tom = new Scrollbar(Scrollbar.HORIZONTAL, 0,1,0,100); add(tom);tom.addAdjustmentListener(this); Label jerrys = new Label("Jerry:"); add(jerrys); jerry = new Scrollbar(Scrollbar.HORIZONTAL, 0,1,0,100); add(jerry);jerry.addAdjustmentListener(this);} public void paint(Graphics g){ g.drawString("Tom", 5, 70); g.fillRect(40,60,tomValue,10); g.drawString("Jerry", 5, 85); g.fillRect(40, 75, jerryValue, 10); if (tomValue > jerryValue) g.drawString("Tom is bigger", 50,50); else g.drawString("Jerry is bigger", 50,50); }

10 Continuation of Example4 Continuation of Example4 public void adjustmentValueChanged(AdjustmentEvent event){ tomValue = tom.getValue(); jerryValue = jerry.getValue(); repaint();}} HTML code to invoke the program Example4 Example4 <applet code="Example4.class" width= 300 height=200> width= 300 height=200>

11 Buttons Buttons As with scrollbars, use of buttons is accomplished using methods on one of the Java libraries, the AWT (Abstract Window Toolkit) library. As with scrollbars, use of buttons is accomplished using methods on one of the Java libraries, the AWT (Abstract Window Toolkit) library. The are six steps to setting up a button: The are six steps to setting up a button: - State that the class implements ActionsListener. - State that the class implements ActionsListener. - Declare a button variable, giving it a name. - Declare a button variable, giving it a name. - Create a new button, giving it a label. - Create a new button, giving it a label. - Add the button to the window - Add the button to the window - Inform the button that this object will respond to - Inform the button that this object will respond to button events, using addActionListener. button events, using addActionListener. - Provide a method called actionPerformed to be - Provide a method called actionPerformed to be invoked when a button click event occurs. invoked when a button click event occurs.

12 Example5 (creating and using Buttons) Example5 (creating and using Buttons) import java.applet.Applet; import java.awt.*; import java.awt.event.*; public class Example5 extends Applet implements ActionListener{ private int diameter = 20; private Button little, large; public void init(){ little = new Button("Little"); add(little);little.addActionListener(this); large = new Button("Large"); add(large);large.addActionListener(this);} public void paint(Graphics g){ public void paint(Graphics g){ g.setColor(Color.blue); g.setColor(Color.blue); g.fillOval(25, 25, diameter, diameter); g.fillOval(25, 25, diameter, diameter); } public void actionPerformed(ActionEvent event){ public void actionPerformed(ActionEvent event){ if (event.getSource() == little) if (event.getSource() == little) diameter = diameter - 10; if (event.getSource()== large) diameter = diameter + 10; repaint(); }} Example5 Example5 <applet code="Example5.class" width= 300 height=200> width= 300 height=200>

13 Creating and Using Text Fields Creating and Using Text Fields import java.applet.*; import java.awt.*; import java.awt.event.*; public class Example6 extends Applet implements ActionListener{ private Button ageB; private TextField ageField; private int age; public void init(){ ageField = new TextField(10); add(ageField);ageField.addActionListener(this); ageB = new Button("MyAge"); add(ageB);ageB.addActionListener(this);} public void actionPerformed(ActionEvent event){ if (event.getSource() == ageB) if (event.getSource() == ageB) age = Integer.parseInt(ageField.getText()); age = Integer.parseInt(ageField.getText()); repaint(); repaint();} public void paint(Graphics g){ setBackground(Color.orange); setBackground(Color.orange); g.drawString("Age is " + age, 50, 50); if (age >= 18) g.drawString("You can vote", 50, 100); else g.drawString("You can not vote", 50,100) }} Example6 Example6 <applet code="Example6.class" width= 300 height=200> width= 300 height=200>

14 Repetitions – while, for and do Repetitions – while, for and do While loop (Example7) While loop (Example7) import java.awt.*; import java.applet.Applet; public class Example7 extends Applet{ public void paint(Graphics g){ int counter, x =5; counter = 0; while (counter < 8){ setBackground(Color.blue); setBackground(Color.blue); g.drawString("*", x, 20); x = x + 10; counter++; } }} HTML code to invoke the program Example7 Example7 <applet code="Example7.class" width= 300 height=200> width= 300 height=200>

15 For Loop For Loop In the for loop, many of the ingredients of the while loop are bundled up together in the statement itself. Example8 (how to use a for loop) import java.awt.*; import java.applet.Applet; public class Example8 extends Applet{ public void paint(Graphics g){ int n = 0; int x = 20; int y = 20; for (int i = 0; i < 8; i++){ setBackground(Color.cyan); setBackground(Color.cyan); g.drawLine(x, y, x + 100, y); y = y + 10; n++;}}} HTML code to invoke the program Example8 Example8 <applet code="Example8.class" width= 300 height=200> width= 300 height=200>

16 do loop do loop Example9 import java.awt.*; import java.applet.Applet; public class Example9 extends Applet{ public void paint(Graphics g){ int countOfSquares = 0; int countOfSquares = 0; int riceOnThisSquare = 1; int totalRice = 0; int y=20; do{ countOfSquares++; countOfSquares++; g.drawString("On Square" + countOfSquares + "are" + riceOnThisSquare, 10,y); g.drawString("On Square" + countOfSquares + "are" + riceOnThisSquare, 10,y); totalRice = totalRice + riceOnThisSquare; totalRice = totalRice + riceOnThisSquare; riceOnThisSquare = riceOnThisSquare * 2; riceOnThisSquare = riceOnThisSquare * 2; y=y+20; y=y+20; } while (totalRice < 100); while (totalRice < 100); g.drawString("Number of squares needed is " + countOfSquares, 10, y + 10); g.drawString("Number of squares needed is " + countOfSquares, 10, y + 10);}} html code to invoke Example9 class Example8 Example8 <applet code="Example8.class" width= 300 height=200> width= 300 height=200>

17 Objects and Classes Objects and Classes Object-oriented programming is about constructing programs form objects. An object is a combination of some data (variables) and some actions (methods). Object-oriented programming is about constructing programs form objects. An object is a combination of some data (variables) and some actions (methods). In Object Programming languages a class is the specification of for any number of objects that are the same. Once a class has been described, a particular object constructed by creating an instance of the class. In Object Programming languages a class is the specification of for any number of objects that are the same. Once a class has been described, a particular object constructed by creating an instance of the class.

18 Example10 – Creating a class or Object Example10 – Creating a class or Object import java.applet.Applet; import java.awt.*; import java.awt.event.*; public class Example10 extends Applet implements ActionListener, AdjustmentListener{ ActionListener, AdjustmentListener{ private Button grow, shrink, left,right, vertGrow,vertShrink; private Button grow, shrink, left,right, vertGrow,vertShrink; private Circle myCircle; private Circle myCircle; private Scrollbar red; private Scrollbar red; private Scrollbar green; private Scrollbar green; private Scrollbar blue; private Scrollbar blue; private int redValue; private int redValue; private int greenValue; private int greenValue; private int blueValue; private int blueValue; private Color color; private Color color;

19 Continuation of Example 10 Continuation of Example 10 public void init(){ grow = new Button("Grow"); grow = new Button("Grow"); add(grow); add(grow); grow.addActionListener(this); grow.addActionListener(this); shrink = new Button("Shrink"); shrink = new Button("Shrink"); add(shrink); add(shrink); shrink.addActionListener(this); shrink.addActionListener(this); left = new Button("left"); left = new Button("left"); add(left); add(left); left.addActionListener(this); left.addActionListener(this); right = new Button("right"); right = new Button("right"); add(right); add(right); right.addActionListener(this); right.addActionListener(this); vertGrow = new Button("Vertical Grow"); vertGrow = new Button("Vertical Grow"); add(vertGrow); add(vertGrow); vertGrow.addActionListener(this); vertGrow.addActionListener(this); vertShrink = new Button("Vertical Shrink"); vertShrink = new Button("Vertical Shrink"); add(vertShrink); add(vertShrink); vertShrink.addActionListener(this); vertShrink.addActionListener(this); Label l1, l2, l3; Label l1, l2, l3; l1 = new Label("Red:"); l1 = new Label("Red:"); l2 = new Label("Green:"); l2 = new Label("Green:"); l3 = new Label("Blue:"); l3 = new Label("Blue:"); red = new Scrollbar(Scrollbar.HORIZONTAL,0,1,0,1000); red = new Scrollbar(Scrollbar.HORIZONTAL,0,1,0,1000); green = new Scrollbar(Scrollbar.HORIZONTAL,0,1,0,1000); green = new Scrollbar(Scrollbar.HORIZONTAL,0,1,0,1000); blue = new Scrollbar(Scrollbar.HORIZONTAL,0,1,0,1000); blue = new Scrollbar(Scrollbar.HORIZONTAL,0,1,0,1000); add(l1); add(l1); add(red); add(red); add(l2); add(l2); add(green); add(green); add(l3); add(l3); add(blue); add(blue); red.addAdjustmentListener(this); red.addAdjustmentListener(this); green.addAdjustmentListener(this); green.addAdjustmentListener(this); blue.addAdjustmentListener(this); blue.addAdjustmentListener(this); myCircle = new Circle(); myCircle = new Circle();}

20 Continuation of Example 10 Continuation of Example 10 public void adjustmentValueChanged(AdjustmentEvent event){ redValue = red.getValue(); redValue = red.getValue(); greenValue = green.getValue(); greenValue = green.getValue(); blueValue = blue.getValue(); blueValue = blue.getValue(); color = new Color(redValue, greenValue,blueValue); color = new Color(redValue, greenValue,blueValue); showStatus("Red is: " + redValue + showStatus("Red is: " + redValue + "; Green is:" + greenValue + "; Green is:" + greenValue + "; Blue is: " + blueValue); "; Blue is: " + blueValue); repaint(); repaint(); } public void actionPerformed(ActionEvent event){ if (event.getSource() == grow ) if (event.getSource() == grow ) myCircle.grow(); myCircle.grow(); if (event.getSource() == shrink) if (event.getSource() == shrink) myCircle.shrink(); myCircle.shrink(); if (event.getSource() == left) if (event.getSource() == left) myCircle.left(); myCircle.left(); if (event.getSource() == right) if (event.getSource() == right) myCircle.right(); myCircle.right(); if (event.getSource() == vertGrow) if (event.getSource() == vertGrow) myCircle.vertGrow(); myCircle.vertGrow(); if (event.getSource() == vertShrink) if (event.getSource() == vertShrink) myCircle.vertShrink(); myCircle.vertShrink(); repaint(); repaint();}

21 Continuation of Example 10 Continuation of Example 10 public void paint(Graphics g){ g.setColor(color); g.setColor(color); myCircle.display(g); myCircle.display(g); }} class Circle{ class Circle{ private int height = 10; private int height = 10; private int width = 10; private int width = 10; private int xCoord = 20, yCoord = 50; private int xCoord = 20, yCoord = 50; private int red = 0; private int red = 0; private int greeen = 0; private int greeen = 0; private int blue = 0; private int blue = 0; private Color color; private Color color; public void display(Graphics g){ public void display(Graphics g){ g.fillOval(xCoord, yCoord,width, height); g.fillOval(xCoord, yCoord,width, height); } public void left(){ public void left(){ xCoord = xCoord - 10; xCoord = xCoord - 10; } public void right(){ public void right(){ xCoord = xCoord + 10; xCoord = xCoord + 10; } public void grow(){ public void grow(){ width = width + 10; width = width + 10; height = height +10; height = height +10; } public void shrink(){ public void shrink(){ width = width - 10; width = width - 10; height = height -10; height = height -10; } public void vertGrow(){ public void vertGrow(){ height = height + 10; height = height + 10; }

22 Continuation of Example 10 Continuation of Example 10 public void vertShrink(){ height = height - 10; height = height - 10; } public void newColor(int red, int green,int blue){ public void newColor(int red, int green,int blue){ color = new Color(red,green,blue); color = new Color(red,green,blue); } }<html><head> Web page Example10 Web page Example10 </head><body> </applet></body></html>

23 Questions? Questions?


Download ppt "Java Applet Presented by Fitsum Okubu. Introduction Introduction Graphics Graphics Methods and Variables Methods and Variables Events Events Decision."

Similar presentations


Ads by Google