I am having issues with my Applet not painting the required graphics.
For some reason, it updates the coloring in the graphics, as you can see this when it prints, it recognizes the mouse is being moved, the graphics are not null, but it still refuses to paint.
How can this be fixed?
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.JApplet;
public class GameApplet extends JApplet implements MouseListener, MouseMotionListener{
/**
*
*/
private static final long serialVersionUID = -6796451079056583597L;
Graphics2D g;
Image image;
Point p = new Point(-100, -100);
public void init(){
init(1000, 900);
}
public void init(int x, int y){
setSize(x, y);
image = createImage(x, y);
g = (Graphics2D) image.getGraphics();
//if(Graphic.setGraphic(image.getGraphics())){
if(g != null)
System.out.println("Graphics made");
//}
g.setColor(Color.GREEN);
g.fillRect(0, 0, x, y);
System.out.println(g+", "+ (g != null));
addMouseListener(this);
addMouseMotionListener(this);
//Graphic.paint();
setVisible(true);
}
@Override
public void paint(Graphics g){
g.setColor(Color.red);
g.fillRect(0, 0, 500, 500);
}
public void update(Graphics g){
paint(g);
}
@Override
public Graphics getGraphics(){
return g;
}
@Override
public void mouseClicked(MouseEvent e) {
System.out.println(e);
}
@Override
public void mousePressed(MouseEvent e) {
p = e.getPoint();
repaint();
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
@Override
public void mouseDragged(MouseEvent e) {
}
@Override
public void mouseMoved(MouseEvent e) {
showStatus(e.toString());
g.fillOval(e.getPoint().x - 5, e.getPoint().y - 5, 10, 10);
p = e.getPoint();
repaint();
}
}
As per my recommendations, do all drawing in a JPanel or JComponent's paintComponent method. Get the image's Graphics object only when you need it and dispose of it when you're done with it. For example:
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.lang.reflect.InvocationTargetException;
import javax.swing.*;
@SuppressWarnings("serial")
public class GameApplet2 extends JApplet {
protected static final int APP_WIDTH = 1000;
protected static final int APP_HEIGHT = 900;
@Override
public void init() {
try {
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
GameAppletPanel panel = new GameAppletPanel(GameApplet2.this);
getContentPane().add(panel);
panel.init(APP_WIDTH, APP_HEIGHT);
setSize(APP_WIDTH, APP_HEIGHT);
}
});
} catch (InterruptedException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
}
@SuppressWarnings("serial")
class GameAppletPanel extends JPanel {
Image image;
Point p = new Point(-100, -100);
private JApplet applet;
public GameAppletPanel(JApplet applet) {
this.applet = applet;
}
public void init() {
init(1000, 900);
}
public void init(int x, int y) {
setSize(x, y);
image = createImage(x, y);
Graphics2D g = (Graphics2D) image.getGraphics();
g.setColor(Color.GREEN);
g.fillRect(0, 0, x, y);
g.dispose();
System.out.println(g + ", " + (g != null));
MyMouseAdapter mmAdapter = new MyMouseAdapter();
addMouseListener(mmAdapter);
addMouseMotionListener(mmAdapter);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(image, 0, 0, this);
g.setColor(Color.red);
g.fillRect(0, 0, 500, 500);
}
private class MyMouseAdapter extends MouseAdapter {
@Override
public void mouseClicked(MouseEvent e) {
System.out.println(e);
}
@Override
public void mousePressed(MouseEvent e) {
p = e.getPoint();
repaint();
}
@Override
public void mouseMoved(MouseEvent e) {
applet.showStatus(e.toString());
Graphics2D g2 = (Graphics2D) image.getGraphics();
g2.fillOval(e.getPoint().x - 5, e.getPoint().y - 5, 10, 10);
p = e.getPoint();
g2.dispose();
repaint();
}
}
}
The "don't paint in top-level containers" still needs attending to, but it was not the immediate source(s) of the problem. Try this simpler code.
// <applet code='GameApplet' width=400 height=200></applet>
import java.awt.*;
import java.awt.event.*;
import javax.swing.JApplet;
public class GameApplet extends JApplet
implements MouseListener, MouseMotionListener{
Point p = new Point(-100, -100);
public void init(){
addMouseListener(this);
addMouseMotionListener(this);
}
@Override
public void paint(Graphics g){
g.setColor(Color.red);
g.fillRect(0, 0, 500, 500);
g.setColor(Color.blue);
g.fillOval(p.x - 5, p.y - 5, 10, 10);
}
@Override
public void mouseClicked(MouseEvent e) {
System.out.println(e);
}
@Override
public void mousePressed(MouseEvent e) {
p = e.getPoint();
repaint();
}
@Override
public void mouseReleased(MouseEvent e) {}
@Override
public void mouseEntered(MouseEvent e) {}
@Override
public void mouseExited(MouseEvent e) {}
@Override
public void mouseDragged(MouseEvent e) {}
@Override
public void mouseMoved(MouseEvent e) {
showStatus(e.toString());
p = e.getPoint();
repaint();
}
}