g.drawstring

For some odd reason, this set of code isn't working

else if (checky == 11)

{

setBackground(Color.white);

g.setColor(Color.black);

g.drawString("YOU LOST!", 110,150);

}

Can someone tell me why the drawstring isnt working? The background is becoming white, but nothing is being displayed on the screen.

[350 byte] By [679420phillip1492a] at [2007-11-26 12:18:35]
# 1
Where is this code? Is it in an overridden paintComponent() method?
642814zadoka at 2007-7-7 14:58:26 > top of Java-index,Archived Forums,Socket Programming...
# 2

This is everything :

while (true)

{

try

{

if (checky == 111){

displayImage(m_Graphics);

}

else if (checky == 11)

{

//repaint ();

setBackground(Color.white);

g.setColor(Color.black);

g.drawString("YOU LOST!", 110,150);

}

m_nCurrImage++;

if (m_nCurrImage == NUM_IMAGES && checky == 111)

m_nCurrImage = 0;

Thread.sleep(10);

}

catch (InterruptedException e)

{

stop();

}

}

679420phillip1492a at 2007-7-7 14:58:26 > top of Java-index,Archived Forums,Socket Programming...
# 3

This is EVERYTHING

import java.applet.*;

import java.awt.*;

import java.util.Random;

import java.io.*;

import java.lang.StringBuffer;

import java.io.IOException;

import java.util.*;

public class leetagain extends Applet implements Runnable

{

private Thread m_leetagain = null;

private Graphics m_Graphics;

private Image m_Images[];

public Image comets[];

public Image m_Images2;

public Image m_Imagescomet1;

public Image m_Imagescomet2;

public Image m_Imagescomet3;

public Image m_Imagescomet4;

public Imagecometimage3;

public Image cometimage2;

public Image cometimage1;

public Image cometimage4;

public Image imagestar;

public Graphics g;

public int counter = 0;

public String strImage3 = new String ("");

public String comet11 = new String ("");

public String comet22 = new String ("");

public String comet33 = new String ("");

public String comet44 = new String ("");

public String strImage6 = new String ("");

public String strImage2 = new String ("");

private int m_nCurrImage;

private int m_nImgWidth = 0;

public int county1 = 0;

public int county2 = 0;

public int county3 = 0;

public int county4 = 0;

public int buffercount1 = 0;

private int m_nImgHeight = 0;

private boolean m_fAllLoaded = false;

private final int NUM_IMAGES = 18;

private final int NUM_IMAGES2 = 4;

public int b;

public int coor3 = 0;

public int coor33 =0;

public int coor4 = 0;

public int coor44 =0;

public int coor2 = 0;

public int coor22 =0;

public int coor1 = 0;

public int coor11 =0;

public int c;

public int cometcoor1 = 0;

public int cometcoor2 = 0;

public int cometcoor3 = (int)(Math.random() *500);

public int cometcoor4 = 500;

public int cometcoor11 = (int)(Math.random() *500);

public int cometcoor22 = (int)(Math.random() *500);

public int cometcoor33 = 0;

public int cometcoor44 = (int)(Math.random() *500);

public int bigcounty = 0;

public int ness;

public int checky = 111;

public int check = 0;

public int check2 = 0;

public int count2 =0;

public int count3 = 0;

public int count = 0;

public int divide2 = 0;

public int divide = 0;

public int coorcounty2 = 0;

public int cometcheck2=0;

public int coorcounty1 = 0;

public int cometcheck1=0;

public int coorcounty3 = 0;

public int cometcheck3=0;

public int coorcounty4 = 0;

public int cometcheck4=0;

public boolean handleEvent(Event e, Graphics g)

{

if(e.id == Event.ACTION_EVENT){

String b= ((Button)e.target).getLabel();

if (b == "Instructions"){

setBackground(Color.white);

ness= 1000;

}

}

return true;

}

public void pause(int ness, Graphics g)

{

}

public void init()

{

resize(500, 500);

}

public void destroy()

{

}

private void displayImage(Graphics g)

{

if (!m_fAllLoaded)

return;

//setBackground(Color.black);

try {

if (checky == 111){

setBackground(Color.black);

g.drawImage(m_Images2 ,250, 250, null);

g.drawImage(imagestar ,180, 145, null);

g.drawImage(imagestar ,460, 240, null);

g.drawImage(imagestar ,27, 300, null);

g.drawImage(imagestar ,300, 420, null);

g.drawImage(imagestar ,350, 40, null);

g.drawImage(imagestar,100, 100, null);

if (bigcounty == 0)

{

//g.draw(star1,150,150);

g.setColor(Color.white);

g.drawString("NOTICE: THE GLOBE WILL BE TO THE LOWER RIGHT OF THE MOUSE CLICK!", 70,40);

g.setColor(Color.black);

}

else if (bigcounty == 1){

repaint();

}

++bigcounty;

g.setColor(Color.black);

g.drawOval(count,count2, 40,40);

g.fillOval(count,count2,40,40);

g.drawOval(count,count2+10, 40,40);

g.fillOval(count,count2+10,40,40);

g.drawOval(count,count2-10, 40,40);

g.fillOval(count,count2-10,40,40);

g.drawOval(count+10,count2, 40,40);

g.fillOval(count+10,count2,40,40);

g.drawOval(count-10,count2, 40,40);

g.fillOval(count-10,count2,40,40);

//setBackground(Color.black);

g.drawImage(m_Images[m_nCurrImage],count, count2, null);

Thread.sleep(20);

if (count < b){

++count;

divide = count/b;

}

else if (count > b){

--count;

divide = count/b;

}

if (count2 < c)

{

++count2;

divide2 = count2/c;

}

else if (count2 > c){

--count2;

divide2 = count2/c;

}

g.setColor(Color.black);

g.drawRect(cometcoor1,cometcoor11, 40,40);

g.fillRect(cometcoor1,cometcoor11,40,40);

g.drawRect(cometcoor1,cometcoor11-10, 40,40);

g.fillRect(cometcoor1,cometcoor11-10,40,40);

g.drawRect(cometcoor1,cometcoor11+10, 40,40);

g.fillRect(cometcoor1,cometcoor11+10,40,40);

g.drawRect(cometcoor1-10,cometcoor11, 40,40);

g.fillRect(cometcoor1-10,cometcoor11,40,40);

g.drawRect(cometcoor1+10,cometcoor11, 40,40);

g.fillRect(cometcoor1+10,cometcoor11,40,40);

if (coorcounty1 == 0){

cometcheck1 = cometcoor11;

}

++coorcounty1;

// X = 0 and Y is changing... Comet starts on the left of the screen

if (county1 ==0){

if (buffercount1 == 0){

g.drawImage(cometimage1,cometcoor1, cometcoor11, null);

}

else if (buffercount1 == 1){

g.drawImage(m_Imagescomet1, cometcoor1, cometcoor11, null);

}

++cometcoor1;

--cometcoor11;

if (cometcheck1 == cometcoor1){

county1 = 1;

cometcoor1 = (int)(Math.random() *500);

cometcoor11=500;

cometcheck1 = cometcoor1;

}

++buffercount1;

if (buffercount1 == 2){

buffercount1 = 0;

}

}

// Y = 500 and X is changing... Comet starts on bottom side of the screen

if (county1 == 1){

g.drawImage(cometimage1,cometcoor1, cometcoor11, null);

++cometcoor1;

--cometcoor11;

if (cometcoor11 == cometcheck1){

county1= 0;

cometcheck1 = cometcoor11;

cometcoor11=(int)(Math.random() *500);

cometcoor1 = 0;

}

}

g.drawRect(cometcoor4,cometcoor44, 40,40);

g.fillRect(cometcoor4,cometcoor44,40,40);

g.drawRect(cometcoor4,cometcoor44-10, 40,40);

g.fillRect(cometcoor4,cometcoor44-10,40,40);

g.drawRect(cometcoor4,cometcoor44+10, 40,40);

g.fillRect(cometcoor4,cometcoor44+10,40,40);

g.drawRect(cometcoor4-10,cometcoor44, 40,40);

g.fillRect(cometcoor4-10,cometcoor44,40,40);

g.drawRect(cometcoor4+10,cometcoor44, 40,40);

g.fillRect(cometcoor4+10,cometcoor44,40,40);

if (coorcounty4 == 0){

cometcheck4 = cometcoor44;

}

++coorcounty4;

// X = 500 and Y is changing... Comet starts on the right of the screen

if (county4 ==0){

g.drawImage(cometimage4,cometcoor4, cometcoor44, null);

--cometcoor4;

--cometcoor44;

if (cometcheck4 == cometcoor4){

county4 = 1;

cometcoor4 = (int)(Math.random() *500);

cometcoor44=500;

cometcheck4 = cometcoor4;

}

}

// Y = 500 and X is changing... Comet starts on bottom side of the screen

if (county4 == 1){

g.drawImage(cometimage4,cometcoor4, cometcoor44, null);

--cometcoor4;

--cometcoor44;

if (cometcoor44 == cometcheck4){

county4= 0;

cometcheck4 = cometcoor44;

cometcoor44=(int)(Math.random() *500);

cometcoor4 = 500;

}

}

g.setColor(Color.black);

g.setColor(Color.black);

g.drawRect(cometcoor3,cometcoor33, 40,40);

g.fillRect(cometcoor3,cometcoor33,40,40);

g.drawRect(cometcoor3,cometcoor33-10, 40,40);

g.fillRect(cometcoor3,cometcoor33-10,40,40);

g.drawRect(cometcoor3,cometcoor33+10, 40,40);

g.fillRect(cometcoor3,cometcoor33+10,40,40);

g.drawRect(cometcoor3-10,cometcoor33, 40,40);

g.fillRect(cometcoor3-10,cometcoor33,40,40);

g.drawRect(cometcoor3+10,cometcoor33, 40,40);

g.fillRect(cometcoor3+10,cometcoor33,40,40);

if (coorcounty3 == 0){

cometcheck3 = cometcoor3;

}

++coorcounty3;

// Y = 0 and Y is changing... Comet starts on the top of the screen

if (county3 ==0){

g.drawImage(cometimage3,cometcoor3, cometcoor33, null);

--cometcoor3;

++cometcoor33;

if (cometcheck3 == cometcoor33){

county3 = 1;

cometcoor33 = (int)(Math.random() *500);

cometcoor3=500;

cometcheck3 = cometcoor3;

}

}

// X = 500 and Y is changing... Comet starts on right side of the screen

if (county3 == 1){

g.drawImage(cometimage3,cometcoor3, cometcoor33, null);

--cometcoor3;

++cometcoor33;

if (cometcoor33 == cometcheck3){

county3= 0;

cometcheck3 = cometcoor3;

cometcoor3=(int)(Math.random() *500);

cometcoor33 = 0;

}

}

g.setColor(Color.black);

g.drawRect(cometcoor2-10,cometcoor22-10, 45,45);

g.fillRect(cometcoor2-10,cometcoor22-10,45,45);

g.drawRect(cometcoor2,cometcoor22-10, 45,45);

g.fillRect(cometcoor2,cometcoor22-10,45,45);

g.drawRect(cometcoor2,cometcoor22+10, 45,45);

g.fillRect(cometcoor2,cometcoor22+10,45,45);

g.drawRect(cometcoor2-10,cometcoor22, 45,45);

g.fillRect(cometcoor2-10,cometcoor22,45,45);

g.drawRect(cometcoor2+10,cometcoor22, 45,45);

g.fillRect(cometcoor2+10,cometcoor22,45,45);

if (coorcounty2 == 0){

if (count2 == 0){

cometcheck2 = cometcoor22;

}

if (count2 == 1){

cometcheck2 = cometcoor2;

}

}

++coorcounty2;

// X = 0 and Y is changing... Comet starts on left side of the screen

if (county2 ==0){

g.drawImage(cometimage2,cometcoor2, cometcoor22, null);

++cometcoor2;

++cometcoor22;

if (cometcheck2 == cometcoor2){

county2 = 1;

cometcoor2=(int)(Math.random() *500);

cometcheck2 = cometcoor2;

cometcoor22 = 0;

}

}

// Y = 0 and X is changing... Comet starts on top of the screen

if (county2 == 1){

g.drawImage(cometimage2,cometcoor2, cometcoor22, null);

++cometcoor2;

++cometcoor22;

if (cometcoor22 == cometcheck2){

county2 = 0;

cometcheck2 = cometcoor22;

cometcoor2=0;

cometcoor22 = (int)(Math.random() *500);

}

}

++counter;

if (count >= 240 && count <= 310)

{

checky=11;

}

}

}

catch (InterruptedException e)

{

stop();

}

}

public void paint(Graphics g)

{

if (m_fAllLoaded)

{

Rectangle r = g.getClipRect();

g.clearRect(r.x, r.y, r.width, r.height);

displayImage(g);

}

else

g.drawString("Loading images...", 10, 20);

}

public void start()

{

if (m_leetagain == null)

{

m_leetagain = new Thread(this);

m_leetagain.start();

}

}

public void stop()

{

if (m_leetagain != null)

{

m_leetagain.stop();

m_leetagain = null;

}

}

public void run()

{

m_nCurrImage = 0;

if (!m_fAllLoaded)

{

//repaint();

m_Graphics = getGraphics();

m_Images= new Image[NUM_IMAGES];

MediaTracker tracker = new MediaTracker(this);

String strImage;

for (int i = 1; i <= NUM_IMAGES; i++)

{

strImage = "images/img00" + ((i < 10) ? "0" : "") + i + ".gif";

m_Images[i-1] = getImage(getDocumentBase(), strImage);

tracker.addImage(m_Images[i-1], 0);

}

strImage2= "images/sun.gif";

m_Images2 = getImage(getDocumentBase(), strImage2);

strImage3="images/star.gif";

imagestar = getImage(getDocumentBase(), strImage3);

comet11="images/comet1.gif";

m_Imagescomet1 = getImage(getDocumentBase(), comet11);

comet22="images/comet2.gif";

m_Imagescomet2 = getImage(getDocumentBase(), comet22);

comet33="images/comet3.gif";

m_Imagescomet3 = getImage(getDocumentBase(), comet33);

comet44="images/comet4.gif";

m_Imagescomet4 = getImage(getDocumentBase(), comet44);

cometimage1=m_Imagescomet1;

cometimage2=m_Imagescomet2;

cometimage3=m_Imagescomet3;

cometimage4=m_Imagescomet4;

try

{

tracker.waitForAll();

m_fAllLoaded = !tracker.isErrorAny();

}

catch (InterruptedException e)

{

}

if (!m_fAllLoaded)

{

stop();

m_Graphics.drawString("Error loading images!", 10, 40);

return;

}

m_nImgWidth = m_Images[0].getWidth(this);

m_nImgHeight = m_Images[0].getHeight(this);

}

//repaint();

while (true)

{

try

{

if (checky == 111){

displayImage(m_Graphics);

}

else if (checky == 11)

{

//repaint ();

setBackground(Color.white);

g.setColor(Color.black);

g.drawString("YOU LOST!", 110,150);

}

m_nCurrImage++;

if (m_nCurrImage == NUM_IMAGES && checky == 111)

m_nCurrImage = 0;

Thread.sleep(10);

}

catch (InterruptedException e)

{

stop();

}

}

}

public boolean mouseDown(Event ext, int x, int y)

{

b = x;

c = y;

//repaint ();

return true;

}

public void lose ()

{

//repaint();

//g.drawString("YOU LOSE");

int y = 0;

//while (y == 0){

//repaint();

//}

}

}

679420phillip1492a at 2007-7-7 14:58:26 > top of Java-index,Archived Forums,Socket Programming...
# 4
1. Use code tags2. I would suggest splitting your code up into some more classes, this is not very OO.3. I think the problem is that you are calling the setBackground mehtod. Just draw a big white square.
642814zadoka at 2007-7-7 14:58:26 > top of Java-index,Archived Forums,Socket Programming...