0

I am trying to make code that adds a label when I click and then removes the label when I press e. Can someone help me do this I added the labels and I got it to compile but the label won't show up and its for my graphics project I worked on it really hard

import java.awt.geom.*;
import java.awt.*;
import javax.swing.*;
import java.io.*;
import java.awt.event.*;
import java.applet.*;
import java.util.*;
import java.awt.Label;
public class Volcano_Client extends Applet implements MouseListener, KeyListener, Runnable
{
  Label label1;
 boolean pickE=false;
 boolean play = true;
 boolean makeLava=false;
 Thread t;
 Thread thr;
 Thread th;
 boolean mouseEntered;
 int count;
 Volcano v1;
 public void init()
 {
    label1 = new Label("You see lava flowing down as you press e");

    v1 = new Volcano();
    thr=new Thread(this);
    thr.start();
    t=new Thread(this);
    t.start();
    th= new Thread(this);
    count=0;
    addMouseListener(this);
    addKeyListener(this);
 }
 public void keyTyped(KeyEvent k)
 {
 }
 public void keyReleased(KeyEvent k)
 {
 }
 public void keyPressed(KeyEvent k)
 {
    if(k.getKeyCode() ==KeyEvent.VK_E)
    {
        pickE=true;
        repaint();
        k.consume();
    }
 }
    public void mouseExited(MouseEvent me)
 {
    mouseEntered=false;
    repaint();
 }
 public void mousePressed(MouseEvent me)
 {
 }
 public void mouseReleased(MouseEvent me)
 {
 }
 public void mouseEntered(MouseEvent me)
 {

    mouseEntered=true;
    repaint();
 }
 public void mouseClicked(MouseEvent me)
 {

    if(mouseEntered==true)
    {
        makeLava=true;
    }
    else
    {
        makeLava=false;
    }
    repaint();
 }
 public void run()
 {
     while(play==true)
    {
        try
        {
            Thread.sleep(100);
        }
        catch(InterruptedException e)
        {
            e.printStackTrace();
        }
        repaint();
    }
    while(true)
    {
        try{
            Thread.sleep(20);
        }
        catch(Exception e){};
        repaint();
    }
 }
 public void stop()
 {
 }
 public void start()
 {
 }
 public void paint(Graphics g)
 {
    Graphics2D g2 = (Graphics2D)g;
    label1 = new Label("You see lava flowing down as you press e");
    v1.makeVolcano(g2);
    if(makeLava)
    {
      v1.makeLavaUp(g2);
      g.setColor(Color.black);
      g.drawString("KABOOM!!!",200,70);
      add(label1);

    }
    else
    {
        g.setColor(Color.white);
        g.drawString("Please Click For Eruption part 1",160,250);
    }
    if(pickE)
    {
        v1.makeLavaFlow(g2);
        remove(label1);
    }
    else
    {
        //g.setColor(Color.white);
        //g.drawString("Please press 'E' for Eruption part 2",160,276);
    }

 }

}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • I already have some code. – Dwane Brown Jr Apr 25 '16 at 14:58
  • 1
    post your tried code into this question, and let us know what you did wrong !! – Vishal Gajera Apr 25 '16 at 15:00
  • I don't know how i am kind of new to this – Dwane Brown Jr Apr 25 '16 at 15:01
  • Java Applets are a deprecated technology, and you (or better yet, your prof) should consider migrating to something else. – Tim Biegeleisen Apr 25 '16 at 15:01
  • Its for a java class. I have to constantly add to it. – Dwane Brown Jr Apr 25 '16 at 15:05
  • 1) Why code an applet? If it is due to the teacher specifying it, please refer them to [Why CS teachers should **stop** teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). 2) See [Java Plugin support deprecated](http://www.gizmodo.com.au/2016/01/rest-in-hell-java-plug-in/) and [Moving to a Plugin-Free Web](https://blogs.oracle.com/java-platform-group/entry/moving_to_a_plugin_free). .. – Andrew Thompson Apr 26 '16 at 03:24
  • .. 3) Why use AWT? See [this answer](http://stackoverflow.com/questions/6255106/java-gui-listeners-without-awt/6255978#6255978) for many good reasons to abandon AWT using components in favor of Swing. – Andrew Thompson Apr 26 '16 at 03:24
  • I haven't learned that yet for java. Its only a java 1 class.. – Dwane Brown Jr Apr 26 '16 at 14:23

1 Answers1

-1

To display the label, below is modified init() function:

 public void init()
`{`
`label1 = new Label("You see lava flowing down as you press e");`

       //v1 = new Volcano();
    thr=new Thread(this);
    thr.start();
    t=new Thread(this);
    t.start();
    th= new Thread(this);
    count=0;

    addMouseListener(this);
    addKeyListener(this);

    add(label1); //to display label
 }


Ktonneh
  • 112
  • 8