Image in a Graphics object

Hi, I'm dealing with the Batik API for working with svg images, but I need to do some wmf imager representation too.
This API offers a class which is able to read WMF files, giving a "WMFRecordStore", that you can use to initialize a "WMFPainter".
Anyway, this WMFPainter class has a "paint" method which I hoped that I could use to print the wmf info in the screen but... I don't know if it can be done.
You must pass a Graphics object to the "paint" method.
If I want to add an image to a JLabel, as I'd do with an ImageIcon, do I have to get the current JLabel Graphics object? Will the image keep displayed when repainting? Is this a correct way of doing this?
thanks a lot

if you have to paint special things on a component, you have to subclass the component and override the paint (or paintComponent) method. You don't want to be getting the graphics object from a component and draw on it, it's not going to work properly.

Similar Messages

  • Getting the image of a graphics object?

    Hi,
    I have a graphics object that I want to update on a thread, but i don't want to redraw everything I had on each time I update (I only want to draw new items). in my paintComponent function, I have
    public void paintComponent(Graphics g)
              Graphics2D g2 = (Graphics2D)g;
              if (bufferedImage == null)
                   bufferedImage = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_RGB);
              g2.drawImage(bufferedImage, 0, 0, null);
              drawBackground(g2);
              addItem(g2);
                 g2_ = g2;
         }where drawBackground makes the constant background and addItem draws new points. The problem that I'm having is that it is only drawing new items on the graph and not adding on to the existing image. I think this is because I never set bufferedImage to the current image, but I'm not sure how to do this?
    Thanks

    demo:
    import java.awt.*;
    import java.awt.image.*;
    import javax.swing.*;
    @SuppressWarnings("serial") public class Example extends JComponent {
        private BufferedImage buffer;
            setOpaque(true);
        @Override protected void paintComponent(Graphics g) {
            int w = getWidth(), h = getHeight();
            if (buffer == null ||
                w > buffer.getWidth() || h > buffer.getHeight()) {
                buffer = getGraphicsConfiguration().createCompatibleImage(w, h);
                composeBuffer();
            g.drawImage(buffer, 0, 0, null);
        protected void composeBuffer() {
            Graphics2D g2 = buffer.createGraphics();
            g2.setColor(Color.RED);
            g2.drawLine(0, 0, buffer.getWidth(), buffer.getHeight());
            g2.dispose();
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable(){
                public void run() {
                    JComponent comp = new Example();
                    comp.setPreferredSize(new Dimension(400,300));
                    JFrame f = new JFrame();
                    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    f.add(comp);
                    f.pack();
                    f.setLocationRelativeTo(null);
                    f.setVisible(true);
    }

  • Graphics object with transparent background

    Ive been trying to create an image using a graphics object, which i want to have a transparent background. I have been researching on the net and have come up with something like this, but its not really getting the job done. Im still getting an image with a black background. The image will always be 200 by 100 pixels.
    public void drawGraph(Graphics2D g,int a,int b, int d)
             Composite oldComp = g.getComposite();
             Composite alphaComp = AlphaComposite.getInstance( AlphaComposite.CLEAR);
             g.setComposite(alphaComp);
             g.fillRect(0,0,200,100);
             g.setComposite(oldComp);
    <<OTHER STUFF DRAWN HERE>>
          }Any tips would be greatly appreciated thanks.

    Create a BufferedImage like this:
    int width = 200;
    int height = 200;
    BufferedImage yourImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);

  • How to convert a Graphics object into a JPG image?

    I have this simple question about how to make a Graphics object into a JPG file...
    I just need the names of the classes or the packages ... I'll figure out the rest of the details...
    Thanks...

    Anyway this might come in handy
    JPEGUtils.java
    ============
    * Created on Jun 22, 2005 by @author Tom Jacobs
    package tjacobs.jpeg;
    import java.awt.Image;
    import java.awt.image.BufferedImage;
    import java.awt.image.RenderedImage;
    import java.io.BufferedInputStream;
    import java.io.BufferedOutputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import javax.imageio.ImageIO;
    import com.sun.image.codec.jpeg.JPEGCodec;
    import com.sun.image.codec.jpeg.JPEGEncodeParam;
    import com.sun.image.codec.jpeg.JPEGImageDecoder;
    import com.sun.image.codec.jpeg.JPEGImageEncoder;
    * Utilities for saving JPEGs and GIFs
    public class JPEGUtils {
         private JPEGUtils() {
              super();
         public static void saveJPEG(BufferedImage thumbImage, File file, double compression) throws IOException {
              BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(file));
              saveJPEG(thumbImage, out, compression);
              out.flush();
              out.close();
         public static void saveJPEG(BufferedImage thumbImage, OutputStream out, double compression) throws IOException {
              JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
              JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(thumbImage);
              compression = Math.max(0, Math.min(compression, 100));
              param.setQuality((float)compression / 100.0f, false);
              encoder.setJPEGEncodeParam(param);
              encoder.encode(thumbImage);
         public static BufferedImage getJPEG(InputStream in) throws IOException {
              try {
                   JPEGImageDecoder decode = JPEGCodec.createJPEGDecoder(in);
                   BufferedImage im = decode.decodeAsBufferedImage();
                   return im;
              } finally {
                   in.close();
         public static BufferedImage getJPEG(File f) throws IOException {
              InputStream in = new BufferedInputStream(new FileInputStream(f));
              return getJPEG(in);
         public static void saveGif(RenderedImage image, File f) throws IOException {
              saveGif(image, new FileOutputStream(f));
         public static void saveGif(RenderedImage image, OutputStream out) throws IOException {
    //          Last time I checked, the J2SE 1.4.2 shipped with readers for gif, jpeg and png, but writers only for jpeg and png. If you haven't downloaded the whole JAI, and you want a writer for gif (as well as readers and writers for several other formats) download:
    //          http://java.sun.com/products/java-media/jai/downloads/download-iio.html
              ImageIO.write(image, "gif", out);
         public static void main(String[] args) {
              // TODO Auto-generated method stub
    }

  • Draw to an image using graphics object

    ive been trying to this. create a starfield 800px wide and twice the height of the screen 2x600. this image would be scrolling down the screen giving the appearance of moving through space. what i did was create an image the called "starField", and created a graphics object "starFieldObj" to draw dots (stars) to this image.
    is this the correct way to do this? my program seems to work differently depending on where i call "generateStarField()" which draws to "starFieldObj"
    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
    public class Alpha1 extends Canvas implements Runnable, MouseMotionListener, MouseListener, KeyListener {
         private Image buffer; //bufer image
         private Graphics gfx;
         private Graphics starFieldObj;
         private Image playerShip;
         private int frames = 60; //frames per second
         private MediaTracker mediaTracker; //tracks loaded images
         private boolean ready; //ready to  start animation
         private int [] keys = new int[256];
         private int playerShipX = 100;
         private int playerShipY = 100;
         private static Random random = new Random();
         private Image starField;
         private int starFieldSpeed = 5;
         private int starFieldX = 0;
         private int starFieldY = -600;
         public static void main(String[] args)
              Alpha1 t = new Alpha1();
              Frame f = new Frame("blah");
              f.setSize(800, 600);
              f.add(t, BorderLayout.CENTER);
              f.show();
              t.start();
              t.requestFocus();
              f.addWindowListener(new WindowAdapter(){
                   public void windowClosing(WindowEvent e){
                        System.exit(0);}});
         public Alpha1()
              addKeyListener(this);
              addMouseListener(this);
              ready = false;
              mediaTracker = new MediaTracker(this);
              playerShip = getToolkit().getImage("testship1.png");
              //starField = getToolkit().getImage("testbg.png");
              //mediaTracker.addImage(playerShip, 0);
              mediaTracker.addImage(starField, 0);
              try {
                   mediaTracker.waitForID(0);
              catch (InterruptedException e) {
                   e.printStackTrace();
         public void start()
              buffer = createImage(getSize().width,getSize().height);
              //starField = createImage(getSize().width,getSize().height * 2);
              gfx = buffer.getGraphics();
              //starFieldObj = starField.getGraphics();
              generateStarfield();
              ready = true;
              new Thread(this).start();
         public void mouseDragged(MouseEvent e)     {}
         public void mouseMoved(MouseEvent e)     
              playerShipX = e.getX();
              playerShipY = e.getY();
         public void mouseReleased(MouseEvent e)     {}
         public void mousePressed(MouseEvent e)     {}
         public void mouseExited(MouseEvent e)     {}
         public void mouseEntered(MouseEvent e)  {}
         public void mouseClicked(MouseEvent e)     {}
         public void keyTyped(KeyEvent keyevent)     {}
         public void keyPressed(KeyEvent keyevent)
              keys[keyevent.getKeyCode()] = 1;
              System.out.println(keyevent.getKeyCode());
         public void keyReleased(KeyEvent keyevent)     
              keys[keyevent.getKeyCode()] = 0;
         public void run()
              while (true)
                   if (isVisible())
                        repaint();
                        updatePositions();
                   }//end if
                   try 
                   { Thread.sleep(1000/frames); }
                   catch (InterruptedException e)
                   { e.printStackTrace(); }
              }//end while
         public void paint(Graphics g) {     /*empty etc*/ }
         public void updatePositions()
              if (keys[38] == 1 & keys[40] == 0) //IF UP IS PRESSED
              { playerShipY -= 3; }
              if (keys[40] == 1 & keys[38] == 0) //IF DOWN IS PRESSED
              { playerShipY += 6; }
              if (keys[37] == 1 & keys[39] == 0) //IF LEFT IS PRESSED
              { playerShipX -= 5; }
              if (keys[39] == 1 & keys[37] == 0) //IF RIGHT IS PRESSED
              { playerShipX += 5; }
              //starFieldY = starFieldY + starFieldSpeed;
              //if (starFieldY == 0)
              //     starFieldY = -600;
              //     System.out.println("dadssa");
         public int getRandom(int from, int to)
              if (from > to)
                   int randTemp;
                   randTemp = from;
                   from = to;
                   to = randTemp;
              return random.nextInt(to-from+1)+from;
         public void generateStarfield()
              starFieldObj.setColor(Color.black);
              starFieldObj.fillRect (0, 0, 800, 600 * 2);
              for (int td=0; td < 900 ; td++) //draw 900x2 dots on 800x1200 image
                        starFieldObj.fillRect (getRandom(1,800), getRandom(1,600), 1, 1);
                        starFieldObj.fillRect (getRandom(1,800) + 800, getRandom(1,600) + 800, 1, 1);
         public void update(Graphics g)
              if (ready)
                   gfx.setColor(Color.black);
                   gfx.fillRect (0, 0, this.getSize().width, this.getSize().height);
                   //gfx.drawImage(starField,starFieldX,starFieldY,null);
                   gfx.drawImage(playerShip,playerShipX,playerShipY,null);
                   //gfx.drawString(Integer.toString(showFps),5,5);
                   g.drawImage(buffer,0,0,null);
    }

    updated code, i cant get starField to be 1200px in height
    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
    public class Alpha1 extends Canvas implements Runnable, MouseMotionListener, MouseListener, KeyListener {
         private Image buffer; //bufer image
         private Graphics gfx;
         private Graphics starFieldObj;
         private Image playerShip;
         private int frames = 60; //frames per second
         private MediaTracker mediaTracker; //tracks loaded images
         private boolean ready; //ready to  start animation
         private int [] keys = new int[256];
         private int playerShipX = 100;
         private int playerShipY = 100;
         private static Random random = new Random();
         private Image starField;
         private int starFieldSpeed = 5;
         private int starFieldX = 0;
         private int starFieldY = -600;
         public static void main(String[] args)
              Alpha1 t = new Alpha1();
              Frame f = new Frame("blah");
              f.setSize(800, 600);
              f.add(t, BorderLayout.CENTER);
              f.show();
              t.start();
              t.requestFocus();
              f.addWindowListener(new WindowAdapter(){
                   public void windowClosing(WindowEvent e){
                        System.exit(0);}});
         public Alpha1()
              addKeyListener(this);
              addMouseListener(this);
              ready = false;
              mediaTracker = new MediaTracker(this);
              playerShip = getToolkit().getImage("testship1.png");
              //starField = getToolkit().getImage("testbg.png");
              //mediaTracker.addImage(playerShip, 0);
              mediaTracker.addImage(starField, 0);
              try {
                   mediaTracker.waitForID(0);
              catch (InterruptedException e) {
                   e.printStackTrace();
         public void start()
              buffer = createImage(getSize().width,getSize().height);
              starField = createImage(getSize().width,1200);
              gfx = buffer.getGraphics();
              starFieldObj = starField.getGraphics();
              ready = true;
              new Thread(this).start();
         public void mouseDragged(MouseEvent e)     {}
         public void mouseMoved(MouseEvent e)     
              playerShipX = e.getX();
              playerShipY = e.getY();
         public void mouseReleased(MouseEvent e)     {}
         public void mousePressed(MouseEvent e)     {}
         public void mouseExited(MouseEvent e)     {}
         public void mouseEntered(MouseEvent e)  {}
         public void mouseClicked(MouseEvent e)     {}
         public void keyTyped(KeyEvent keyevent)     {}
         public void keyPressed(KeyEvent keyevent)
              keys[keyevent.getKeyCode()] = 1;
              System.out.println(keyevent.getKeyCode());
         public void keyReleased(KeyEvent keyevent)     
              keys[keyevent.getKeyCode()] = 0;
         public void run()
              generateStarfield();
              while (true)
                   if (isVisible())
                        repaint();
                        updatePositions();
                   }//end if
                   try 
                   { Thread.sleep(1000/frames); }
                   catch (InterruptedException e)
                   { e.printStackTrace(); }
              }//end while
         public void paint(Graphics g) {     /*empty etc*/ }
         public void updatePositions()
              if (keys[38] == 1 & keys[40] == 0) //IF UP IS PRESSED
              { playerShipY -= 3; }
              if (keys[40] == 1 & keys[38] == 0) //IF DOWN IS PRESSED
              { playerShipY += 6; }
              if (keys[37] == 1 & keys[39] == 0) //IF LEFT IS PRESSED
              { playerShipX -= 5; }
              if (keys[39] == 1 & keys[37] == 0) //IF RIGHT IS PRESSED
              { playerShipX += 5; }
              starFieldY = starFieldY + starFieldSpeed;
              if (starFieldY == 0)
                   starFieldY = -600;
                   System.out.println("dadssa");
         public int getRandom(int from, int to)
              if (from > to)
                   int randTemp;
                   randTemp = from;
                   from = to;
                   to = randTemp;
              return random.nextInt(to-from+1)+from;
         public void generateStarfield()
              starFieldObj.setColor(Color.black);
              starFieldObj.fillRect (0, 0, 800, 1200);
              for (int td=0; td < 900 ; td++) //draw 900x2 dots on 800x1200 image
                        starFieldObj.setColor(Color.white);
                        starFieldObj.fillRect (getRandom(1,800), getRandom(1,600), 1, 1);
                        starFieldObj.fillRect (getRandom(1,800) + 800, getRandom(1,600) + 800, 1, 1);
         public void update(Graphics g)
              if (ready)
                   gfx.setColor(Color.black);
                   //gfx.fillRect (0, 0, this.getSize().width, this.getSize().height);
                   gfx.drawImage(starField,starFieldX,starFieldY,null);
                   gfx.drawImage(playerShip,playerShipX,playerShipY,null);
                   //gfx.drawString(Integer.toString(showFps),5,5);
                   g.drawImage(buffer,0,0,null);
    }

  • Best way to draw thousands of graphic objects on the screen

    Hello everybody, I'm wanting to develop a game where at times thousands of graphic objects are displayed on-screen. What better way to do this in terms of performance and speed?
    I have some options below. Do not know if the best way is included in these options.
    1 - Each graphical object is displayed on a MovieClip or Sprite.
    2 - There is a Bitmap that represents the game screen. All graphical objects that are displayed on screen have your images stored in BitmapData. Then the Bitmap that represents the game screen copies for themselves the BitmapData of graphical objects to be screened, using for this bitmapData.copyPixels (...) or BitmapData.draw  (...). The Bitmap that represents the screen is added to the stage via addChild (...).
    3 - The graphical objects that are displayed on screen will have their images drawn directly on stage or in a MovieClip/Sprite added to this stage by addChild (...). These objects are drawn using the methods of the Graphics class, as beginBitmapFill and beginFill.
    Recalling that the best way probably is not one of these 3 above.
    I really need this information to proceed with the creation of my game.
    Please do not be bothered with my English because I'm using Google translator.
    Thank you in advance any help.

    Thanks for the information kglad. =)
    Yes, my game will have many objects similar in appearance.
    Some other objects will use the same image stored, just in time to render these objects is that some effects (such as changing the colors) will be applied in different ways. But the picture for them all is the same.
    Using the second option, ie, BitmapDatas, which of these two methods would be more efficient? copyPixels or draw?
    Thank you in advance any help. =D

  • How to use a Graphics Object in a JSP?

    Hello, I do not know if this is a good or a silly question. Can anyone tell me if we can use a Graphics object in a JSP. For example to draw a line or other graphics, i am planning to use the JSP. Any help is much appreciated.
    Regards,
    Navin Pathuru.

    Hi Rob or Jennifer, could you pour some light here.
    I have not done a lot of research for this, but what i want to do is below the polygon i would like to display another image object like a chart... is it possible? If so how to do it? Any help is much appreciated.
    here is the code:
    <%
    // Create image
    int width=200, height=200;
    BufferedImage image = new BufferedImage(width,
    height, BufferedImage.TYPE_INT_RGB);
    // Get drawing context
    Graphics g = image.getGraphics();
    // Fill background
    g.setColor(Color.white);
    g.fillRect(0, 0, width, height);
    // Create random polygon
    Polygon poly = new Polygon();
    Random random = new Random();
    for (int i=0; i < 5; i++) {
    poly.addPoint(random.nextInt(width),
    random.nextInt(height));
    // Fill polygon
    g.setColor(Color.cyan);
    g.fillPolygon(poly);
    // Dispose context
    g.dispose();
    // Send back image
    ServletOutputStream sos = response.getOutputStream();
    JPEGImageEncoder encoder =
    JPEGCodec.createJPEGEncoder(sos);
    encoder.encode(image);
    %>
    Regards,
    Navin Pathuru

  • Need a Graphic Object for printing

    Hi,
    I generate a report with data from a database.
    At the moment I have an object that implements printable.
    Everytime the print method is called the report has
    to be created new.
    Now I want a seperate Graphics2D object in which teh report
    is created for once. And when a print
    job is started I want to copy it to the Graphics object
    in the printmethod.
    How can I create a Graphics2D Object for my own ?
    Thanx Stephan

    try:
    BufferedImage bufferedImage = new BufferedImage(500, 200, BufferedImage.TYPE_INT_RGB);
    Graphics2D g = (Graphics2D) bufferedImage.getGraphics();
    g.drawString("Go to town!", 10, 10);This is how I did it when I needed to create images in a servlet. I expect it would also work for printing purposes... I guess I'll find out soon enough. :)

  • I cant get it to work! NullPointerExeption at first awt.Graphics Object

    I am writing a game with AWT having it as an applet. I intend NOT to use anything outside of Java 1.1
    For my Game i thought that i ATLEAST need the following classes:
    MainGame, Level, Unit.
    I Started writing Level. wrote a fewe methods for it such as
    drawGras(int grastype) And started to write MainGame to see if they would work together. it's is/is going to be my first Java application where i actually know what object orienting is... I have already wrote a game in Java but with out knowing what a class or what object orienting realy is(the game works rather good to) but this time i wanted to use object orienting but came up with problems =( if first posted in "New to Javaprogramming" but they couldn't help me and advice me here
    My previous post: http://forum.java.sun.com/thread.jsp?forum=54&thread=290558
    Here's my code:
    import java.awt.*;
    public class MainGame extends java.applet.Applet
         implements Runnable {
         Thread runner;
         Image ImgLevel, ImgUnit;
         public MainGame() {
              Level nr1 = new Level();
              nr1.init();
              nr1.setGround(300);
              nr1.drawGras(1);
              nr1.CalcGraphics();
              nr1.drawSky(1);
              Image ImgLevel = nr1.getLevelImage();
         public void paint(Graphics g) {
              g.drawImage(ImgLevel,0,0,this);
         public void update(Graphics g) {
              paint(g);
         public void init() {
              MainGame Game = new MainGame();
         public void stop() {
              runner = null;
              System.exit(1);
         public void start() {
              if(runner == null) {
                   runner = new Thread(this);
                   runner.start();
         void Pause(int time) {
             try {
                 Thread.sleep(time);
             } catch(InterruptedException e) {
               System.out.println(e.toString());
         public void run() {
              Thread thisThread = Thread.currentThread();
                   while(thisThread == runner) {
    class Level extends java.applet.Applet {
        Font BigText = new Font("Arial", Font.PLAIN, 36);
        Font Normal = new Font("Arial", Font.PLAIN, 14);
        Font Bold = new Font("Arial", Font.BOLD, 14);
        Graphics GrLevel;
        int pattern, sky, GroundtoWalk, Ground;
        Color BgColor = new Color(166,202,240);
        Color gras1 = new Color(0,255,0);
        Color gras2 = new Color(0,200,0);
        Color gras3 = new Color(0,100,0);
        Image ImgLevel;
        void setBgColor(int r, int g, int b) {
            BgColor = new Color(r,g,b);
        void CalcGraphics() {
            ImgLevel = createImage(800,600);
            GrLevel = ImgLevel.getGraphics();
        void setGround(int GrounD) {
            Ground = GrounD;
        void GroundToWalkADD(int addToGround) {
            GroundtoWalk = Ground + addToGround;
        void setGroundToWalk(int GrToWalk) {
            GroundtoWalk = GrToWalk;
        public void init() {
              System.out.println("Init 1"); // This was placed here when i was checking how far it got when debuging
              CalcGraphics();
              System.out.println("Init 2");
              drawGras(1);
              System.out.println("Init 3");
              repaint();
              System.out.println("Init 4");
        Image getLevelImage() {
            return ImgLevel;
        public void paint(Graphics g) {
              g.drawImage(ImgLevel,0,0,this);
        public void update(Graphics g) {
            paint(g);
        void drawGras(int pt) {
         GrLevel.setColor(BgColor);
         GrLevel.fillRect(0,0,800,600);
         switch(pt) {
              case 1:
                   GrLevel.setColor(gras1);
                   GrLevel.fillRect(0,GroundtoWalk,800,300);
                   GrLevel.setClip(0,GroundtoWalk,800,300);
                   for(int i = 0; 890>i;i=i+5) {
                        GrLevel.setColor(gras2);
                        GrLevel.drawLine(0+i,GroundtoWalk,-190+i,GroundtoWalk+300);
                   for(int i=950; 0<i; i=i-5) {
                        GrLevel.setColor(gras3);
                        GrLevel.drawLine(890-i,GroundtoWalk,1000-i,GroundtoWalk+300);
              break;
              case 2:
                   GrLevel.setColor(gras2);
                   GrLevel.fillRect(0,GroundtoWalk+5,800,300);
                   for(int i = 0; 890>i;i=i+5) {
                        GrLevel.setColor(gras3);
                        GrLevel.drawLine(0+i,GroundtoWalk,-190+i,GroundtoWalk+300);
                   for(int i=950; 0<i; i=i-5) {
                        GrLevel.setColor(gras1);
                        GrLevel.drawLine(890-i,GroundtoWalk,1000-i,GroundtoWalk+300);
              break;
              case 3:
                   GrLevel.setColor(gras3);
                   GrLevel.fillRect(0,GroundtoWalk+10,800,300);
                   for(int i = 0; 800>i; i=i+20) {
                        GrLevel.fillArc(0+i,GroundtoWalk,20,20,180,-180);
              break;
         pattern = pt;
        int getGrasPattern() {
             return pattern;
        void drawSky(int cl) {
                GrLevel.setColor(BgColor);
                GrLevel.fillRect(0,0,800,155);
                switch(cl) {
                    case 1:
                   for(int y = 0;y<7;y++) {
                   for(int x = 0;x<800;x=x+20) {
                                GrLevel.setColor(Color.white);
                                GrLevel.drawArc(x,y*20,20,20,180,180);
                                GrLevel.setColor(Color.red);
                                GrLevel.drawArc(x,y*20+4,20,20,180,180);
                                GrLevel.setColor(Color.yellow);
                                GrLevel.drawArc(x,y*20+2,20,20,180,180);
                                GrLevel.setColor(Color.green);
                                GrLevel.drawArc(x,y*20+8,20,20,180,180);
                                GrLevel.setColor(Color.blue);
                                GrLevel.drawArc(x,y*20+12,20,20,180,180);
                                GrLevel.setColor(Color.pink);
                                GrLevel.drawArc(x,y*20+16,20,20,180,180);
              break;
              case 2:
                        GrLevel.setColor(Color.gray);
              break;
         sky = cl;
         GrLevel.setFont(BigText);
         GrLevel.setColor(Color.black);
         GrLevel.fillRect(0,0,800,50);
         GrLevel.setColor(Color.white);
         GrLevel.drawString("Score Tabel - comming someday",((size().width)/2)-260, 40);
        int getSkyTyp() {
             return sky;
    }I tested running the code of class Level by puting it into Level.java and compiled it...ran it with appletviewer and it worked as it was intended to.
    But when i compiled it with MainGame... and in the HTML started MainGame.class,.....applet didn't intializeand i got the following error messege:
    Init 1
    java.lang.NullPointerException
            at Level.CalcGraphics(MainGame.java:84)
            at MainGame.<init>(MainGame.java:11)Line 82: void CalcGraphics() {
    Line 83: ImgLevel = createImage(800,600);
    Line 84: GrLevel = ImgLevel.getGraphics(); // The First awt.Graphics object
    Line 85: }
    Line 9:      public MainGame() {
    Line 10:     Level nr1 = new Level();
    Line 11:     nr1.CalcGraphics(); // calling the method above -> same error first awt.Graphics Object.
    If i change it os that "drawGras" comes first it's first awt.Graphics Line will show the same error
    Hope some one can and will help me

    As per the documentation of
    java.awt.Component.createImage(int,int):
    Creates an off-screen drawable image to be used for
    double buffering.
    Parameters:
    width - the specified width
    height - the specified height
    Returns:
    an off-screen drawable image, which can be used for
    double buffering. The return value may be null if the
    component is not displayable. This will always happen
    if GraphicsEnvironment.isHeadless() returns true.
    Since:
    JDK1.0 its not
    ImgLevel = createImage(800,600); that's making the problem but The first awt.Graphics Object in class Level, no mather which on it is =((
    >
    The key part is '...may be null if the component is
    not displayable.'
    You should put your graphics initialization code in
    start(), which will be called when your applet is
    ready to go. Before then, your applet is not
    displayable. (Also, remember to dispose() your
    graphics in your stop() method, if not sooner.)for the part writen above...I'll try to change it like you are saying.
    Thanx for your help!

  • Is there a way to initialize a Graphics object (indirectly)?

    Hi,
    I've a problem with Graphics Abstract class;
    how must I do to initialize a Graphics object?
    I must execute this code:
    Graphics gr;
    gr.drawString("Image",30,0);
    gr.drawString("not",30,30);
    gr.drawString("Found",30,60);
    image.paintIcon(null,gr,0,0);
    the compiler returns my the error:
    "gr not initialized"
    How can I do to initialize this last?
    Thank.

    Infact with
    gr=null;
    i've a null pointer exception;
    but I've also try
    jLabel2.setText("Prova");
    Graphics gr = jLabel2.getGraphics();
    gr.drawString("Image",30,0);
    gr.drawString("not",30,30);
    gr.drawString("Found",30,60);
    image.paintIcon(this,gr,0,0);
    jLabel3.setIcon(image);
    and I've always a null pointer exception.
    Why?
    Thank.

  • How must I initialize a graphics object?

    Hi,
    I've a problem with Graphics Abstract class;
    how must I do to initialize a Graphics object?
    I must execute this code:
    Graphics gr;
    gr.drawString("Image",30,0);
    gr.drawString("not",30,30);
    gr.drawString("Found",30,60);
    image.paintIcon(null,gr,0,0);
    the compiler returns my the error:
    "gr not initialized"
    How can I do to initialize this last?
    Thank.

    Or you can call getGraphics() on any Component, but most likely you will do any graphics in the paint method as tjacobs said.

  • Table linking graphics object to a smartform

    Hello,
    We are changing logos/ bitmap images that contains our address ( because we are moving to another place).
    Is anyone aware of a table that links the graphics object to a smartform?  For eg, All the texts in a smartform is available in table STXFTX. This helps to have a where used list kind of report.
    We are not able to find that can give us a hwere used list of graphic objects loaded via se78.
    If anyone could give some pointers, that owuldbe of great help.
    Thanks
    Ganesh.S

    You would need to scan the source code for each of your generated function modules.  The graphics calls are directly embedded in the generated code and are called at runtime via function SSFCOMP_PRINT_GRAPHIC.  Check one of your functions and you will see how this works.

  • Create a graphics object

    Hello,
    I want to create a graphics object. However, I need to use it in a function outside of a paint method in an applet.
    I need to create the object to check the FontMetrics of a combination of font parameters. This needs to be done independent of an applet. The problem is I want to use fontMetrics but not in an applet. I need to test the width and height of a message, which will eventually be used in an applet. However, this needs to be done before the applet receives the font.
    Can I create a graphics object outside of a paint method to check the fontMetrics of a combination of fonts? If not, can I check FontMetrics without a graphics object?

    Ok, by "graphic", when you say "I need to test whether one graphic will fit inside another", do you mean an instance of the Image class? If so (sorry, I thought you were wondering if you could create a "Graphics" object for use in calculating the Font Metrics) then you can use the stuff in the javax.imageio package to read your image, or, if you're not at 1.4 yet, do something like:
    image = new ImageIcon("image.gif").getImage();
    // Get the dimensions of the image; these will be non-negative
    width = image.getWidth(null);
    height = image.getHeight(null);to get the dimensions of your images.
    Hope that helped
    Lee

  • Drawing a Graphics object on a Graphics object

    Is there anyway to draw a Graphics object on another Graphics object? I'm trying to show 2 screens, side by side, on a Canvas and I'm trying to cheat a little :-P Basically, I want to take half of one screen and half of another and put them on top of each other to form one screen.
    OR is there a way to turn a Graphics object into an Image object?

    I'm not sure if this is what you're after, but you can
    - create an offscreen image using "createImage(int width, int height)" method of a Component , then
    - obtain the graphics context of the offscreen image using "getGraphics()",
    - you can then draw onto the offscreen image using the graphics context.
    hope that helps. =)

  • Saving Graphics Object

    Is it possible to transfer a Graphics Object to another Canvas so that I can use the painted graphics there?
    I have a canvas, where some texts and boxes are painted. Now I want to start another Canvas, where the user can move a Pointer in the Graphic of the other Canvas.
    So I thought I hand over the Graphics-Object to my new Canvas and overwrite my actual Graphics-Object. Now I want to paint a crosspointer and move it on the screen.
    I hope anyone has understood this stupid question.
    Great_T

    You have to use "manual" double buffering:
    private Image image = Image.creaneImage(this.getWeight(), this.getHeight());
    private Graphics graphics = image.getGraphics();
    Paint to graphics.
    public void paint(Graphics g) {
    g.drawImage(image, 0, 0, Graphics.TOP | Graphics.LEFT);
    Then pass your image in the new Canvas and continue to paint to it's
    Graphics object
    I hope this is helpful.

Maybe you are looking for

  • Error-handling in PKGBUILDs is far from perfect

    Hi, PKGBUILDs should die on failure, rather than just continue regardless of errors. Here's a little rant: Why is it acceptable for ./configure to fail? Or make install to fail? Or the installation of just about any file? They could fail for lots of

  • Data Persistence during WebSite transition

    Hi there, I have a website that stores some files within on the computer, for example within the program data folder.  Like, C:\ProgramData\<company name>\<app> For numerous reasons I would like to keep this data there but I am unsure as to what happ

  • Plz Help!  Unable to open Net 8 Easy Config.

    Hello, I am unable to open Net 8 Easy Config. When I try to open the Net 8 Easy Config, the program hangs. My OS is Win 2k Pro. I am running a P4 chip at 1.5 Ghz. I am trying to open Net 8 Easy Config to create a service name to connect Oracle forms

  • Reg Custom GR message type

    This is regarding GR Message Type WE03 As part of Global template GR form for message we03 is already exists .. we need to develop new form for current roll-out , initially we decided to have a our own GR custon message for this purpose But when i se

  • E55 user defined access point for outgoing email (...

    Hello I changed the outgoing default connection in E55 here: menu. email. settings. mymailbox. mailbox settings. advanced mailb settings.outgoing mail settings. access point in use. user defined now when i do a email message it doesnt send when i cho