Sunday, August 8, 2010

Java Related Ques?

This post is dedicated to discussion related to Java problem, So do not post general programming concept related problems over here.

10 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. Hello,
    The problem with your code is that you are calling update in your paint function, then you can not do anything in paint, as control has already been transfered, so now just put your code after update(g) into your update function itself.
    public void update(Graphics g)
    {
    g.setColor(Color.red);
    g.fillRect(this.x,80,100,32);
    g.setColor(Color.black);
    g.drawString(this.msg,this.x,100);
    }
    public void paint(Graphics g)
    {
    update(g);
    }
    Here I am assuming that you have appropriate run function in place.
    I hope that am able to hep you.
    Regards,
    Amar Arora
    Note: Please write name along with your Ques.

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
  4. solution of
    Lets's go to each error one by one,
    First inetCheck() is user defined non static user defined function, so that can not be used in main until you declare it also as static, because non static member function can not be used in side static member function
    Secondly getHostName() is again a non static function of InetAddress class, so that;s again can't be used with class name, it should have some object associated with it.
    Last is getHostname() is reverse DNS function which coverts IP Address in to a Host Name string, so it should be passed to a string variable only.
    please find the corrected program as attachment, which will give you a better overview.
    Please post this conversion with your code as well as corrected code on blog, so that others can also be benefited.
    Hope able to answer your queries,:)







    import java.net.*;
    import java.io.*;

    class TestInet
    {

    public static void main(String args[])
    {
    try{

    InetAddress inet = InetAddress.getByName("69.147.125.65");
    InetAddress inet1 = InetAddress.getLocalHost();
    InetAddress inet2[] = InetAddress.getAllByName("www.google.com");
    //InetAddress inet3 = InetAddress.getHostName();

    System.out.println ("Host of IP is[inet.getByName]: " + inet.getHostName());
    System.out.println("Localhost is[inet.getLocalHost]: " + inet1);
    for(int a = 1; a < inet2.length; a++)
    {
    System.out.println("IPs of google are[i.getAllByName]: " + inet2[a]);
    }

    inetCheck(inet);
    //System.out.println("Localhost is[inet.getHostName]: " + inet3);
    }
    catch(Exception e)
    {
    e.printStackTrace();
    }

    }

    private static void inetCheck(in)
    {
    int inet3 = in.hashCode();
    System.out.println("Localhost is[in.getHostName]: " + inet3);
    }

    }

    ReplyDelete
  5. /// program moving banner in frame
    import java.awt.event.WindowEvent;
    import java.awt.*;
    import java.awt.event.*;
    public class Sampleframe extends Frame implements Runnable {
    int x ;
    String msg="";
    Boolean bval;
    Thread t;

    public void init()
    { this.x=0;
    this.t = new Thread(this);
    msg = "Dj aws";
    }


    public void start() {
    this.bval = true;
    this.t.start();

    }

    public void run()
    {
    try
    {
    while (true) {
    Thread.sleep(100);

    if (this.x > 240)
    {
    this.x = 0;

    }
    this.x += 1;

    this.msg = ("dj aws " + this.x);
    repaint();

    if (!this.bval) break;
    }
    } catch (Exception localException) {
    System.out.println(localException);
    }
    }
    public void paint(Graphics g)
    {
    g.drawString("in frame", 100, 200);
    g.setColor(Color.red);
    g.fillRect(x,80,100,32);
    g.setColor(Color.black);
    g.drawString(this.msg, this.x+5, 100);

    }
    public void stop() {
    this.bval = false;
    }
    Sampleframe(String title)
    {
    super(title);
    window adapter = new window(this);
    addWindowListener(adapter);
    }

    public static void main (String args[])
    {
    Sampleframe app = new Sampleframe("hello");
    app.setBackground(Color.DARK_GRAY);
    app.setSize(250,250);
    app.setVisible(true);
    app.init();
    app.start();


    }
    }
    class window extends WindowAdapter
    {
    Sampleframe fm;
    public window(Sampleframe fm)
    {
    this.fm = fm;
    }
    public void windowClosing(WindowEvent we)
    { System.exit(0);
    //fm.setVisible(false);
    }
    }
    // regards dj

    ReplyDelete
  6. import java.awt.event.WindowEvent;
    import java.awt.*;
    import java.awt.event.*;
    public class Sampleframe extends Frame implements Runnable {
    int x,y ;
    String msg="";
    Boolean bval,right,top;
    Thread t;

    public void init()
    { this.x=1;
    this.y=1;
    this.t = new Thread(this);
    msg = "Dj aws";
    }


    public void start() {
    this.bval = true;
    right = true;
    top = false;
    this.t.start();

    }

    public void run()
    {
    try
    {
    while (true) {
    Thread.sleep(20);
    if(right)
    {
    if(x >240)
    right = false;
    else
    x +=1;

    }
    else
    {
    if(x<1)
    right= true;
    else
    {
    x -=1;
    if (x==120)
    { top = true;
    y++;
    }




    }
    }






    //this.x == 1;

    this.msg = ("Dj aws " + this.x);
    repaint();

    if (!this.bval) break;
    }
    } catch (Exception localException) {
    System.out.println(localException);
    }
    }
    public void paint(Graphics g)
    {
    g.drawString("in frame", 100, 200);
    g.setColor(Color.red);
    g.fillRoundRect(x, 80, 100, 32, 44, 30);

    g.setColor(Color.black);
    g.drawString(this.msg, this.x+5, 100);

    }
    public void stop() {
    this.bval = false;
    }
    Sampleframe(String title)
    {
    super(title);
    window adapter = new window(this);
    addWindowListener(adapter);
    }

    public static void main (String args[])
    {
    Sampleframe app = new Sampleframe("hello");
    app.setBackground(Color.magenta);
    app.setSize(250,250);
    app.setVisible(true);
    app.init();
    app.start();


    }
    }
    class window extends WindowAdapter
    {
    Sampleframe fm;
    public window(Sampleframe fm)
    {
    this.fm = fm;
    }
    public void windowClosing(WindowEvent we)
    { System.exit(0);
    //fm.setVisible(false);
    }
    }

    ReplyDelete
  7. great work dj, that's really a good one...
    keep going like that,
    I expect others also to participate as much as possible, and make this information sharing a great success....

    ReplyDelete
  8. This comment has been removed by the author.

    ReplyDelete
  9. sir check ur mail there is a program name "bouncingdiscoball"in frame check it out (some new implementation )
    den i will post it on blog

    ReplyDelete
  10. Thanks Deepansh, Its a great program, keep up the good work. The same has been put up on the blog.

    ReplyDelete