Drawing Image  
Author Message
asksumit@gmail.com





PostPosted: 2006-4-3 21:32:00 Top

java-programmer, Drawing Image Hi,
I am developing a login application in which i need to have an image
put on to a JFrame which has several internal frames. (The main frame)
when I use the graphics object, it draws up the image and it disappears
in a few seconds.
I tried to change the background color, but i see only a flash of the
color and it disappears.
It would be extremely helpful is someone could sort this problem for
me.

Thank you,
Sumit
import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.awt.event.*;
import javax.imageio.*;
import java.awt.Graphics.*;
public class Login extends JPanel implements KeyListener ,
ActionListener
{
JFrame mainFrame;
JDesktopPane desktop, background;
JInternalFrame user;

JButton ok, cancel;
JTextField userName;
TextField pass;
JLabel lname,lpass,message,ms;
JMenuBar mb;
JMenu start,view,options,insert,help;
JMenuItem ocm,ccm,print,exit,about;
Boolean connectionManagerStatus;
Image img;
Login()
{
/*
* Basic Theme
*/

try
{
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
//UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
}
catch (Exception e)
{
System.out.println(e.toString());
}

/*
* Initializations and addition to the program
*/
mb = new JMenuBar();

desktop = new JDesktopPane();
background = new JDesktopPane();
mainFrame = new JFrame (".:: I . C . E ::. Application");
mainFrame.setJMenuBar(mb);
start = new JMenu ("Start");
view = new JMenu ("View");
options = new JMenu ("Options");
insert = new JMenu ("Insert");
help=new JMenu("help");
start.setMnemonic(KeyEvent.VK_S);
view.setMnemonic(KeyEvent.VK_V);
options.setMnemonic(KeyEvent.VK_O);
insert.setMnemonic(KeyEvent.VK_I);
help.setMnemonic(KeyEvent.VK_H);
mb.add(start);
mb.add(view);
mb.add(options);
mb.add(insert);
mb.add(help);


background.setVisible(true);
background.setSize(640,640);

ocm=new JMenuItem("Open Connection Manager",KeyEvent.VK_O);
ccm = new JMenuItem("Close Connection",KeyEvent.VK_C);
print = new JMenuItem("Print",KeyEvent.VK_P);
exit = new JMenuItem("Exit",KeyEvent.VK_X);
about = new JMenuItem("About Us",KeyEvent.VK_A);



start.add(ocm);
start.add(ccm);
start.addSeparator();
start.add(print);
start.add(exit);
help.add(about);
user = new JInternalFrame("Connection Manager");
lname = new JLabel("USERNAME:");
userName = new JTextField (50);
lpass = new JLabel("PASSWORD:");
pass = new TextField(50);
pass.setEchoChar('*');


ms = new JLabel ("Message From Server: ");
message = new JLabel("3 Wrong attempts and account freezes");
ok = new JButton ("Login");
cancel = new JButton ("Cancel");
ok.setBounds(20,180,100,20);
cancel.setBounds(130,180,100,20);
mainFrame.setUndecorated(false);
mainFrame.add(desktop);

mainFrame.setFocusable(true);
options.setEnabled(false);
insert.setEnabled(false);
view.setEnabled(false);
print.setEnabled(false);
ocm.setEnabled(false);
ccm.setEnabled(false);
connectionManagerStatus=true;

mainFrame.setSize(Toolkit.getDefaultToolkit().getScreenSize());

desktop.setSize(640,480);
lname.setBounds(10,40,70,20);
userName.setBounds(100,40,130,20);
lpass.setBounds(10,100,100,20);
pass.setBounds(100,100,130,20);
ms.setBounds(15,270,140,20);
message.setBounds(10,320,240,20);
message.setForeground(Color.darkGray);
user.setLayout(null);
user.getContentPane().add(lname);
user.getContentPane().add(userName);
user.getContentPane().add(lpass);
user.getContentPane().add(pass);
user.getContentPane().add(ok);
user.getContentPane().add(cancel);
user.getContentPane().add(ms);
user.getContentPane().add(message);
user.setResizable(false);
//JLabel s = new JLabel("XXXXXXXXXXXXXXx");


ok.setEnabled(false);
user.setVisible(true);
user.setSize(340,480);
desktop.setLocation(30,200);
desktop.add(user);
user.setLocation(30,200);

//user.setBorder(BorderFactory.createRaisedBevelBorder());

// adding listeners

userName.addKeyListener(this);
exit.addActionListener(this);

cancel.addActionListener(this);
ocm.addActionListener(this);
pass.addKeyListener(this);
ok.addActionListener(this);
mainFrame.setVisible(true);


mainFrame.validate();
l();
}

public void actionPerformed (ActionEvent ae)
{
if ((ae.getSource())==exit)
{
System.exit(0);
}
else if ((ae.getSource())==cancel)
{
user.setVisible(false);
userName.setText(null);
pass.setText(null);
ocm.setEnabled(true);

}
else if ((ae.getSource())==ocm)
{
user.setVisible(true);
ocm.setEnabled(false);
}
else if ((ae.getSource())==ok)
{
user.setVisible(false);
ocm.setEnabled(false);
ccm.setEnabled(true);
System.out.println(pass.getText());
}
}
public void keyPressed (KeyEvent ke)
{
if ((ke.getComponent())==userName)
{
if (userName.getText()!=null)
{
ok.setEnabled(true);

}
else
{
ok.setEnabled(false);
}

}
else if ((ke.getComponent())==pass)
{


}

}
public void keyReleased (KeyEvent ke1)
{

}
public void keyTyped (KeyEvent ke2)
{

}
public static void main (String args [])
{
Login obj = new Login ();
}
public void l()
{
Graphics g = mainFrame.getGraphics();


try
{

img = Toolkit.getDefaultToolkit().getImage("d:\\loge.bmp");




g.drawImage(img,1024,800,null);




System.out.println("Image drawn");
}
catch (Exception e)
{
System.out.println(e.toString());
}
}

}

 
Knute Johnson





PostPosted: 2006-4-4 0:58:00 Top

java-programmer >> Drawing Image email***@***.com wrote:
> Hi,
> I am developing a login application in which i need to have an image
> put on to a JFrame which has several internal frames. (The main frame)
> when I use the graphics object, it draws up the image and it disappears
> in a few seconds.
> I tried to change the background color, but i see only a flash of the
> color and it disappears.
> It would be extremely helpful is someone could sort this problem for
> me.
>
> Thank you,
> Sumit
> --

Sumit:

With rare exception all drawing needs to be done in the paintComponent()
method of Swing components. Just extend your component and override the
paintComponent() method and draw your image. The example below uses the
ImageObserver pattern. Unless you are using a compiler before 1.4
(which you shouldn't be) you should use the javax.imageio class for
image input and output.

You will be more likely to get help if you can write a small, easily
followed example program, to explain your problem. It also helps you
identify where the problem is better.

And remember all Swing GUIs need to be created on the Event Dispatch
Thread as in my example below.

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

public class test extends JPanel {
Image image;

public test() {
image = Toolkit.getDefaultToolkit().getImage("saturn.jpg");
setPreferredSize(new Dimension(800,600));
}

public void paintComponent(Graphics g) {
g.drawImage(image,0,0,getWidth(),getHeight(),this);
}

public static void main(String[] args) {
Runnable r = new Runnable() {
public void run() {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
test t = new test();
f.add(t);
f.pack();
f.setVisible(true);
}
};
EventQueue.invokeLater(r);
}
}

--

Knute Johnson
email s/nospam/knute/
 
Roedy Green





PostPosted: 2006-4-4 1:01:00 Top

java-programmer >> Drawing Image On 3 Apr 2006 06:32:29 -0700, "email***@***.com"
<email***@***.com> wrote, quoted or indirectly quoted someone who
said :

>I tried to change the background color, but i see only a flash of the
>color and it disappears.

see http://mindprod.com/jgloss/jframe.html
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.