Hot questions for Using Applets in graphics2d

Question:

I'm making a 2D Java Game using JApplet. For the Graphics I obviously use Graphics respectively Graphics2D. My Problem: I want to display an image in my paintComponent(Graphics g) method. The picture is located in the same directory as the HTML5-File (in which the Applet is implemented) and the class files. Without the following code snippet, the applet works fine (except there's no image, obviously). But as soon as I add it to the class, the Applet doesn't start (in the Browser!). It won't display anything, and I don't get an exception.

Code:

Graphics2D gg = (Graphics2D) g;
Image img = getImage(getDocumentBase(), "img.PNG");
gg.drawImage(img, 70, 50, this);
gg.finalize();

Please note that I have Java Version 8 Update 51 installed and that I have the URL of the HTML5-File on my whitelist in the Java Control Panel (So I won't get a security-error). I tried the applet in Firefox and IE.


Answer:

  1. Never read in an image from within a painting method such as paint or paintComponent. Even if this were successful (it's not in your case), it will needlessly result in re-reading the image in multiple times and slowing down your program's perceived responsiveness -- the last thing that you want to do.
  2. Instead read the image in once and store it in a variable.
  3. Your problem has to be that you're not looking for the image in the right place -- with the right path. I recommend that you obtaine your Image as a resource from a URL or InputStream and use ImageIO.read(...) to read it in. You will need to find the correct path to the image, something hard for us to guess given your current question, but you will want to use a path relative to your class file locations.
  4. This, gg.finalize();, is dangerous code and should not be there; get rid of this line.

Question:

I was wondering how I would use something like a bufferstrategy in a Java Applet.

I want to draw something like an image

gfx.drawImage(frame, 0, 0, width, height, null);

to my applet but I am getting white flashes due to the graphics drawing to the screen not being buffered.

Any help would be greatly appreciated!

Edit: I have a thread that is repainting this image

Edit 2: The image is constantly changing so I need to repaint it each time it changes


Answer:

Ah, so the public void update(Graphics g) method doesn't have white flashes due to the graphics drawing to the screen not being buffered. But the paint method does. Odd.

Question:

This code displays the logo, but there's no rotation. Am I missing something? I've been trying to rotate for a long time. Everything displays fine, and the calculations are correct, but the image displays as a normal image

    import javax.swing.JApplet;
import java.awt.Graphics2D;
import java.awt.Graphics;
import java.applet.Applet;
import java.awt.Font;
import java.awt.image.*;
import java.awt.Image;
import java.awt.geom.*;
import java.awt.Color;
import java.io.*;
import javax.imageio.ImageIO;


public class Display extends Applet
{
static Image logo;
static double rotation;


public void init()
{
    logo = getImage( getCodeBase() , "logo.jpg" );
    rotation = 90;
    setBackground(Color.WHITE);

}

public void paint(Graphics g)
{
    g.drawImage( rotateGear(this), 0 , 0 , this);

}

public static Image rotateGear(ImageObserver o)
{
    BufferedImage bf = new BufferedImage( 400 , 400 , BufferedImage.TYPE_INT_ARGB );
    Graphics2D g2d = bf.createGraphics();
    g2d.drawImage(logo, 0, 0, o);
    g2d.setColor(Color.red);
    g2d.fillRect(390, 390, 10, 10);
    AffineTransform at = new AffineTransform();
    at.rotate(rotation, 178, 178);
    g2d.setTransform(at);
    System.out.println( Math.toRadians(rotation));
    return bf;


}
}

Answer:

Because you're setting the transform after you have drawn the image. Do it the other way around!

(I tried it and your code works fine otherwise.)

Question:

I am following a tutorial on applets on Youtube. My code looks exactly the same as the tutorial's does, but the background does not turn pink and Eclipse tells me there are errors in implements MouseListener and g2.draw(line); What did I do wrong? Click here for the video and here is my code:

package applets1;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;

import javax.swing.JApplet;

public class clean extends JApplet implements MouseListener{

public void start(){



}

public void init(){

    setBackground(Color.pink);
    addMouseListener(this);

}

public void paint(Graphics g){

    Graphics g2D = (Graphics2D) g;

    g.drawString("WAZZUP", 100, 90);
    g.drawRect(100, 100, 400, 400);

    Point2D.Double topLeft = new Point2D.Double(0.0, 25.0);
    Point2D.Double topRight = new Point2D.Double(100.0, 25.0);

    Line2D.Double line = new Line2D.Double(topLeft, topRight);

    g2D.draw(line);

}
}

EDIT: The error at g2D.draw(line); says The method draw(Line2D.Double) is undefined for the type Graphics. I changed g2D.drawLine to g2D.draw I also fixed the implements typo. The background is still not pink, despite the absence of an error and everything else works. What can I do to fix the pinkness and g2D.draw?


Answer:

You have a typographical error. implements not implemets:

public class clean extends JApplet implements MouseListener{


Also you have declared g2D with the wrong type (Graphics vs Graphics2D). In other words, instead of Graphics g2D = (Graphics2D) g; you need to use Graphics2D g2D = (Graphics2D) g;

Once you make the above change, you will be able to invoke the g2D.draw() methods using the various 2D classes as parameters.


Also you have overridden the paint() method but you have not included a call to super.paint() - this should be the first line in your paint() method. Once you do this the background color should be rendered correctly (because it is handled by the base class, JApplet)