The Artima Developer Community
Sponsored Link

Java Answers Forum
Method problems

4 replies on 1 page. Most recent reply: Sep 23, 2006 3:06 PM by John M

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 4 replies on 1 page
John M

Posts: 3
Nickname: temp
Registered: Jun, 2006

Method problems Posted: Jun 3, 2006 12:23 PM
Reply to this message Reply
Advertisement
Hello,

I am working with the following code for an assignment:

import java.awt.*;
import java.applet.*;
 
public class Mystery5 extends Applet {
  Image Buffer;
  Graphics gBuffer;
 
  public void init() {
    Buffer=createImage(getSize().width, getSize().height);
    gBuffer=Buffer.getGraphics();
  }
 
  public boolean MouseDown(Event evt,int x,int y) {
    drawRB(x,y);
    repaint();
    return true;
  }
 
  public void drawRB(int x, int y) {
    int red=(int)(Math.random()*255);
    int green=(int)(Math.random()*255);
    int blue=(int)(Math.random()*255);
    Color randomColor=new Color(red, green, blue);
    gBuffer.setColor(randomColor);
    int diameter=(int)(Math.random()*90)+10;
    gBuffer.fillOval(x-diameter/2,y-diameter/2,diameter,diameter);
  }
 
  public void update(Graphics g) {
    paint(g);
  }
 
  public void paint(Graphics g) {
    gBuffer.setColor(Color.black);
    gBuffer.drawString("Click the applet to go!", 50,20);
    g.drawImage (Buffer,0,0, this);
  }
}


The assignment simply says to implement the following code and determine what it does.

The code had some deprication errors before I began working with it and I worked all of those out. The program now compiles but nothing happens when ran with the following html file

<HTML>
<HEAD>
<TITLE>Mystery5</TITLE>
</HEAD>
<BODY>
<p>The Mystery5 program...</p>
<APPLET CODE="Mystery5.class" WIDTH=500 HEIGHT=300>
</APPLET>
</BODY>
</HTML>


The "Click the applet to go!" text appears but once clicked nothing happens. Any advice would be greatly appreciated.

Thank you.


Sanjay Perera

Posts: 42
Nickname: angel6
Registered: Apr, 2004

Re: Method problems Posted: Jun 8, 2006 12:15 AM
Reply to this message Reply
I modified the code and now it works fine. Sorry I couldn't format the code as I was very busy.

Note: Problem was that you haven't registered the applet with a MouseListener.

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;

public class Mystery5 extends Applet implements MouseListener{
Image Buffer;
Graphics gBuffer;

public void init() {
Buffer=createImage(getSize().width, getSize().height);
gBuffer=Buffer.getGraphics();
addMouseListener(this);
}

public boolean MouseDown(int x,int y) {
drawRB(x,y);
repaint();
return true;
}

public void mouseClicked(MouseEvent e){
MouseDown( e.getX(),e.getY());
}
public void mouseEntered(MouseEvent e){};
public void mouseExited(MouseEvent e){};
public void mousePressed(MouseEvent e){};
public void mouseReleased(MouseEvent e){};


public void drawRB(int x, int y) {
int red=(int)(Math.random()*255);
int green=(int)(Math.random()*255);
int blue=(int)(Math.random()*255);
Color randomColor=new Color(red, green, blue);
gBuffer.setColor(randomColor);
int diameter=(int)(Math.random()*90)+10;
gBuffer.fillOval(x-diameter/2,y-diameter/2,diameter,diameter);
}

public void update(Graphics g) {
paint(g);
}

public void paint(Graphics g) {
gBuffer.setColor(Color.black);
gBuffer.drawString("Click the applet to go!", 50,20);
g.drawImage (Buffer,0,0, this);
}
}

John M

Posts: 3
Nickname: temp
Registered: Jun, 2006

Re: Method problems Posted: Sep 23, 2006 12:38 PM
Reply to this message Reply
First, my appologies for the delayed response.

Second, thank you very much. Your advice about implimenting the MouseListener solved my problem perfectly.

Thank you again.

Boston Thornton

Posts: 8
Nickname: preston
Registered: Sep, 2006

Re: Method problems Posted: Sep 23, 2006 2:15 PM
Reply to this message Reply
Right now, it doesn't do much. Does the colors which has been set present itself in the web browser? It looks like the code builds the presentation layer (The user friendly GUI view). The MouseDown indicates that addition code will be added to carry out some function at a later point. But again, someone else may have more insight.

John M

Posts: 3
Nickname: temp
Registered: Jun, 2006

Re: Method problems Posted: Sep 23, 2006 3:06 PM
Reply to this message Reply
You can either run the program via a web browser to see the outcome of the program or use the appletviewer command via the command prompt.

Either way you will need an html file:

<HTML>
<HEAD>
<TITLE>Mystery5</TITLE>
</HEAD>
<BODY>
<p>The Mystery5 program...</p>
<APPLET CODE="Mystery5.class" WIDTH=500 HEIGHT=500>
</APPLET>
</BODY>
</HTML>

Flat View: This topic has 4 replies on 1 page
Topic: Mythical Man Month question Previous Topic   Next Topic Topic: SCJP question

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use