简体   繁体   中英

Java Applet and Console

I'm new in building Java applets I want to show what it's display in my console in the Java applet, I know you should use the paint method, but I don't know how to call it in the code.

public void paint(Graphics g) {        
    //Draw a rectangle width=250, height=100       
    g.drawRect(0,0,250,100);         
    //Set the color to blue      
    g.setColor(Color.blue);         
    //Write the message to the web page       
    g.drawString("Running",10,50);   

This is what I currently have, the whole program it's made to run from the console in

System.out.println(x)

is there anyway to call something like

paint(x)

to show the same on the applet as is show on the console? Plus I need it to refresh constantly.

You don't have a call to the superclass method. Put

super.paint(g);

before anything else.

If you want to display a message on the applet at the same time as on the console, you need to have a method that calls repaint();. The way I would set this up is to have an Arraylist of Strings. When you want something written to the applet output, you add the new String to the ArrayList, then call repaint(). Then, in paint(),

ArrayList<String> ar = new ArrayList();
public void paint(Graphics g) {   
    super.paint();
    //Draw a rectangle width=250, height=100       
    g.drawRect(0,0,250,100);         
    //Set the color to blue      
    g.setColor(Color.blue);         
    //Write the message to the web page
    for(int i = 0; i<ar.size(); i++)
        g.drawString(ar.get(i), 10, 50); }

If you want a function that does this, you might write it like so

public void appletOut.println(String str){
   ar.add(str);
   repaint();
}

Finally, I question whether this route is really best for what you are trying to accomplish. I would suggest researching text fields and scrollbars and seeing if that might fit your parameters better.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM