Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (536)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Moving frame data, simple solution?  (Read 669 times)
0 Members and 1 Guest are viewing this topic.
Offline Kurten

Junior Member


Medals: 3
Exp: 3 years


-w-


« Posted 2011-05-17 01:29:54 »

Hello folks!

I've been building a very basic program on my free time, just for fun, it basically draws random numbers in a line, then jumps to the next row when the line finishes. However, when reaching the bottom right part of the frame it continues down out of view. What i want it to do is move everything, numbers and background graphics upwards one line, then draw a new black square at the bottom row to draw the new numbers on. I looked around a bit in the Java API docs after a method that could do this for me and i found a method in the Graphics class called copyArea. This method moves everything but i can't make it display the graphics properly, im unsure of what i am doing wrong. Could you guys help?

EDIT: I managed to fix it, im unsure how though XD

Here is the source
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.util.*;

public class MainEngine extends JPanel implements Runnable{
   
   Random rand = new Random();
   
   int letterPosX, letterPosY;
   int number;
   boolean redraw = false;
   boolean running = true;
   boolean drawLetters = true;
   
   Graphics2D g2d;
   BufferedImage backbuffer;
   JPanel p1 = new JPanel();
   JFrame f = new JFrame();
   Thread CalcLoop;
   
   public static void main(String[] args){
      MainEngine me = new MainEngine();
      me.initCalculate();
   }
   
   public void initCalculate(){
      CalcLoop = new Thread(this);
      CalcLoop.start();  
   }
   
   @Override
   public void run() {

      initgfx();
     
      while(running == true){
         try{
            updateGraphics();
            repaint();
            Thread.sleep(1);
         }catch(InterruptedException e){}
      }
   }
   
   public void update(Graphics g){
      System.out.println("updating");
      paint(g);
   }
   
   

   
   public void initgfx(){
      backbuffer = new BufferedImage(800, 600, BufferedImage.TYPE_INT_RGB);
      g2d = backbuffer.createGraphics();
      f.setSize(800, 600);
      f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      f.getContentPane().add(this);
      f.setVisible(true);
      g2d.setColor(Color.BLACK);
      g2d.fillRect(0, 0, 800, 600);
   }
   
   public void updateGraphics(){
     
      if(drawLetters == true){
         g2d.setColor(Color.WHITE);
         number = rand.nextInt(10);
         g2d.drawString("" + number, letterPosX, letterPosY);
         letterPosX += 7;
      }
      if(letterPosX >= 790){
         letterPosX = 0;
         letterPosY += 11;
         
      }
     
      if(letterPosY >= 600){
         g2d.copyArea(0, 0, 800, 600, 0, -11);
         g2d.setColor(Color.BLACK);
         g2d.drawRect(0, 0, 800, 600);
         redraw = true;
         g2d.setColor(Color.WHITE);
         letterPosY = 589;
      }
     
      if(redraw != false){
         g2d.setColor(Color.BLACK);
         g2d.drawRect(0, 0, 800, 600);
         //running = false;
     }
   }
   
   public void updateLastPos(){

   }
   
   public void paint(Graphics g){
      g.drawImage(backbuffer, 0, 0, null);
   }
   
}
Offline ra4king

JGO Kernel


Medals: 340
Projects: 2
Exp: 5 years


I'm the King!


« Reply #1 - Posted 2011-05-17 04:02:12 »

I ran this on my computer and when the lines scrolled up, they overlapped the ones above them. After further investigation, I realized it was because the last line is still there after copying all the ones above. The fix is that you should replace line 87 on this code with:
1  
g2d.fillRect(0,letterPosY-10,800,11);

And there is now no need for lines 93 to 97 since I don't see the point of drawing a black rectangle around the whole thing.

Also, did you know you can just use boolean variables by themselves in any statement or loop that requires a boolean.
For example, you can:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
//change
while(running == true) {

//to
while(running) {

//and
if(redraw != false) {

//is the same thing as
if(redraw == true) {

//which can be changed to
if(redraw) {

Offline Kurten

Junior Member


Medals: 3
Exp: 3 years


-w-


« Reply #2 - Posted 2011-05-17 04:09:01 »

I removed the black rectangle, it was just a failed attempt by me to get rid of the overlapping Tongue

I just realized why it overlapped, just while posting this and looking through those exact lines you mentioned. I actually wanted to fill a rect, but somehow mistook that for drawrect, which explains why it was still overlapping even after i thought i filled a rect over the entire thing.

Thanks for the help ra4king ^_^

Also, i'll try to remember the boolean checks you presented since they seem easier to read and use ^_^
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ra4king

JGO Kernel


Medals: 340
Projects: 2
Exp: 5 years


I'm the King!


« Reply #3 - Posted 2011-05-17 04:15:11 »

Glad to help Smiley

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

CogWheelz (9 views)
2014-07-30 21:08:39

Riven (20 views)
2014-07-29 18:09:19

Riven (14 views)
2014-07-29 18:08:52

Dwinin (12 views)
2014-07-29 10:59:34

E.R. Fleming (32 views)
2014-07-29 03:07:13

E.R. Fleming (12 views)
2014-07-29 03:06:25

pw (42 views)
2014-07-24 01:59:36

Riven (42 views)
2014-07-23 21:16:32

Riven (29 views)
2014-07-23 21:07:15

Riven (30 views)
2014-07-23 20:56:16
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!