Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

using graphics object

 
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi i am trying to create a game in which i need to use drawImage of graphics method outside the paint method.I am trying it in the following way.The code is complilling fine & also not giving any exception but it doesn draw any image.Can anyone help me out??? I am sending some part of this code.

import java.awt.*;
import java.applet.*;
public class MyImage extends Applet
{
Dimension d;
Graphics g1;// this is a variable of graphics object
int x=10;
int y=10;
Image ir,ib,ig,iy,i1;

public void init()
{
getImages();
d = getSize();
setBackground(Color.BLACK);
addKeyListener(this);
}

public void paint(Graphics g)
{
if (g1==null && d.width>0 && d.height>0)
{
i1 = createImage(d.width, d.height); // creating the graphics objetct
g1 = i1.getGraphics();
}
if (g1==null || i1==null)
return;
g.drawImage(ib,100,100,this);
}

public void met()
{
g1.drawImage(ir,50,50,this); //drawing an image with g1(graphics object)
}

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

}
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it absolutely required that your code be exactly similar to the way you have it written?
Also, is this for an applet or not?
 
Those cherries would go best on cherry cheesecake. Don't put those cherries on this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic