Having Trouble With GetGraphics(); In Applets - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Having Trouble With GetGraphics(); In Applets

ColacXColacX Posts: 141Member
Hi i have two classes and one webbpage using JCreatorPro
class server that currently doesnt extend any thing nor implements anything
class playergui extends java.applet.Applet implements KeyListener

the code marked below in red doesnt work, further below you can se it in its context.
[color=Red]background1.getGraphics().drawLine(0,0,100,100);[/color]

I think that the error codes are saying something about creating the image with toolkit but i created them using applet.

Hope I've been clear enough. Thanks for your help.

This is the error codes
[code]
Exception in thread "AWT-EventQueue-1" java.lang.UnsupportedOperationException: getGraphics() not valid for images created with createImage(producer)
at sun.awt.image.ToolkitImage.getGraphics(ToolkitImage.java:292)
at playergui.paint(playergui.java:55)
at sun.awt.RepaintArea.paintComponent(RepaintArea.java:248)
at sun.awt.RepaintArea.paint(RepaintArea.java:224)
at sun.awt.windows.WComponentPeer.handleEvent(WComponentPeer.java:301)
at java.awt.Component.dispatchEventImpl(Component.java:4486)
at java.awt.Container.dispatchEventImpl(Container.java:2116)
at java.awt.Component.dispatchEvent(Component.java:4240)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)
[/code]
server.java
[code]
import java.awt.*;

public class server{
playergui playergui1;
Image picture;
Image background1;
Image plane1;
int width,heigth;

public server //communication through refrence until networking is completed
(
playergui playerguix,
Image backgroundx1,
Image planex1
)
{
playergui1=playerguix;
background1=backgroundx1; //Bypassing applet securitys
plane1=planex1;

width=1200;
heigth=600;

[color=Red]background1.getGraphics().drawLine(0,0,100,100);[/color]

playergui1.picture=background1;
}
}
[/code]
playergui.java
[code]
import java.awt.*;
import java.awt.event.*;

public class playergui extends java.applet.Applet implements KeyListener
{

int guiwidth,guiheight;
server server1;
Image picture;

int shipx,shipy;

public void init()
{
guiwidth=getWidth();
guiheight=getHeight();
System.out.println(guiwidth);
System.out.println(guiheight);
server1=new server //communication through refrence until networking is completed
(
this,
getImage(getDocumentBase(),"background1.jpg"),
getImage(getDocumentBase(),"plane1.jpg")
);

addKeyListener(this);
}

public void keyPressed(KeyEvent e)
{
if(e.getKeyCode()==KeyEvent.VK_W)
System.out.println(e);
else if(e.getKeyCode()==KeyEvent.VK_A)
System.out.println(e);
else if(e.getKeyCode()==KeyEvent.VK_S)
System.out.println(e);
else if(e.getKeyCode()==KeyEvent.VK_D)
System.out.println(e);
}
public void keyReleased(KeyEvent e)
{
}
public void keyTyped(KeyEvent e){}

public void paint(Graphics g)
{
g.drawImage(picture,0,0,Color.white,this);
}
}
[/code]
Webbpage.html
[code]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">











[/code]

Comments

  • zibadianzibadian Posts: 6,349Member
    : Hi i have two classes and one webbpage using JCreatorPro
    : class server that currently doesnt extend any thing nor implements
    : anything
    : class playergui extends java.applet.Applet implements KeyListener
    :
    : the code marked below in red doesnt work, further below you can se
    : it in its context.
    : [color=Red]background1.getGraphics().drawLine(0,0,100,100);[/color]
    :
    : I think that the error codes are saying something about creating the
    : image with toolkit but i created them using applet.
    :
    : Hope I've been clear enough. Thanks for your help.
    :
    : This is the error codes
    : [code]:
    : Exception in thread "AWT-EventQueue-1" java.lang.UnsupportedOperationException: getGraphics() not valid for images created with createImage(producer)
    : at sun.awt.image.ToolkitImage.getGraphics(ToolkitImage.java:292)
    : at playergui.paint(playergui.java:55)
    : at sun.awt.RepaintArea.paintComponent(RepaintArea.java:248)
    : at sun.awt.RepaintArea.paint(RepaintArea.java:224)
    : at sun.awt.windows.WComponentPeer.handleEvent(WComponentPeer.java:301)
    : at java.awt.Component.dispatchEventImpl(Component.java:4486)
    : at java.awt.Container.dispatchEventImpl(Container.java:2116)
    : at java.awt.Component.dispatchEvent(Component.java:4240)
    : at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
    : at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)
    : at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)
    : at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)
    : at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)
    : at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)
    : at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)
    : [/code]:
    : server.java
    : [code]:
    : import java.awt.*;
    :
    : public class server{
    : playergui playergui1;
    : Image picture;
    : Image background1;
    : Image plane1;
    : int width,heigth;
    :
    : public server //communication through refrence until networking is completed
    : (
    : playergui playerguix,
    : Image backgroundx1,
    : Image planex1
    : )
    : {
    : playergui1=playerguix;
    : background1=backgroundx1; //Bypassing applet securitys
    : plane1=planex1;
    :
    : width=1200;
    : heigth=600;
    :
    : [color=Red]background1.getGraphics().drawLine(0,0,100,100);[/color]
    :
    : playergui1.picture=background1;
    : }
    : }
    : [/code]:
    : playergui.java
    : [code]:
    : import java.awt.*;
    : import java.awt.event.*;
    :
    : public class playergui extends java.applet.Applet implements KeyListener
    : {
    :
    : int guiwidth,guiheight;
    : server server1;
    : Image picture;
    :
    : int shipx,shipy;
    :
    : public void init()
    : {
    : guiwidth=getWidth();
    : guiheight=getHeight();
    : System.out.println(guiwidth);
    : System.out.println(guiheight);
    : server1=new server //communication through refrence until networking is completed
    : (
    : this,
    : getImage(getDocumentBase(),"background1.jpg"),
    : getImage(getDocumentBase(),"plane1.jpg")
    : );
    :
    : addKeyListener(this);
    : }
    :
    : public void keyPressed(KeyEvent e)
    : {
    : if(e.getKeyCode()==KeyEvent.VK_W)
    : System.out.println(e);
    : else if(e.getKeyCode()==KeyEvent.VK_A)
    : System.out.println(e);
    : else if(e.getKeyCode()==KeyEvent.VK_S)
    : System.out.println(e);
    : else if(e.getKeyCode()==KeyEvent.VK_D)
    : System.out.println(e);
    : }
    : public void keyReleased(KeyEvent e)
    : {
    : }
    : public void keyTyped(KeyEvent e){}
    :
    : public void paint(Graphics g)
    : {
    : g.drawImage(picture,0,0,Color.white,this);
    : }
    : }
    : [/code]:
    : Webbpage.html
    : [code]:
    : <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    :
    :
    :
    :
    :
    :

    :
    :
    :

    :
    :
    : [/code]:
    :
    In this case there are two image objects: 1 in the applet and 1 on the server. In the call new server(), the applet sends the image data to the server, and there a new image gets built to use it there. The error you're getting ensures that both copies remain in synch to eachother.
  • ColacXColacX Posts: 141Member
    : In this case there are two image objects: 1 in the applet and 1 on
    : the server. In the call new server(), the applet sends the image
    : data to the server, and there a new image gets built to use it
    : there. The error you're getting ensures that both copies remain in
    : synch to eachother.

    I did? I thought I sent an image to the server. Let the server process it and send a new image back. How would I write to make new images? (nonsynch)
  • zibadianzibadian Posts: 6,349Member
    : : In this case there are two image objects: 1 in the applet and 1 on
    : : the server. In the call new server(), the applet sends the image
    : : data to the server, and there a new image gets built to use it
    : : there. The error you're getting ensures that both copies remain in
    : : synch to eachother.
    :
    : I did? I thought I sent an image to the server. Let the server
    : process it and send a new image back. How would I write to make new
    : images? (nonsynch)

    On the server, you should first create a BufferedImage object. Then you can draw the client image onto that one and possibly paint other things on it. Finally send that image back to the client.

    For more info see: http://java.sun.com/javase/6/docs/api/java/awt/image/BufferedImage.html
Sign In or Register to comment.