a different way to do graphics other than canvas
Author |
Message |
ify
|
Posted: 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
code: | // The "FrameApp" class.
import java.awt.*;
public class FrameApp extends Frame
{
public FrameApp ()
{
super ("FrameApp"); // Set the frame's name
setSize (400, 400); // Set the frame's size
show (); // Show the frame
} // Constructor
public void paint (Graphics g)
{
// Place the drawing code here
} // paint method
public static void main (String[] args)
{
new FrameApp (); // Create a FrameApp frame
} // main method
} // FrameApp class |
you place all of the image/drawring in the paint method hope this helps someone [/b] |
|
|
|
|
|
Sponsor Sponsor
|
|
|
ify
|
Posted: 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 |
|
|
|
|
|
ify
|
Posted: 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
code: | public static void main (String[] args)
{
new Final (); // Create a Final frame
} // main method
} // Final class |
place that code at the bottom after the paint method me forgot in last post |
|
|
|
|
|
ify
|
Posted: 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
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;
Image offScreenImage;
Graphics offScreenBuffer;
// 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 ();
return true;
}
else
return false;
}
public void paint (Graphics g)
{
if (offScreenBuffer == null)
{
offScreenImage = createImage (size ().width, size ().height);
offScreenBuffer = offScreenImage.getGraphics ();
}
offScreenBuffer.clearRect (0, 0, size ().width, size ().height);
offScreenBuffer.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;
}
offScreenBuffer.fillRect (ballX, ballY, 10, 10);
ballX = ballX + xTwenty;
ballY = ballY + yFive;
offScreenBuffer.setColor (Color.white);
offScreenBuffer.fillRect (ballX, ballY, 10, 10);
offScreenBuffer.setColor (Color.black);
offScreenBuffer.fillRect (playerX - 30, 320, 30, 10);
offScreenBuffer.setColor (Color.white);
offScreenBuffer.fillRect (playerX, 320, 100, 10);
offScreenBuffer.setColor (Color.black);
offScreenBuffer.fillRect (playerX + 100, 320, 30, 10);
repaint ();
// Transfer the offScreenBuffer to the screen
g.drawImage (offScreenImage, 0, 0, this);
// Place the drawing code here
} // paint method
public void update (Graphics g)
{
paint (g);
}
public void destroy ()
{
offScreenBuffer.dispose ();
}
public static void main (String[] args)
{
new Final (); // Create a Final frame
} // main method
} // Final class
|
|
|
|
|
|
|
ify
|
Posted: 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
code: | // For drawing images offScreen (prevents Flicker)
// These variables keep track of an off screen image object and
// its corresponding graphics object
Image offScreenImage;
Graphics offScreenBuffer;
// Set up the offscreen buffer the first time paint() is called
if (offScreenBuffer == null)
{
offScreenImage = createImage (size ().width, size ().height);
offScreenBuffer = offScreenImage.getGraphics ();
}
// All of the drawing is done to an off screen buffer which is
// then copied to the screen. This will prevent flickering
// Clear the offScreenBuffer first
offScreenBuffer.clearRect (0, 0, size ().width, size ().height);
// Transfer the offScreenBuffer to the screen
g.drawImage (offScreenImage, 0, 0, this);
// We need to override update when using the offScreenBuffer
// To prevent the automatic clearing of the screen
public void update (Graphics g)
{
paint (g);
}
// We need to get rid of the offScreenBuffer on our own
// I don't know if this is needed
public void destroy ()
{
offScreenBuffer.dispose ();
} |
note : everything in your paint method must be offScreenBuffer.whatever
such as:
offScreenBuffer.drawRect(x,y,width,height); |
|
|
|
|
|
rizzix
|
Posted: Sun Jun 06, 2004 2:39 pm Post subject: (No subject) |
|
|
nice.. keep up the good work ifv.. we'd like to see more submissions and contributions to the java section. |
|
|
|
|
|
|
|