Computer Science Canada a different way to do graphics other than canvas |
Author: | ify [ Sat Jun 05, 2004 12:41 pm ] | ||
Post subject: | a different way to do graphics other than canvas | ||
here is a frame layout for a program i find it simpler than canvas
you place all of the image/drawring in the paint method hope this helps someone [/b] |
Author: | ify [ Sat Jun 05, 2004 12:50 pm ] |
Post subject: | and..... |
by image and drawring i mean g.drawRect(x,y,width,height) , and stuff like that and there will probably be filcker such as in this program [code]// The "Final" class. import java.awt.*; public class Final extends Frame { int ballX = 50; int ballY = 200; int xTwenty = 20; int yFive = 5; int playerX = 200; int mouseMoved = 0; // Image offScreenImage; //Graphics offScreenBuffer; public Final () { super ("Final"); // Set the frame's name setSize (400, 400); // Set the frame's size setBackground (Color.black); show (); // Show the frame // Constructor } public boolean mouseMove (Event event, int x, int y) { if (x >= 30 || x <= 350) { playerX = x; repaint (); mouseMoved = 1; return true; } else return false; } public void paint (Graphics g) { g.setColor (Color.black); try { Thread.sleep (75); } catch (Exception e) { } if (ballX < 50 || ballX > 340) { xTwenty = -xTwenty; } else if (ballY < 50 || ballY > 350) { yFive = -yFive; } else if (ballX <= playerX + 100 && ballX >= playerX && ballY >= 310 && ballY <= 330) { yFive = -yFive; } g.fillRect (ballX, ballY, 10, 10); ballX = ballX + xTwenty; ballY = ballY + yFive; g.setColor (Color.white); g.fillRect (ballX, ballY, 10, 10); g.setColor (Color.black); g.fillRect (playerX - 30, 320, 30, 10); g.setColor (Color.white); g.fillRect (playerX, 320, 100, 10); g.setColor (Color.black); g.fillRect (playerX + 100, 320, 30, 10); repaint (); // Place the drawing code here } // paint method if there is filcker use this code to fix it im not sure where u put it but my teacher showed us and ther was no filcker because it draws everything off screen first and then brings it on screen later [code] if (offScreenBuffer == null) { offScreenImage = createImage (size ().width, size ().height); offScreenBuffer = offScreenImage.getGraphics (); } offScreenBuffer.clearRect (0, 0, size ().width, size ().height); g.drawImage (offScreenImage, 0, 0, this); public void update (Graphics g) { paint (g); } public void destroy () { offScreenBuffer.dispose (); }[/code] btw if anyone could fix the filcker on that program plz cause i suck at java and i dont kno thx if you dont want to just dont come on here bitching at me thanks |
Author: | ify [ Sat Jun 05, 2004 1:32 pm ] | ||
Post subject: | one more thing... | ||
if you want to see what the program does (its going to be a break out game(one player pong)) put a while (true) statement at the top of the paint method that is closed at the bottom of the paint method however this wont let u move the pattle and in my last post you will need this to complete it
place that code at the bottom after the paint method me forgot in last post |
Author: | ify [ Sat Jun 05, 2004 2:08 pm ] | ||
Post subject: | and finaly | ||
i fixed the filcker so here is the complete code the paddle and the ball work
|
Author: | ify [ Sat Jun 05, 2004 4:38 pm ] | ||
Post subject: | flicker code with comments | ||
if your having trouble with the flicker code read this it explains where to put the code okee
note : everything in your paint method must be offScreenBuffer.whatever such as: offScreenBuffer.drawRect(x,y,width,height); |
Author: | rizzix [ Sun Jun 06, 2004 2:39 pm ] |
Post subject: | |
nice.. keep up the good work ifv.. we'd like to see more submissions and contributions to the java section. |