小程序不画 [英] Applet not painting

查看:19
本文介绍了小程序不画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 Applet 出现问题,无法绘制所需的图形.

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.

如何解决这个问题?

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

}

推荐答案

根据我的建议,在 JPanel 或 JComponent 的paintComponent 方法中进行所有绘图.仅在需要时获取图像的 Graphics 对象,并在使用完后处理它.例如:

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

}

这篇关于小程序不画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆