Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Buttons/Events/Colors in 1.0 Model

Posted on 1998-05-05
6
Medium Priority
?
185 Views
Last Modified: 2006-11-17
I've been trying to make an easier roll-over applet using buttons, changing the colors of the text and button based on the movement of the mouse in 1.0 model for older browsers.  The problem is this.  When I use Netscape 3.0, the button colors don't change (I use setBackground) but the events work (and the text/Foreground color changes as it should).  When I use Netscape 4.0, the button colors change fine, but the events aren't working.  

If it would help to see what I'm talking about, go to:
 http://www.mnplan.state.mn.us/datanetweb/Martintmp/Hover/h.html (and h2.html)

h.html is the applet I have that works, but doesn't use buttons.  h2.html uses the buttons and displays the problem described above.  I think I have the code up as well.

What's going on and how do I fix it?

webslider
0
Comment
Question by:webslider
  • 3
  • 3
6 Comments
 
LVL 5

Expert Comment

by:fontaine
ID: 1220575
I have tried with N4.0 and h2.html, the Java console shows that there is a java.lang.NullPointerException in your program. If you post it, I can take a quick look.
0
 

Author Comment

by:webslider
ID: 1220576
if you just look at:
http://www.mnplan.state.mn.us/datanetweb/Martintmp/Hover/

The directory is readable.  Look at hover2.java.


0
 
LVL 5

Accepted Solution

by:
fontaine earned 200 total points
ID: 1220577
When you try to make your own component, you should rather subclass Panel for example. If you compare your implementation with Button.java, you will see that most of the methods of Button are no more valid. In fact, all that remains is...the 3D border. When you try to change the color of a component, don' forget to call its repaint() method. Also, inner classes are JDK 1.1 (your MultiButton is implemented as an inner class in hover2.java). All of this put together gives the following code:

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;

public class hover2 extends Applet {
   MultiButton mbutton;
   URL link = null;
   String line1txt = "";
   String line2txt = "";
   Color bkcolor = Color.white;
   Color bktxtcolor = Color.black;
   Color h_bkcolor = Color.white;
   Color h_bktxtcolor = Color.black;

   public void init() {
      setLayout(null);

     /*  Get Parameters */
      String param = new String();
      param = getParameter("text1");
      if (param != null)
        line1txt = param;  

      param = getParameter("text2");
      line2txt = param;   // if null, won't print on button

      param = getParameter("color");  // regular backgroundcolor
      if (param != null) {
         if (param.charAt(0) == 35) {   // 1st char is a '#'
             param = param.substring(1);  // get all chars but '#'
             bkcolor = new Color(Integer.parseInt(param,16));   // # b/t 0-255
         } else
             bkcolor = new Color(255,255,255);
      }

      param = getParameter("textcolor");  // regular textcolor
      if (param != null) {
         if (param.charAt(0) == 35) {   // 1st char is a '#'
             param = param.substring(1);  // get all chars but '#'
             bktxtcolor = new Color(Integer.parseInt(param,16));   // # b/t 0-255
         } else
             bktxtcolor = new Color(0,0,0);
      }

      mbutton = new MultiButton(line1txt,line2txt);
      mbutton.resize(size().width,size().height);
      mbutton.setBackground(bkcolor);
      mbutton.setForeground(bktxtcolor);
      add(mbutton);
  }

  public boolean mouseEnter(Event e, int x, int y) {
       mbutton.setBackground(Color.green);
       mbutton.setForeground(Color.blue);
       mbutton.repaint();
       return true;
  }

  public boolean mouseExit(Event e, int x, int y) {
       mbutton.setBackground(bkcolor);
       mbutton.setForeground(bktxtcolor);
       mbutton.repaint();
       return true;
  }

  public boolean mouseDown(Event e, int x, int y) {
       mbutton.setBackground(Color.red);
       mbutton.setForeground(Color.white);
       mbutton.repaint();
       return true;
   }

   public boolean mouseUp(Event e, int x, int y) {

      try {
        link = new URL(getDocumentBase(),"other.html");
      } catch (MalformedURLException mue) {
            System.out.println("URL error");
      }

      getAppletContext().showDocument(link,"_self");
      return true;
   }


}

   class MultiButton extends Panel {
       String label1, label2;

       MultiButton(String s1, String s2) {
           label1 = s1;
           label2 = s2;
       }

       public void paint(Graphics g) {
            super.paint(g);
            Rectangle r = bounds();

            if (label1 != null) {
                 g.drawString(label1,r.x+5, r.y+r.height/3);
                 g.drawString(label2,r.x+5, r.y+(2*r.height)/3);
            } else {
                 g.drawString(label1,r.x+5, r.y+r.height/3);
            }
       }


   }

The code does what you need and has been tested on Netscape 4.0 and IE 4.0.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:webslider
ID: 1220578
So, are you saying that there is no way I can use buttons versus panels to do the roll over?  
0
 
LVL 5

Expert Comment

by:fontaine
ID: 1220579
The Button class provided with the AWT is a heavyweight component. This means that it is implemented through platform-dependent GUI. Because of that, you can't directly change it by overriding paint(). The effect is simply not guaranteed. If you try your original code with IE, for example, you will see that the two-lines of text that serve as label are not shown. The following article will interest you:

http://www.javaworld.com/javaworld/jw-03-1997/jw-03-imagebutton.html
0
 

Author Comment

by:webslider
ID: 1220580
Thanks so much!
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question