3 Replies - 2143 Views - Last Post: 21 November 2010 - 08:01 AM Rate Topic: -----

#1 jess2705  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 20-November 10

java applet

Posted 20 November 2010 - 11:13 PM

Write a program with a Menu. In that there are three Menu Color (Red,Green,Blue),Font(any three) and Calculation (Addition, Substraction, Division, Multipication).When you click on Color menu then it changes the particular background of the applet. When you click on particular Font then it changes the particular Font of that Label. When you click particular item in Calculation menu then one DialogBox should be opened in that there are three text boxes and one button is there in first two Textboxes are No1 and No2 and in third textbox the ans is display when you click on button.
import java.awt.event.*;
import java.awt.*;
import java.applet.*;

//<applet code="MenuApp" width=300 height = 300></applet>

class dialog extends Dialog implements ActionListener
{
	Button btn;
	TextField t1,t2,t3;
	Label l1,l2,l3;
	myMenu m;
	GridLayout gl1,gl2,gl3;
	int a,b,c=0;
	Panel p1,p2,p3;
	dialog(myMenu m,String s)
	{
		super(m,s);
		this.setSize(200,200);
		this.setVisible(true);
		l1 = new Label("Enter first Value");
		l2 = new Label("Enter second Value ");
		l3 = new Label("Answer is");
		t1 = new TextField(3);
		t2 = new TextField(3);
		t3 = new TextField(3);
		t3.setEditable(false);
		btn = new Button();

		if(s=="Addition")
		{
			btn.setLabel("+");
		}
		if(s=="Subtraction")
		{
			btn.setLabel("-");
		}
		if(s=="Division")
		{
			btn.setLabel("/");
		}
		if(s=="Multiplication")
		{
			btn.setLabel("*");
		}

		btn.addActionListener(this);
	     	p1 = new Panel();
		p1.setLayout(new GridLayout(3,3));
		p1.add(l1);
		p1.add(t1);
		p1.add(l2);
		p1.add(t2);
		p1.add(l3);
		p1.add(t3);
		add(p1);
		add(btn,"South");
		
		addWindowListener(new WindowAdapter(){
		public void windowClosing(WindowEvent we)
		{
			dispose();
		}});
		
	}
	
	public void actionPerformed(ActionEvent ae)
	{
		a = Integer.parseInt(t1.getText());
		b = Integer.parseInt(t2.getText());
		if(ae.getActionCommand().equals("+"))
		{
			c = a + b;
		}
		if(ae.getActionCommand().equals("-"))
		{
			c = a - b;
		}
		if(ae.getActionCommand().equals("*"))
		{
			c = a  * b;
		}
		if(ae.getActionCommand().equals("/"))
		{
			c = a / b;
		}
		t3.setText(" "+c);
	}
}



class myMenu extends Frame implements ActionListener
{
	MenuApp ma;
	int n;
	
	myMenu(String title, MenuApp ma)
	{
		super(title);
		this.ma= ma;
		
		MenuBar mb = new MenuBar();
		setMenuBar(mb);

		Menu m1 = new Menu("Color");
		Menu m2 = new Menu("Font");
		Menu m3 = new Menu("Calculation");
		mb.add(m1);
		mb.add(m2);
		mb.add(m3);

		//Color menu and menuItems
		MenuItem m11 = new MenuItem("Red");
		m1.add(m11);
		m11.addActionListener(this);
		MenuItem m12 = new MenuItem("Green");
		m1.add(m12);
		m12.addActionListener(this);
		MenuItem m13 = new MenuItem("Blue");
		m1.add(m13);
		m13.addActionListener(this);

		//Font menu and MenuItems
		MenuItem m21 = new MenuItem("Viner Hand ITC");
		m2.add(m21);
		m21.addActionListener(this);
		MenuItem m22 = new MenuItem("Terminal");
		m2.add(m22);
		m22.addActionListener(this);
		MenuItem m23 = new MenuItem("Verdana");
		m2.add(m23);
		m23.addActionListener(this);

		//Calculation menu and MenuItems
		MenuItem m31 = new MenuItem("Addition");
		m3.add(m31);
		m31.addActionListener(this);
		MenuItem m32 = new MenuItem("Subtraction");
		m3.add(m32);
		m32.addActionListener(this);
		MenuItem m33 = new MenuItem("Division");
		m3.add(m33);
		m33.addActionListener(this);
		MenuItem m34 = new MenuItem("Multiplication");
		m3.add(m34);
		m33.addActionListener(this);
	
		addWindowListener(new WindowAdapter(){
		public void windowClosing(WindowEvent we)
		{
			setVisible(false);
		}});

	}
	
	public void actionPerformed(ActionEvent ae)
	{
		String str = ae.getActionCommand();
		if (str.equals("Red"))
			ma.setBackground(Color.red);
		else if(str.equals("Blue"))
			ma.setBackground(Color.blue);
		else if(str.equals("Green"))
			ma.setBackground(Color.green);
		if(str.equals("Viner Hand ITC"))
		{
			ma.myMethod(1);
			//ma.repaint();
		}
		if(str.equals("Terminal"))
		{
			ma.myMethod(2);
		}
		if(str.equals("Verdana"))
		{
			ma.myMethod(3);
		}
		
		if(str.equals("Addition"))
		{
			dialog dlg = new dialog(this,str);
		}
		if(str.equals("Subtraction"))
		{
			dialog dlg = new dialog(this,str);
		}
		if(str.equals("Division"))
		{
			dialog dlg = new dialog(this,str);
		}
		if(str.equals("Multiplication"))
		{
			dialog dlg = new dialog(this,str);
		}

	}
		
	
}


public class MenuApp extends Applet
{
	Label l;
	int N = 0;
	public void init()
	{
		myMenu mm = new myMenu("Application", this);
		mm.setSize(300,300);	
		mm.show();
		l = new Label("                       JAVA                   ", Label.CENTER);
		Panel p = new Panel();
		p.add(l);
		add(p,"SOUTH");
		showStatus("!!!!JAVA!!!");
		Font font = new Font("Viner Hand ITC", 0, 20);	
		l.setFont(font);
	}
	
	public void myMethod(int n)
	{
		N= n;
		//return N;
		repaint();
	}
	public void paint(Graphics g)
	{
		g.drawString("Welcome!!!",6,60);
		if (N==1)
		{
			g.drawString(""+N,6,70);
			Font font = new Font("Viner Hand ITC", 0, 20);	
			setFont(font);
		}
		if (N==2)
		{
			g.drawString(""+N,6,70);
			Font font = new Font("Terminal", 0, 30);	
			setFont(font);

		}
		if (N==3)
		{
			g.drawString(""+N,6,70);
			Font font = new Font("Verdana", 0, 10);	
			setFont(font);
		}
		
	}
}



-the flicker is genrated,when program is run how to remove flicker??

Is This A Good Question/Topic? 0
  • +

Replies To: java applet

#2 -shadow-  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 17
  • View blog
  • Posts: 204
  • Joined: 18-November 10

Re: java applet

Posted 20 November 2010 - 11:34 PM

What do you mean by flicker?
is it doing a constant repaint()?
Was This Post Helpful? 0
  • +
  • -

#3 MrLuke187  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 81
  • View blog
  • Posts: 237
  • Joined: 09-July 10

Re: java applet

Posted 21 November 2010 - 07:57 AM

If you have problems with flickering you need the update()-Method in your Applet Class. This might look like this:
public void update(Graphics g){
	if (dbimage == null){
		// Create the Image in the Memory:
		dbimage = createImage(this.getSize().width, this.getSize().height);
		// Create a "Graphics"-Object from it:
		dbg = dbimage.getGraphics();
	}
	dbg.setColor(getBackground());
	// Give the it to the paint() method to paint on it:
	paint(dbg);
	// Now draw it on the Canvas:
	g.drawImage(dbimage, 0, 0, this);
}



You also need the two Variables Image dbimage and Graphics dbg for the whole class:
private Image dbimage;
private Graphics dbg;

public void init(){...}



if you use the repaint()-Method to repaint you Applet, the flickering should be gone.

You might also want to look in here (last Point "Optimizing the Clock") where the same Problem was discussed.

Greetings: Luke

This post has been edited by MrLuke187: 21 November 2010 - 07:59 AM

Was This Post Helpful? 0
  • +
  • -

#4 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2857
  • View blog
  • Posts: 10,960
  • Joined: 15-July 08

Re: java applet

Posted 21 November 2010 - 08:01 AM

And why do you have a Frame holding an Applet? This makes no sense. You need to in some way control the repainting as well. Try using a Swing Timer and Swing components, as it will make this sort of thing easier to do. In addition, it may help if you painted on a Panel instead of directly on an Applet, due to the "special" features of the Applet.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1