The Artima Developer Community
Sponsored Link

Java Answers Forum
draw

3 replies on 1 page. Most recent reply: Jun 21, 2002 9:19 PM by Charles Bell

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 3 replies on 1 page
Chant

Posts: 5
Nickname: meridian
Registered: Jun, 2002

draw Posted: Jun 21, 2002 8:16 PM
Reply to this message Reply
Advertisement
can someone tell me why this simple code isn't working? there's a double buffer but i haven't added the threading yet:


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

public class Shapes extends JApplet
{

private final Dimension wh;
private Image backImage;
private Graphics backGraphics;

public void init()
{

wh = getSize();

}


public void paint(Graphics g)
{


backImage = createImage(wh.width, wh.height);
backGraphics = backImage.getGraphics();


backGraphics.setColor(Color.black);
backGraphics.fillRect(0, 0, wh.width, wh.height);
backGraphics.setColor(Color.red);
backGraphics.fillOval(300, 300, 50, 50);
backGraphics.setColor(Color.green);
backGraphics.fillOval(310, 310, 5, 5);

g.drawImage(backImage, 0, 0, this);

}

}


Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: draw Posted: Jun 21, 2002 8:33 PM
Reply to this message Reply

change the line:
private final Dimension wh;

to:
private Dimension wh;

make sure your html applet tags has a large enough width and height and it should also work on your PC.
It works on mine.


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

public class Shapes extends JApplet
{

//private final Dimension wh;
private Dimension wh;
private Image backImage;
private Graphics backGraphics;

public void init(){

wh = getSize();

}


public void paint(Graphics g){


backImage = createImage(wh.width, wh.height);
backGraphics = backImage.getGraphics();


backGraphics.setColor(Color.black);
backGraphics.fil lRect(0, 0, wh.width, wh.height);
backGraphics.setColor(Color.red);
backGraphics.fillOval(300, 300, 50, 50);
backGraphics.setColor(Color.green);
backGraphics.fillOval(310, 310, 5, 5);

g.drawImage(backImage, 0, 0, this);

}

}


*****************************************


<html>
<hea d>
<title>Shapes</title>
</head>

<body>
<applet codebase ="." code="Shapes.class" width = "500" height = "500">

</applet>

</body>

</html>

Chant

Posts: 5
Nickname: meridian
Registered: Jun, 2002

Re: draw Posted: Jun 21, 2002 8:43 PM
Reply to this message Reply
Thanks a lot. that worked fine. Any reason why I can't declare the Dimension final? I thought final mean't that a variable can't be changed...

Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: draw Posted: Jun 21, 2002 9:19 PM
Reply to this message Reply
The following works also;


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

public class Shapes extends JApplet
{

//private final Dimension wh;
private final Dimension wh = getSize();
private Image backImage;
private Graphics backGraphics;

public void init(){



}


public void paint(Graphics g){


backImage = createImage(wh.width, wh.height);
backGraphics = backImage.getGraphics();


backGraphics.setColor(Color.black);
backGraphics.fillRect(0, 0, wh.width, wh.height);
backGraphics.setColor(Color.red);
backGraphics.fillOval(300, 300, 50, 50);
backGraphics.setColor(Color.green);
backGraphics.fillOval(310, 310, 5, 5);

g.drawImage(backImage, 0, 0, this);

}

}

Flat View: This topic has 3 replies on 1 page
Topic: Accessing mailbox from landline phone Previous Topic   Next Topic Topic: Problems with resizing JFrame

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use