Results 1 to 2 of 2
- 05-07-2008, 05:01 PM #1
Member
- Join Date
- May 2008
- Posts
- 2
- Rep Power
- 0
what is wrong with this program ?
This applet is to create two labels ( Oval ) and ( Line ) . so when we click the button Oval it draws Oval shape , and same with Line ..
*** The problem is when I run the prog .. Its shows the 2 buttons and the Oval shape is already there .. when I click the (Line) button it doesnt work ( it changes the Oval color from yellow to green ) ...
** I want to have separate action for each button. .
so when I click Oval .. it shows Oval with one color
and when I click Line ..it shows a line with one color ( the Oval shoud be disppear from da screen when Line is viewd )...
This is my program :
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class Two extends Applet implements ActionListener
{
Button g,y;
char c;
public void init()
{
setLayout(new FlowLayout());
g=new Button("Oval");
y=new Button("Line");
g.addActionListener(this);
y.addActionListener(this);
add(g);
add(y);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==g)
{
c='g';
repaint();
}
if(e.getSource()==y)
{
c='y';
repaint();
}
}
public void paint(Graphics g)
{
g.setColor(Color.green);
if(c=='g')
g.setColor(Color.yellow);
if(c=='y')
g.drawLine(100,100,100,100);
g.fillOval(100,100,100,100);
}
}Last edited by Poor Bee; 05-07-2008 at 05:03 PM.
- 05-07-2008, 07:23 PM #2
Java Code:// <applet code="AppletTwo" width="400" height="400"></applet> // atThePrompt>appletviewer AppletTwo.java import java.awt.*; import java.applet.*; import java.awt.event.*; public class AppletTwo extends Applet implements ActionListener { Button g,y; char c; public void init() { System.out.println("default layout = " + getLayout().getClass().getName()); setLayout(new FlowLayout()); g=new Button("Oval"); y=new Button("Line"); g.addActionListener(this); y.addActionListener(this); add(g); add(y); } public void actionPerformed(ActionEvent e) { if(e.getSource()==g) { c='g'; } if(e.getSource()==y) { c='y'; } repaint(); } public void paint(Graphics g) { if(c=='g') { g.setColor(Color.yellow); g.fillOval(100,100,100,100); } if(c=='y') { g.setColor(Color.green); // x1, y1, x2, y2 // g.drawLine(100,100,100,100); g.drawLine(50,50,200,200); } } }
Similar Threads
-
wrong values
By mark-mlt in forum New To JavaReplies: 8Last Post: 04-25-2008, 11:11 AM -
I am Doing Something Wrong But Don't Know What?
By BHCluster in forum New To JavaReplies: 3Last Post: 04-16-2008, 01:16 PM -
what is wrong with this code
By masaka in forum New To JavaReplies: 5Last Post: 04-16-2008, 08:27 AM -
What's wrong with this code?
By Wizard wusa in forum New To JavaReplies: 14Last Post: 01-22-2008, 11:55 PM -
Is there somethign wrong with this code?
By Soda in forum New To JavaReplies: 1Last Post: 12-08-2007, 04:46 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks