putting an image to an application


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: putting an image to an application

  1. #1
    Andew Guest

    putting an image to an application


    Hi I'm new to Java, and I am reading some books and sample codes on how to
    put up an image to an application. However most of this codes illustrate
    using applet and they use the graphics.paint method. How do I do that in
    an application?

  2. #2
    Paul Clapham Guest

    Re: putting an image to an application

    The same way, if you are using the java.awt classes.

    PC2

    Andew <ong_tt@yahoo.com> wrote in message news:3aac9bb4$1@news.devx.com...
    >
    > Hi I'm new to Java, and I am reading some books and sample codes on how to
    > put up an image to an application. However most of this codes illustrate
    > using applet and they use the graphics.paint method. How do I do that in
    > an application?




  3. #3
    andrew Guest

    Re: putting an image to an application


    Hi Paul, I have tried that without any success. I would appreciate very much
    if you can show me an example. Either a post or an email. Thanks in advance.

    "Paul Clapham" <pclapham@core-mark.com> wrote:
    >The same way, if you are using the java.awt classes.
    >
    >PC2
    >



  4. #4
    Paul Clapham Guest

    Re: putting an image to an application

    To tell you the truth, I've been working on an application for over a year
    now, it has close to 300 classes, and I've never used the graphics.paint
    method. So I don't understand what your actual problem is. Applications
    and applets use the identical methods to display images. So that isn't your
    problem. Perhaps you could ask a more specific question?

    PC2

    andrew <ong_tt@yahoo.com> wrote in message news:3aad804c$1@news.devx.com...
    >
    > Hi Paul, I have tried that without any success. I would appreciate very

    much
    > if you can show me an example. Either a post or an email. Thanks in

    advance.
    >
    > "Paul Clapham" <pclapham@core-mark.com> wrote:
    > >The same way, if you are using the java.awt classes.
    > >
    > >PC2
    > >

    >




  5. #5
    Puneet Wadhwa Guest

    Re: putting an image to an application


    Dear Paul,
    Even I have the same problem, about drawing an image on an application (not
    an applet..) Actually, the paint method has a graphics object, so we can
    use it to draw in case of applets..Whereas in case of application we can't
    access methods such as getDocumentBase and getImage from inside the static
    main method..
    I am pasting an example of code where I tried desperately to make this thing
    work, but maximum i could get it to compile..but it throws a NullPointerException
    while running.


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

    public class Notepad extends Applet implements ActionListener, WindowListener
    {
    static Frame f;
    static FileDialog fd,fd1;
    static TextArea ta;
    static Font fn=new Font("Courier",0,14);
    static MenuItem m1,m2,m3,m4,m5,m6,m7,m8,cut,paste,copy,delete,about;
    static Menu m,help;
    static MenuBar mbar;
    static String cuttext="",txtmsg="";
    static int getcaretpos=0,setcaretpos=0;
    static Image img;
    static boolean imgflag=false;
    static Graphics offG;

    /* Notepad(String s)
    {
    super(s);
    }*/

    public void windowOpened(WindowEvent we){}
    public void windowActivated(WindowEvent we){}
    public void windowDeactivated(WindowEvent we){}
    public void windowClosed(WindowEvent we){}
    public void windowIconified(WindowEvent we){}
    public void windowDeiconified(WindowEvent we){}


    public static void main(String args[])
    {
    Notepad f=new Notepad();
    f.setBounds(50,50,300,300);
    f.addWindowListener(f);
    fd=new FileDialog(f,"Open..",FileDialog.LOAD);
    fd1=new FileDialog(f,"Save As..",FileDialog.SAVE);
    mbar=new MenuBar();
    f.setMenuBar(mbar);

    m1=new MenuItem("New");
    m2=new MenuItem("Save..");
    m3=new MenuItem("Open");
    m4=new MenuItem("Print");
    m5=new MenuItem("Exit");
    m5.addActionListener(f);
    m4.addActionListener(f);
    m3.addActionListener(f);
    m2.addActionListener(f);
    m1.addActionListener(f);
    Menu m=new Menu("File");
    help=new Menu("Help");
    about=new MenuItem("About");
    m.add(m1);
    m.add(m2);
    m.add(m3);
    m.add(m4);
    m.addSeparator();
    m.add(m5);

    m6=new MenuItem("Red");
    m7=new MenuItem("Blue");
    m8=new MenuItem("Green");
    m6.addActionListener(f);
    m7.addActionListener(f);
    m8.addActionListener(f);

    Menu edit=new Menu("Edit");
    Menu colors=new Menu("Colors");
    cut=new MenuItem("Cut");
    paste=new MenuItem("Paste");
    copy=new MenuItem("Copy");
    delete=new MenuItem("Delete");

    cut.addActionListener(f);
    paste.addActionListener(f);
    copy.addActionListener(f);
    delete.addActionListener(f);
    about.addActionListener(f);

    colors.add(m6);
    colors.add(m7);
    colors.add(m8);
    edit.add(colors);
    edit.addSeparator();
    edit.add(cut);
    edit.add(copy);
    edit.add(paste);
    edit.add(delete);
    help.add(about);
    mbar.add(m);
    mbar.add(edit);
    mbar.add(help);

    ta=new TextArea(5,4);
    ta.setFont(fn);
    f.add(ta);
    f.setVisible(true);
    }

    public void paint(Graphics g)
    {
    if (imgflag==true)
    {
    g.drawImage(img,0,0,f);
    }
    }

    public void windowClosing(WindowEvent we)
    {
    System.exit(0);
    System.out.println("Exiting..");
    }

    public void actionPerformed(ActionEvent e)
    {
    if (e.getSource()==m5)
    {
    System.exit(0);
    }

    if (e.getSource()==copy)
    {
    cuttext=ta.getSelectedText();
    }

    if (e.getSource()==cut)
    {
    cuttext=ta.getSelectedText();
    int cutlen=cuttext.length();
    }

    if (e.getSource()==paste)
    {
    getcaretpos=ta.getCaretPosition();
    ta.insertText(cuttext,getcaretpos);
    }

    if (e.getSource()==about)
    {
    String imgname="mynotepad.gif";
    offG=f.getGraphics();
    try
    {
    img=getImage(showDocumentBase(),imgname);
    offG.drawImage(img);
    }
    catch(Exception ure)
    {System.out.println("Exception caught");}

    imgflag=true;
    repaint();
    }
    if (e.getSource()==m1)
    {
    ta.setText("");
    }

    if (e.getSource()==m3)
    {
    txtmsg="";
    fd.setVisible(true);
    String filename=fd.getFile();
    String dirname=fd.getDirectory();
    File openfile=new File(dirname,filename);

    try
    {
    FileInputStream fis=new FileInputStream(openfile);
    int bytelength=fis.available();
    for (int bytecount=0;bytecount<bytelength;bytecount++)
    {
    char fch=(char)fis.read();
    txtmsg=txtmsg+fch;
    }
    ta.setText(txtmsg);
    }
    catch(Exception ioe)
    {
    System.out.println("An exception has occured");
    }
    }
    if (e.getSource()==m6)
    {
    ta.setBackground(new Color(255,204,255));
    ta.setForeground(new Color(0,0,102));
    repaint();
    }
    if (e.getSource()==m7)
    {
    ta.setBackground(new Color(153,204,255));
    ta.setForeground(Color.black);
    repaint();
    }
    if (e.getSource()==m8)
    {
    ta.setBackground(new Color(204,255,204));
    ta.setForeground(Color.black);
    repaint();
    }
    if (e.getSource()==m2)
    {
    fd1.setVisible(true);
    }
    }
    }



    "Paul Clapham" <pclapham@core-mark.com> wrote:
    >To tell you the truth, I've been working on an application for over a year
    >now, it has close to 300 classes, and I've never used the graphics.paint
    >method. So I don't understand what your actual problem is. Applications
    >and applets use the identical methods to display images. So that isn't

    your
    >problem. Perhaps you could ask a more specific question?
    >
    >PC2
    >
    >andrew <ong_tt@yahoo.com> wrote in message news:3aad804c$1@news.devx.com...
    >>
    >> Hi Paul, I have tried that without any success. I would appreciate very

    >much
    >> if you can show me an example. Either a post or an email. Thanks in

    >advance.
    >>
    >> "Paul Clapham" <pclapham@core-mark.com> wrote:
    >> >The same way, if you are using the java.awt classes.
    >> >
    >> >PC2
    >> >

    >>

    >
    >



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center