Solved

Buttons/Events/Colors in 1.0 Model

Posted on 1998-05-05
6
142 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 50 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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

705 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now