小程序不画画

时间:2012-02-29 18:25:08

标签: java applet paint

我的Applet没有绘制所需图形的问题。

出于某种原因,它会更新图形中的颜色,因为您可以在打印时看到它,它识别出鼠标正在移动,图形不为空,但它仍然拒绝绘制。

如何解决这个问题?

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();
}

}

2 个答案:

答案 0 :(得分:3)

根据我的建议,在JPanel或JComponent的paintComponent方法中进行所有绘制。只在需要时获取图像的Graphics对象,并在完成后对其进行处理。例如:

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();
      }
   }

}

答案 1 :(得分:2)

“不要在顶层容器中绘画”仍然需要注意,但它不是问题的直接来源。试试这个更简单的代码。

// <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();
}
}