The Artima Developer Community
Sponsored Link

Java Answers Forum
Applet problem !!!

5 replies on 1 page. Most recent reply: Aug 1, 2002 7:21 AM by thomas

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 5 replies on 1 page
Lepi

Posts: 2
Nickname: bora
Registered: Jul, 2002

Applet problem !!! Posted: Jul 29, 2002 4:33 PM
Reply to this message Reply
Advertisement
Hi everyone,
I?m supposed to write a methos squareOfAsterisks that displays a solid square of asterisks whose side specified in integer parameter sizeValue. For example, if sizeValue is 4, the method displays:
****
****
****
****
I should incorporate this method into an applet that reads an integer value for sizeValue from the user at the keyboard and performs the drawing with the squareAsterisks method. The method should be called from the applet?s paint method and should be passed the Graphics object from paint. Below is a code that compiles but it doesn?t draw anything because the user doesn?t get a chance to type in the value of size of the square. Anyone can help? Appreciate.

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

public class SquareAsterisk extends JApplet implements ActionListener {

JLabel inputSize,showPattern;
JTextField size, pattern;
int inputValue;

public void init()
{
Container c=getContentPane();
c.setLayout(new FlowLayout());

inputSize=new JLabel("Enter the size of the square:");
size=new JTextField(5);
size.addActionListener(this);
c.add(inputSize);
c.add(size);
}

public void actionPerformed(ActionEvent e)
{
inputValue=Integer.parseInt(e.getActionCommand());
}

public void paint(Graphics g)
{
squareOfAsterisks(g,inputValue);
}

public void squareOfAsterisks(Graphics g,int sizeValue)
{

for(int x = 0; x < sizeValue; x++)
for(int y = 0; y < sizeValue; y++)
g.drawString("*", x * 16 + 10, y * 16 + 50);
}

}


Jonathan Paul

Posts: 5
Nickname: jona
Registered: Jul, 2002

Re: Applet problem !!! Posted: Jul 30, 2002 12:49 AM
Reply to this message Reply
Well, it seems that u r trying to draw through the paint function. As far as i know, the paint function is only called when the applet is initially loaded or whenever refreshing is needed. So, it is better for you to make a new function that takes the same parameters and draw the star-square there. And remember to call the fucntion after taking in the vlaue from the user.

Lepi

Posts: 2
Nickname: bora
Registered: Jul, 2002

Re: Applet problem !!! Posted: Jul 30, 2002 6:21 AM
Reply to this message Reply
I figure out something but new problem arises...
I?ll post you 2 almost identical codes:

import java.awt.*;
import java.applet.Applet;
import java.awt.event.*;

public class Square extends Applet implements ActionListener {
Label label;
TextField number;
int num;

public void init()
{
label = new Label( "Enter an integer:" );
number = new TextField( "0", 10 );
number.addActionListener( this );
add( label );
add( number );
}

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

public void squareOfAsterisks ( Graphics h )
{
int xpos, ypos=50;
for ( int i = 1; i <= num; i++ ) {
xpos = 50;
for ( int j = 1; j <= num; j++ ) {
h.drawString( "*", xpos, ypos );
xpos += 15;
}
ypos += 15;
}
}



public void actionPerformed( ActionEvent e )
{
num = Integer.parseInt( number.getText() );
repaint();
}
}
AND

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

public class Square extends JApplet implements ActionListener {
JLabel label;
JTextField number;
int num;

public void init()
{
Container c=getContentPane();
c.setLayout(new FlowLayout());
label = new JLabel( "Enter an integer:" );
number = new JTextField( "0", 10 );
number.addActionListener( this );
c.add( label );
c.add( number );
}

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

public void squareOfAsterisks ( Graphics h )
{
int xpos, ypos=50;
for ( int i = 1; i <= num; i++ ) {
xpos = 50;
for ( int j = 1; j <= num; j++ ) {
h.drawString( "*", xpos, ypos );
xpos += 15;
}
ypos += 15;
}
}



public void actionPerformed( ActionEvent e )
{
num = Integer.parseInt( number.getText() );
repaint();
}
}

Now, my question is: Why does the first one work while the second one doesn?t when these two are almost the same?

Jay Kandy

Posts: 77
Nickname: jay
Registered: Mar, 2002

Re: Applet problem !!! Posted: Jul 31, 2002 6:15 AM
Reply to this message Reply
One is an AWT applet and the other is a Swing applet. Their functionalities are the same and both work. Did you try in appletviewer?

thomas

Posts: 42
Nickname: turbomanic
Registered: Jul, 2002

Re: Applet problem !!! Posted: Jul 31, 2002 3:03 PM
Reply to this message Reply
you may have to use repaint() in the method actionPerformed(). It may not work because I not a hundred percent sure.

thomas

Posts: 42
Nickname: turbomanic
Registered: Jul, 2002

Re: Applet problem !!! Posted: Aug 1, 2002 7:21 AM
Reply to this message Reply
The only thing I can see is it must deal with the use of the container since all the rest is the same. try adding the buttons by add() not c.add() if this works then check back over the container methods. It has to be a simple mishap somewhere.

Flat View: This topic has 5 replies on 1 page
Topic: Help !!!! Dynamic dependent dropdown list using servlet Previous Topic   Next Topic Topic: applet to application

Sponsored Links



Google
  Web Artima.com   

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