小程序不画 [英] Applet not painting

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

问题描述

我有我的小程序不画了所需的图形的问题。

由于某些原因,它更新着色的图形,你可以看到这一点,打印时,它识别鼠标正在移动,图形是不为空,但它仍然拒绝作画。

这怎么能解决吗?

 进口java.awt中的*。
进口java.awt.event.MouseEvent中;
进口java.awt.event.MouseListener;
进口java.awt.event.MouseMotionListener;进口javax.swing.JApplet中;公共类GameApplet扩展JApplet的实现的MouseListener,{的MouseMotionListener/ **
 *
 * /
私有静态最后的serialVersionUID长= -6796451079056583597L;
Graphics2D的克;
形象画像;
点P =新的点(-100,-100);公共无效的init(){
    的init(1000,900);
}公共无效的init(INT X,int y)对{
    的setSize(X,Y);
    图像=的createImage(X,Y);
    G =(Graphics2D的)image.getGraphics();
    //if(Graphic.setGraphic(image.getGraphics())){
    如果(G!= NULL)
        的System.out.println(图形制作);
    //}
    g.setColor(Color.GREEN);
    g.fillRect(0,0,X,Y);
    的System.out.println(G +,+(G =空)!);
    addMouseListener将(本);
    addMouseMotionListener(本);
    //Graphic.paint();
    调用setVisible(真);
}@覆盖
公共无效漆(图形G){
    g.setColor(Color.red);
    g.fillRect(0,0,500,500);
}公共无效更新(图形G){
    油漆(G);
}@覆盖
公共图形的getGraphics(){
    返回克;
}@覆盖
公共无效的mouseClicked(的MouseEvent E){
    的System.out.println(E);
}@覆盖
公共无效鼠标pressed(的MouseEvent E){
    p值= e.getPoint();
    重绘();
}@覆盖
公共无效的mouseReleased(的MouseEvent E){}@覆盖
公共无效的mouseEntered(的MouseEvent E){}@覆盖
公共无效的mouseExited(的MouseEvent E){}@覆盖
公共无效的mouseDragged(的MouseEvent E){}@覆盖
公共无效的mouseMoved(的MouseEvent E){
    showStatus(e.toString());
    g.fillOval(e.getPoint()× - 5,e.getPoint()Y - 5,10,10);
    p值= e.getPoint();
    重绘();
}}


解决方案

按我的建议,你都在一个JPanel或者JComponent中的paintComponent方法绘制。获取图像的图形对象只有当你需要它,当你用它做处置。例如:

 进口java.awt中的*。
进口java.awt.event.MouseAdapter;
进口java.awt.event.MouseEvent中;
进口java.lang.reflect.InvocationTargetException;进口的javax.swing *。@燮pressWarnings(串行)
公共类GameApplet2扩展JApplet的{
   受保护的静态最终诠释APP_WIDTH = 1000;
   受保护的静态最终诠释APP_HEIGHT = 900;   @覆盖
   公共无效的init(){
      尝试{
         SwingUtilities.invokeAndWait(新的Runnable(){
            公共无效的run(){
               GameAppletPanel面板=新GameAppletPanel(GameApplet2.this);
               。的getContentPane()加(面板);
               panel.init(APP_WIDTH,APP_HEIGHT);
               的setSize(APP_WIDTH,APP_HEIGHT);
            }
         });
      }赶上(InterruptedException的E){
         e.printStackTrace();
      }赶上(的InvocationTargetException E){
         e.printStackTrace();
      }
   }
}@燮pressWarnings(串行)
类GameAppletPanel继承JPanel {
   形象画像;
   点P =新的点(-100,-100);
   私人JApplet的小程序;   公共GameAppletPanel(JApplet的小程序){
      this.applet =小程序;
   }   公共无效的init(){
      的init(1000,900);
   }   公共无效的init(INT X,int y)对{
      的setSize(X,Y);
      图像=的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 =空)!);
      MyMouseAdapter mmAdapter =新MyMouseAdapter();
      addMouseListener将(mmAdapter);
      addMouseMotionListener(mmAdapter);
   }   @覆盖
   保护无效paintComponent(图形G){
      super.paintComponent方法(G);
      g.drawImage(图像,0,0,这一点);
      g.setColor(Color.red);
      g.fillRect(0,0,500,500);
   }   私有类MyMouseAdapter扩展MouseAdapter {      @覆盖
      公共无效的mouseClicked(的MouseEvent E){
         的System.out.println(E);
      }      @覆盖
      公共无效鼠标pressed(的MouseEvent E){
         p值= e.getPoint();
         重绘();
      }      @覆盖
      公共无效的mouseMoved(的MouseEvent E){
         applet.showStatus(e.toString());
         Graphics2D的G2 =(Graphics2D的)image.getGraphics();
         g2.fillOval(e.getPoint()× - 5,e.getPoint()Y - 5,10,10);
         p值= e.getPoint();
         g2.dispose();
         重绘();
      }
   }}

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

}

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

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