Jump to content

  • Log In with Google      Sign In   
  • Create Account

Interested in a FREE copy of HTML5 game maker Construct 2?

We'll be giving away three Personal Edition licences in next Tuesday's GDNet Direct email newsletter!

Sign up from the right-hand sidebar on our homepage and read Tuesday's newsletter for details!


[java] java graphics


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
9 replies to this topic

#1 Temer   Members   -  Reputation: 122

Like
Likes
Like

Posted 11 October 2000 - 05:34 AM

I wrote a program to display squares on the screen but when i minimize the window or move a portion off of the screen the squares disappear. that is if i minimize then all the squares disappear and if i move a portion off then the portion of the squares that went off disappears. how do i stop this. here is the src import java.awt.*; import java.applet.*; public class DrawSquare extends Applet { int width = 50, height = 50; int winX=20; int x=winX , y=0; int xdiff = 80, ydiff = 70; public void paint(Graphics g) { g.setColor(Color.blue); // g.fillRect(0,0,200,200); for(int index=0; index <=6; index++) { for(int i=0; i < index; i++) { g.fillRect(x, y, width, height); x+=xdiff; } x=winX; y+=ydiff; } g.setColor(Color.blue); } }

Sponsor:

#2 DJNattyP   Members   -  Reputation: 122

Like
Likes
Like

Posted 11 October 2000 - 09:35 AM

I''m not sure if this is the best way, but this clears it up, and it shows how to do double buffering (something you will need to know later if you are planning on working on Java games)...

>> START OF CODE

import java.awt.*;
import java.applet.*;

public class DrawSquare extends Applet
{
int width, height, winX;
int x,y, xdiff, ydiff;

Image backbuffer;
Graphics bbg;

public void init() {
width = 50;
height = 50;
winX = 20;
x = winX;
y = 0;
xdiff = 80;
ydiff = 70;
}


public void update(Graphics g) {
paint(g);
}
public void paint(Graphics g)
{

if (backbuffer == null) {
backbuffer = createImage(this.getSize().width, this.getSize().height);
bbg=backbuffer.getGraphics();
}
bbg.setColor(Color.blue);

for(int index=0; index <=6; index++)
{
for(int i=0; i < index; i++)
{
bbg.fillRect(x, y, width, height);
x+=xdiff;
}
x=winX;
y+=ydiff;

}
bbg.setColor(Color.blue);
g.drawImage(backbuffer, 0, 0, this);
}
}

<< END OF CODE

If there is anything you don''t get about this, please email me at NTPruett@aol.com

Thanks,
Nate

#3 loserkid   Members   -  Reputation: 122

Like
Likes
Like

Posted 11 October 2000 - 09:36 AM

Hello,

The way to fix this is to use double buffering. Here is how with your code:

    

import java.awt.*;
import java.applet.*;


public class DrawSquare extends Applet {

int width = 50, height = 50;
int winX=20;
int x=winX , y=0;
int xdiff = 80, ydiff = 70;

Image buffer;
Graphics offScreen;

public void init() {
buffer = createImage(getSize().width,getSize().height);
offScreen = buffer.getGraphics();
}


public void paint(Graphics g){
offScreen.setColor(Color.blue);
offScreen.fillRect(0,0,200,200);
for(int index=0; index <=6; index++){
for(int i=0; i < index; i++){
offScreen.fillRect(x, y, width, height);
x+=xdiff;
}
x=winX;
y+=ydiff;
}
offScreen.setColor(Color.blue);
g.drawImage(buffer,0,0,this);
}
}


Hoped this helped.
JP.

=============================
a wise man once said....
=============================
www.thejpsystem.com

#4 loserkid   Members   -  Reputation: 122

Like
Likes
Like

Posted 11 October 2000 - 09:37 AM

jeez, you post quickly DJNattyP.
(his is the same as mine)
JP

=============================
a wise man once said....
=============================
www.thejpsystem.com

#5 Temer   Members   -  Reputation: 122

Like
Likes
Like

Posted 11 October 2000 - 11:00 AM

why is there no double buffering in the Simple Graph applet demo that comes with jdk1.3 but there is no problem with that program? Also what is the best program to type java programs in cause i get tired of pressing space and backspace to format code properly in wordpad. is there a way to set up wordpad to work better? by the way thanks for the help though!

#6 Anonymous Poster_Anonymous Poster_*   Guests   -  Reputation:

Likes

Posted 11 October 2000 - 05:06 PM

I have nothing to compare it to but Code Warrior Discovery Programming is pretty nice. $50 for java, C++, and C. I got it at Outpost.com and it came the next day.

#7 DJNattyP   Members   -  Reputation: 122

Like
Likes
Like

Posted 12 October 2000 - 01:13 AM

That''s weird... I think I found out the answer to your question about how the Simple Graph applet works without double-buffering. When I first looked at it I thought that it was just the simple fact that it uses drawLine() because when I changed the fillRect() in your code to drawLine() it didn''t disappear when I minimized the appletviewer window. That was weird thing #1... that drawLine() doesn''t disappear but fillRect() does... I still thought, "Yeah, that''s great and all... the lines don''t disappear... but what if I positively, absolutely, definitely want to draw a filled rectangle... Hmmm... I could draw lots of lines next to each other... Nahhhh!" Then I noticed weird thing #2... that the Simple Graph applet doesn''t use *any* global variables to generate the lines! Every variable was local to paint, or was sent into it via a the f() method that is defined in Simple Graph... So try this: move all your variable declaration (simple cut and paste) out of the top of the DrawSquare class and put it at the top of the paint() method... Compile and... Voila! The squares stay on the screen! I''m not sure why this works... but if anyone knows please post!

Thanks,
Nate

#8 Temer   Members   -  Reputation: 122

Like
Likes
Like

Posted 12 October 2000 - 06:50 AM

Thanks, you''re right it did work. But why does it?? Please someone let us know!

Temer

#9 latrobe   Members   -  Reputation: 122

Like
Likes
Like

Posted 12 October 2000 - 02:39 PM

Try putting
System.out.println("x = " + x + " y = " + y);
at the end of your paint() method...after the second g.setColor() will do.

You will see that y is not being reset to 0 because you left it a class variable. Basically, the second time paint is called, it starts drawing at 20, 490 which is outside of your appletviewer window. Just put y=0 at the beginning of your paint() method and it should work fine.

System.out.println() is your friend...just take them out when you are done.

#10 deakin   Members   -  Reputation: 122

Like
Likes
Like

Posted 12 October 2000 - 11:50 PM

Just in response to the question about an editor, I use EditPlus, which does automatic indenting, keyword highlighting etc. Here is the link. There are a huge range of text editors like this to choose from that normally are free or have a shareware license (Ultraedit is another that comes to mind).

- Daniel
VG Games





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS