Hot questions for Using Applets in java 2d

Question:

I am trying to write an applet program in which a circle is drawn. The size of circle will increase if i press L and decrease if I press S.

import java.applet.Applet; 
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

public class oval extends Applet implements KeyListener{

    private int d=10;
    @Override
    public void init() {
        setSize(500,500);
        addKeyListener(this);
    }
    @Override
    public void paint(Graphics g) {
        super.paint(g);
        g.drawOval(100, 100, d, d);
    }
    @Override
    public void keyTyped(KeyEvent e) {   
    }
    @Override
    public void keyPressed(KeyEvent e) {
        if(e.getKeyChar()=='L')
            d=d+5;    
        else if(e.getKeyChar()=='S')
            d=d-5;
        repaint();
    }
    @Override
    public void keyReleased(KeyEvent e) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}

Answer:

EDIT: Sorry, i didn't read your code completely... You were infact using KeyEvent right but when you are comparing the chars you need to do lowercase l not uppercase:

if(e.getKeyChar() == 'l')

not

if(e.getKeyChar() == 'L')  //this will work if the user presses Shift+l

I just showed you another way of doing it, the way i have it set up will work for both lowercase l and uppercase l. So it isn't case sensitive.

You were not properly using KeyEvent to trigger the certain key. Here is your code tested and working:

import java.applet.Applet;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

public class oval extends Applet implements KeyListener
{
    private int d=10;

    @Override
    public void init() 
    {
        setSize(500,500);
        addKeyListener(this);
    }

    @Override
    public void paint(Graphics g) 
    {
        super.paint(g);
        g.drawOval(100, 100, d, d);
    }

    @Override
    public void keyTyped(KeyEvent e) 
    {
    }

    @Override
    public void keyPressed(KeyEvent e) 
    {
        if(e.getKeyCode()==KeyEvent.VK_L)
            d=d+5;    
        else if(e.getKeyCode()==KeyEvent.VK_S)
            d=d-5;
        repaint();
    }

    @Override
    public void keyReleased(KeyEvent e) 
    {

    }
}

I removed the error you were throwing through keyReleased event because i have no idea why that was there...

Question:

For a school project, I need to create an applet that produces a 10 x 10 grid in which each cell will change color in accordance to what some threads are doing in the background. I have all of the rest figured out, but I don't have the slightest clue as to how to display this grid. This is the only example code we were given:

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

public class Array2 extends Applet {
  private final ststic int LIMIT = 9;
  private int[][] results;

  public void init() {
    int count = 1;
    results = new int [LIMIT][LIMIT];

    for (int i = 0; i < LIMIT; i++) {
      for (int j = 0; j < LIMIT; j++) {
        results[i][j] = count % 2;
        count++;
      }
    }
  }

  public void paint (Graphics g) {
    int xLoc = 25;
    int yLoc = 25;

    for (int i = 0; i < LIMIT; i++) {
      for (int j = 0; j < LIMIT; j++) {
        g.drawString(Integer.toString(results[i][j]), xLoc. yLoc);
        xLoc += 20;
      }
      xLoc = 25;
      yLoc += 20;
    }
  }
}

This ends up printing a blank 2 x 2 grid. This is easy enough to modify into a 10 x 10. However, what I DON'T know how to do is color the squares. Everything I've searched mentions using jPanels or jFrames or something, but this HAS to be an applet. I was just looking for some suggestions as to what I should look into for the coloring process, as this is literally all I have to go on.Thanks!


Answer:

The applet draws with the class Graphics and passes you an instance in the paint method. You can use Graphics to do many cool things on the screen, so check its methods out! But to draw a colored square, first set the color using g.setColor(color) and then use g.fillRect(xLoc, yLoc, size, size) with xLoc and yLoc being the top-left coordinates of the square.

Question:

I have been doing some searching, and I have not found anything that suits my needs. I have a small game applet I am writing in Java. I am curious if there is a way for me to obtain the color value (preferably RGB format) of a pixel on the Applet Viewer screen. I am planning on using this to detect collisions. Is there a way to get the color values of a pixel on the Java Applet screen?


Answer:

  1. Create a BufferedImage, with the width/height of your applet.
  2. Create a Graphics2D object by calling createGraphics() on your BufferedImage object.
  3. Pass this Graphics2D object to the paint method of your applet - now the applet will be painted on your BufferedImage.
  4. Call dispose() on your Graphics2D object.
  5. Examine the pixel colors in your BufferedImage.

BTW, Andrew Thompson is probably right when he says that this must be an ineffective way of calculating collisions. Probably you should be looking into the intersects method of Rectangle. Or for best results use a game engine, they have support for collision detection.

Question:

Below is a simple Applet Code the problem is after for loop is finished.

Nothing is displayed on the applet screen.

I guess screen is cleared after for loop is finished.

I am unable to fix it I would like to know how to prevent the screen from clearing so that my output is there on the screen.

public class ColorArcs extends Applet
{
int width=50;
int length=50;

int topx=200-25,topy=200-25;

public void paint(Graphics g)
{
    for(;length<250;)
    {
        g.drawArc(200-length/2,200-width/2,length,width,0,180);

        length+=2;
        width++;

        if(length>=50&&length<=75)
            setForeground(Color.cyan);
        else
            if(length>=75&&length<=100)
            setForeground(Color.yellow);
        else
            if(length>=100&&length<=125)
            setForeground(Color.green);
        else
            setForeground(Color.red);

        try
        {
            Thread.sleep(80);
        }
        catch(InterruptedException ie){}
    }
}
}

Answer:

It is not getting cleared after for loop finished.