[java] java graphics

Started by
8 comments, last by Temer 23 years, 6 months ago
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); } }
Advertisement
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
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
==============================================I feel like a kid in some kind of store... ============================================== www.thejpsystem.com
jeez, you post quickly DJNattyP.
(his is the same as mine)
JP

=============================
a wise man once said....
=============================
www.thejpsystem.com
==============================================I feel like a kid in some kind of store... ============================================== www.thejpsystem.com
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!
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.
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
Thanks, you''re right it did work. But why does it?? Please someone let us know!

Temer
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.
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
- DanielMy homepage

This topic is closed to new replies.

Advertisement