Meaningless Drivel is fun!*
The moose likes Swing / AWT / SWT and the fly likes graphics object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "graphics object" Watch "graphics object" New topic
Author

graphics object

venkatesh pendharkar
Ranch Hand

Joined: Apr 29, 2006
Posts: 106
I am trying to use graphics object outside the paint method.For that i am using getGraphics() method.But when i am trying to draw the image using drabImage() method,the image is not getting drawn.This is the code:-

import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class MyImage extends Applet implements KeyListener,Runnable
{
Graphics g1;
int x=10;
int y=10;
Image ir,ib,ig,iy;
public void init()
{
getImages();
addKeyListener(this);
}

public void paint(Graphics g)
{
if (g1==null )
{
g1 = this.getGraphics();//graphics object created
}
g.drawImage(ib,100,100,this);
}

public void getImages()
{
ib=getImage(getDocumentBase(),"blue.gif");
ig=getImage(getDocumentBase(),"green.gif");
ir=getImage(getDocumentBase(),"red.gif");
iy=getImage(getDocumentBase(),"yellow.gif");
}

public void keyPressed(KeyEvent e)
{
if (e.getKeyCode()==37)//when left arrow is pressed
{
System.out.println(e.getKeyCode());
g1.drawImage(ir,50,50,this); //image is drawn
}
}

public void keyReleased(KeyEvent e)
{
}
public void keyTyped(KeyEvent e)
{

}
public void run()
{
}
}


Here i dn understand why image is drawn only if left arrow is pressed twice.If its pressed once then object is not drawn.Please help me in this.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
try this
(you may need to click the applet to give it focus, for the key listener to work)

Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 862
I can't tell if you're trying to use Graphics outside of paint() just for the challenge of it, or if you feel that you must.

If the latter, Mr. Dunn's approach is a much better idea. If the former I'm not sure what to tell you, since I don't know of any way that is guaranteed to work.


bitguru blog
venkatesh pendharkar
Ranch Hand

Joined: Apr 29, 2006
Posts: 106
Thanks Micheal & Brian for your replies.
Micheal I am definately clicking the applet first & then only using left arrow.But still image gets displayed only when arrow is pressed twice & not for the first time.
Brian I am trying to crate a game which is pretty much similar to tetrix.And for that i need to continously drawIamge() outisde paint method.So i will need to use this graphics object not only in keyPressed() method but different other methods & I cant write the code for drawing images only in paint() method.So can anyone suggest me a better way or whats going wrong in my current code.
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Originally posted by venkatesh pendharkar:
Thanks Micheal & Brian for your replies.
Micheal I am definately clicking the applet first & then only using left arrow.But still image gets displayed only when arrow is pressed twice & not for the first time.
Brian I am trying to crate a game which is pretty much similar to tetrix.And for that i need to continously drawIamge() outisde paint method.So i will need to use this graphics object not only in keyPressed() method but different other methods & I cant write the code for drawing images only in paint() method.So can anyone suggest me a better way or whats going wrong in my current code.


Well, the idea is a Game Loop in that you have a loop that runs continually, typically a while loop. In this game loop you would determine all game logic and set up drawing events and then you would have the game loop call the repaint method which will tell the dispatcher that a paint must occur and it will call the paint method for you. Technically, you should never call an objects paint method directly.

This tutorial would server you well to understand the concepts of game logic and how to control such things.


GenRocket - Experts at Building Test Data
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 862
Originally posted by venkatesh pendharkar:
I am trying to crate a game which is pretty much similar to tetrix.And for that i need to continously drawIamge() outisde paint method.


No you don't. What you should do in keyPressed() and those other methods is update the game state (for example, by adding something to the list of objects that are currently appearing on the screen) and call repaint().

What repaint() does is have the system call paint() with an appropriate Graphics object. So your paint() should draw the objects in that list. It should draw not only the new one that was added in keyPressed(), but all the old ones too. This way if the entire game needs to be redrawn (say because another window briefly popped up on top of it) then it will indeed be redrawn correctly.

Note that, depending on the situation, several calls to repaint() may be coalesced into a single call to paint(). This is a good thing.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: graphics object
 
Similar Threads
Game(ball breaker) developed using swings not running.
using graphics object
Beginner game
Please help with this program
Please check if the paint method used properly