LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Java Applet and Displaying text (https://www.linuxquestions.org/questions/programming-9/java-applet-and-displaying-text-795252/)

Larry James 03-13-2010 11:21 PM

Java Applet and Displaying text
 
I’m having various problems with the graphic printing features for applets.

In this immediate applet, when I print and update the previous line bleeds through the next line. I tried printing a series of spaces to clear the first text, but it doesn’t work.

Can someone provide some tips on resolving this issue?

Code:

import java.awt.Graphics;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import java.net.InetAddress;

public class checkconnect extends JApplet{
    @Override
    public void paint(Graphics g)
    {
        String status1 = "Checking...";
        boolean status = false;
        g.drawString(status1,1,10);
        // g.drawString{"test",1,10};
        try {
         
            String host = "127.0.0.1";
            int timeOut = 1450;
            status = InetAddress.getByName(host).isReachable(timeOut);
        } catch (IOException ex) {
            Logger.getLogger(checkconnect.class.getName()).log(Level.SEVERE, null, ex);
        }
        if ( status ){
        status1 = "Success";
        }
        g.drawString("          ",1.10); //an attempt to cleanup the previous text.
        g.drawString(status1,1,10);     
    }
}

Thanks!

-- L. James

--
L. D. James
ljames@apollo3.com
www.apollo3.com/~ljames

khodeir 03-14-2010 10:35 AM

could you provide the output screen

Larry James 03-14-2010 12:27 PM

Quote:

Originally Posted by khodeir (Post 3897831)
could you provide the output screen

The white space on the blue is the applet's output:

http://image.apollo3.com/image/gen/1...playOutput.jpg

-- L. James

--
L. D. James
ljames@apollo3.com
www.apollo3.com/~ljames

khodeir 03-14-2010 05:03 PM

is this line correct
Quote:

g.drawString(" ",1.10);
or do u mean
Quote:

g.drawString(" ",1,10);

Larry James 03-14-2010 05:10 PM

Quote:

Originally Posted by khodeir (Post 3898183)
is this line correct

or do u mean

The one with the "," (the correct one) is the one that was compiled. It seems that I made a mistake when extracting the example for the message.

I have since tested the message sample. It's running with the "," in the picture I outputted to you. It won't compile without the proper syntax.

The image I linked is the one with the comma... the proper syntax.

-- L. James

--
L. D. James
ljames@apollo3.com
www.apollo3.com/~ljames

khodeir 03-15-2010 03:17 AM

I am very busy but I will tell u a little hint
spaces did not remove the words
why don't u search a character that looks like a white box??
search in the asci characters for that

ntubski 03-15-2010 10:38 AM

Quote:

Originally Posted by khodeir (Post 3898713)
why don't u search a character that looks like a white box??

No, don't do that, use clearRect instead.

Another problem with the code is that paint() is the wrong place for the ip checking code: paint() is called by the system whenever the screen needs to be drawn, so just covering and uncovering the window would cause the ip to checked again.

Larry James 03-15-2010 11:44 AM

Quote:

Originally Posted by khodeir (Post 3898713)
I am very busy but I will tell u a little hint
spaces did not remove the words
why don't u search a character that looks like a white box??
search in the asci characters for that

Thanks. This I could tell from the output. I believe I had been on a good track. With a lot of experimenting, I found that I could draw a box that would clear the previous text:

Code:

g.clearRect(1, 1, 100, 20);
It wasn't working before because it wasn't wide enough.

I'll look for a character that will do what you say and compare which would be more efficient.

I searched hard to find examples of reprinting but couldn't.

By the way, it's not very clean to have to have all my code embeded in the "public void paint(Graphics g)" block just to print. At present I'm very restricted become of of my could will error when trying to contain it in that box.

I'm still trying to figure out a clean way to work outside that "public void paint(Graphics g)" block, but have to do with this workaround for now.

I tried:

Code:

import java.awt.*;
import javax.swing.*;

public class quicktest extends java.applet.Applet {

    Graphics g;

    class doSomeWork {
        // work with mysql database and other tools

        doSomeWork() {
            // print results from work done.
            g.drawString("Out Put from work done", 100, 100);
        }
    }
}

In that example the "g.drawString", while it compiles, it doesn't output anything. I realize it's because the "Graphics g" is outside the "paint()" container. But I can't figure out how to provide the "paint()" container to all the other blocks that follows.

Quote:

Originally Posted by ntubski (Post 3899134)
No, don't do that, use clearRect instead.

Another problem with the code is that paint() is the wrong place for the ip checking code: paint() is called by the system whenever the screen needs to be drawn, so just covering and uncovering the window would cause the ip to checked again.

Thanks for the input, Ntubski. I can't believe it, but I had already replied to the message this morning but clicked Preview instead of submit.

You're right. The clearRect does better.

-- L. James

--
L. D. James
ljames@apollo3.com
www.apollo3.com/~ljames

Larry James 03-15-2010 06:06 PM

Quote:

Originally Posted by ntubski (Post 3899134)
No, don't do that, use clearRect instead.

Another problem with the code is that paint() is the wrong place for the ip checking code: paint() is called by the system whenever the screen needs to be drawn, so just covering and uncovering the window would cause the ip to checked again.

You are so right about the paint(). I'm going to try to figure out how to replace that and the g.drawString() with something else.

The problem is very apparent when you look at http://faq.apollo3.com/ljames/fsinn/...testservers.pl .

I'm trying to use some variation of the "HelloWorld" and the "theApplet" examples you gave in the thread about output. However, they are so dependent on the other classes that are included in the codes that they fail to work without including the other classes.

Dissecting the other classes and retaining functionality is one of my immediate projects.

-- L. James

--
L. D. James
ljames@apollo3.com
www.apollo3.com/~ljames


All times are GMT -5. The time now is 08:37 PM.