interference between two displays


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: interference between two displays

  1. #1
    Join Date
    May 2005
    Posts
    2

    interference between two displays

    hello everyone:
    im having lots of problem gettin this thing to work!!!!!!! as u can see its a pong game written in JApplet and my problem is that i cant seem to find a solution to make both the initial display for the introduction and the display of the game to work together without any interference between them. if anyone could help me on this one id really appreciate it (also any advices are welcome since its possible that there are many unused things in the code), here's the code:

    import java.awt.Graphics;
    import java.awt.Event;
    import java.awt.Color;
    import java.awt.Point;
    import java.awt.Rectangle;
    import java.awt.Image;
    import java.lang.Thread;
    import java.lang.Math;
    import java.applet.*;
    import java.net.*;
    import java.awt.*;
    import java.applet.Applet;
    import javax.swing.*;

    public class jPongback extends Applet implements Runnable { Thread runner;
    Image offscreeni;
    Graphics offscreeng;
    Rectangle plane;
    Point ballPoint, racketPoint, enemyPoint, ballSpeed;
    boolean start, death = false;
    int playerScore, enemyScore = 0;
    int tuffhet = 8;
    int place;
    AudioClip rebond;
    AudioClip touch;
    AudioClip murrebond;
    Image ii;
    Dimension d;
    int[] dx,dy;
    Graphics goff;
    Font largefont = new Font("Helvetica", Font.BOLD, 24);
    Font smallfont = new Font("Helvetica", Font.BOLD, 14);
    final int blocksize=24;
    final int nrofblocks=15;
    FontMetrics fmsmall, fmlarge;
    final int scrsize=nrofblocks*blocksize;
    boolean showtitle=true;
    boolean scared=false;
    final int screendelay=120;
    int count=screendelay;
    short[] screendata;

    public String getAppletInfo()
    {
    return("PacMan - par Hugon Bastien");
    }

    public void init() {
    Graphics g;
    d = getSize();
    setBackground(Color.blue);
    g=getGraphics();
    g.setFont(smallfont);
    fmsmall = g.getFontMetrics();
    g.setFont(largefont);
    fmlarge = g.getFontMetrics();
    GameInit();}
    public void GameInit(){
    offscreeni = createImage(this.size().width, this.size().height);
    offscreeng = offscreeni.getGraphics();
    setBackground(Color.black);
    ballPoint = new Point((this.size().width/2), (this.size().height/2));
    racketPoint = new Point((this.size().width -25), ((this.size().height/2) -25));
    enemyPoint = new Point(20, ((this.size().height/2) -25));
    plane = new Rectangle(15, 15, (this.size().width), (this.size().height -30));
    ballSpeed = new Point(0,0);
    rebond = getAudioClip (getCodeBase(), "rebond.au");
    murrebond = getAudioClip (getCodeBase(), "murrebond.au");
    touch = getAudioClip (getCodeBase(), "touch.au");
    repaint();
    }
    public void start() {
    if (runner == null) {
    runner = new Thread(this);
    runner.start();
    }
    }
    public void stop() {
    if (runner != null) {
    runner.stop();
    runner = null;
    }
    }
    public void run() {

    while (true) {
    checkRacket();
    checkEnemy();
    checkWalls();
    moveBall();
    moveEnemy(enemyPoint.y + 25);
    repaint();
    try { Thread.sleep(35); }
    catch (InterruptedException e) { };
    }

    }
    public boolean mouseMove(Event evt, int x, int y) {
    racketPoint.y = (y - 25);
    repaint();
    return true;

    }
    public boolean mouseUp(Event evt, int x, int y) {
    if (start == false) { ballSpeed.x = 4; ballSpeed.y = 2;
    start = true; }
    return true;
    }
    void moveBall() {
    ballPoint.x = (ballPoint.x + ballSpeed.x);
    ballPoint.y = (ballPoint.y + ballSpeed.y);
    }
    void moveEnemy(int enemyPos) {
    int dist = java.lang.Math.abs(ballPoint.y - enemyPos);
    if (ballSpeed.x < 0) {
    if (enemyPos < (ballPoint.y - 3)) enemyPoint.y = (enemyPoint.y + dist/tuffhet);
    else if (enemyPos > (ballPoint.y + 3)) enemyPoint.y = (enemyPoint.y - dist/tuffhet);
    }
    else {
    if (enemyPos < (this.size().height / 2 - 3)) enemyPoint.y = (enemyPoint.y + 2); else if (enemyPos > (this.size().height / 2 + 3)) enemyPoint.y = (enemyPoint.y - 2);
    }
    }
    void checkRacket() {
    if (ballSpeed.x < 0) return;
    if ((ballPoint.x + ballSpeed.x) >= racketPoint.x - 6 & (ballPoint.x < racketPoint.x))
    if ((ballPoint.y + 8) > racketPoint.y & ballPoint.y < (racketPoint.y + 50)) {
    int racketHit = (ballPoint.y - (racketPoint.y +25));
    rebond.play();
    ballSpeed.y = (ballSpeed.y + (racketHit/7));
    ballSpeed.x = (ballSpeed.x * -1);
    }
    }
    void checkEnemy() {
    if (ballSpeed.x > 0) return;
    if ((ballPoint.x + ballSpeed.x) <= enemyPoint.x + 4 & (ballPoint.x > enemyPoint.x))
    if ((ballPoint.y + 8) > enemyPoint.y & ballPoint.y < (enemyPoint.y + 50)) {
    int racketHit = (ballPoint.y - (enemyPoint.y +25));
    rebond.play();
    ballSpeed.y = (ballSpeed.y + (racketHit/7));
    ballSpeed.x = (ballSpeed.x * -1);
    }
    }
    void checkWalls() {
    if ((ballPoint.x + ballSpeed.x) <= plane.x) miss();
    if ((ballPoint.x + ballSpeed.x) >= (plane.width - 20)) miss();
    if ((ballPoint.y + ballSpeed.y) <= plane.y) {murrebond.play();ballSpeed.y = (ballSpeed.y * -1);}
    if ((ballPoint.y + ballSpeed.y) >= (plane.height + 8)) {murrebond.play();ballSpeed.y = (ballSpeed.y * -1);}
    }
    void miss() {
    if (ballSpeed.x < 0) {
    touch.play();
    playerScore = (playerScore + 1);
    if (tuffhet > 2) tuffhet = (tuffhet - 1);
    }
    else {touch.play();enemyScore = (enemyScore + 1);}
    ballSpeed.x = (ballSpeed.x * -1);
    ballPoint.x = (ballPoint.x + ballSpeed.x);

    for (int i = 3; i > 0; i = (i - 1)) {
    death = true;
    repaint();
    try { Thread.sleep(300); }
    catch (InterruptedException e) { };
    death = false;
    repaint();
    try { Thread.sleep(300); }
    catch (InterruptedException e) { };
    }

    ballPoint = new Point((this.size().width/2), (this.size().height/2)); //reinitialisation de la position de la balle
    ballSpeed.x = 0;
    ballSpeed.y = 0;
    start = false;
    }

    public void ShowIntroScreen()
    {
    String s;

    goff.setFont(largefont);

    goff.setColor(new Color(0,32,48));
    goff.fillRect(16, scrsize/2 - 40, scrsize-32,80);
    goff.setColor(Color.white);
    goff.drawRect(16, scrsize/2 - 40, scrsize-32,80);

    if (showtitle)
    {
    s="Java Pong";
    scared=false;

    goff.setColor(Color.white);
    goff.drawString(s,(scrsize-fmlarge.stringWidth(s)) / 2 +2, scrsize/2 - 20 +2);
    goff.setColor(new Color(96,128,255));
    goff.drawString(s,(scrsize-fmlarge.stringWidth(s)) / 2, scrsize/2 - 20);

    s="par Bastien Hugon";
    goff.setFont(smallfont);
    goff.setColor(new Color(255,160,64));
    goff.drawString(s,(scrsize-fmsmall.stringWidth(s))/2,scrsize/2 + 20);

    s="lacanaupro@hotmail.com";
    goff.setColor(new Color(255,160,64));
    goff.drawString(s,(scrsize-fmsmall.stringWidth(s))/2,scrsize/2 + 30);
    }
    else
    {
    goff.setFont(smallfont);
    goff.setColor(new Color(96,128,255));
    s="'S' pour une nouvelle partie";
    goff.drawString(s,(scrsize-fmsmall.stringWidth(s))/2,scrsize/2 - 10);
    goff.setColor(new Color(255,160,64));
    s="Utiliser la souris pour se deplacer";
    goff.drawString(s,(scrsize-fmsmall.stringWidth(s))/2,scrsize/2 + 20);
    scared=true;
    }
    count--;
    if (count<=0)
    { count=screendelay; showtitle=!showtitle; }
    }
    public void update(Graphics g) {
    paint(g);
    }

    public void PlayDemo(){ShowIntroScreen();}

    public void paint(Graphics g) {
    String s;
    Graphics gg;

    if (goff==null && d.width>0 && d.height>0)
    {
    ii = createImage(d.width, d.height);
    goff = ii.getGraphics();
    }
    if (goff==null || ii==null)
    return;

    goff.setColor(Color.black);
    goff.fillRect(0, 0, d.width, d.height);


    PlayDemo();

    g.drawImage(ii, 0, 0, this);


    offscreeng.setColor(Color.red); offscreeng.fillRect(0,0,this.size().width, this.size().height); if (death == false) offscreeng.setColor(Color.black);
    else offscreeng.setColor(Color.yellow); offscreeng.drawString(Integer.toString(enemyScore), 135, 35);
    offscreeng.drawString(Integer.toString(playerScore), 215, 35);
    offscreeng.clipRect(plane.x, plane.y, plane.width - 28, plane.height + 1); offscreeng.drawRect(plane.x, plane.y, plane.width - 30, plane.height); offscreeng.fillRect(racketPoint.x, racketPoint.y, 6,50); offscreeng.fillRect(enemyPoint.x, enemyPoint.y, 6, 50);
    offscreeng.fillOval(ballPoint.x, ballPoint.y, 8, 8); offscreeng.drawLine(plane.width/2,plane.y,plane.width/2,(plane.y+plane.height));
    PlayDemo();
    g.drawImage(offscreeni,0,0,this);

    }
    }


    thanks everyone..........

  2. #2
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560

    Comments:

    Always ensure that the applet has width & height before using these for
    initializing other objects. If you override paint/update then do it all
    yourself, i.e. dont call methods like setBackground(Color), instead do it
    as the first step in update (set a color and fillRect the whole applet).

    You should set up paint/update like:
    Code:
    public void paint(Graphics g) {
      update(g);
    }
    public void update(Graphics g) {
      // do the "setBackground()" stuff
       Color c=g.getColor();
       g.setColor(Color.blue);
       g.fillRect(0,0,getWidth(), getHeight());
       g.setColor(c);
       .
       .
    }
    I see that you do this the other way around (update invokes paint), have you
    tested that ? When I do that my memorystack goes through the roof.....

    Also, if you want fontmetrics then pick them up on the first call to update(Graphics),
    that is, when these metrics are null

    Also, use MediaTracker for all images that you create or upload to ensure
    that they are not used before they are ready.
    Last edited by sjalle; 05-22-2005 at 11:44 AM.
    eschew obfuscation

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center