1

我仍在尝试让 repaint() 方法在具有扩展 JComponent 的类的单独类中工作。我在这里放了几个帖子,到目前为止,我还无法让代码正常工作。我得到了一些很好的建议。我的位置低于我目前所拥有的。

主要课程 1:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

public class DDHGenericFrame extends JFrame {
        private static final long serialVersionUID = 1L;
        DDHGenericPanel d = new DDHGenericPanel(); /*User defined class that is above*/

        public DDHGenericFrame() {
            initUI();
        }

        public final void initUI() {
            add(d);//Adds the panel to the JFrame
            setSize(650,350);
            setTitle("Lines");
            setLocationRelativeTo(null);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }

        public static void main(String[] args) {
            DDHGenericFrame ex = new DDHGenericFrame();
            ex.setVisible(true);
        }
}

第 2 类:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.AffineTransform;
import java.awt.geom.Ellipse2D;
import java.awt.image.BufferedImage;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JPanel;

class DDHGenericPanel extends JPanel {
    private static final long serialVersionUID = 1L;
      public JButton aButton1;
      public JButton aButton2;
      public TestPane tPane = new TestPane();

    DDHGenericPanel(){
        System.out.println("DDH Generic JPanel");

          aButton1 = new JButton();
          aButton1.setText("Button 1");
          aButton1.addActionListener(new myButtonActionListener1());
          add(aButton1);

          aButton2 = new JButton();
          aButton2.setText("Button 2");
          aButton2.addActionListener(new myButtonActionListener2());
          add(aButton2);

          System.out.println("Before the setDraw!!!");
          tPane.setDraw(); 
          System.out.println("After the setDraw!!!");
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        System.out.println("paintComponent of DDHGenericPanel.java");
    }
}   

class myButtonActionListener1 implements ActionListener {
    public TestPane tPane = new TestPane();

    @Override
    public void actionPerformed(ActionEvent arg0) {
         System.out.println("Button 1 -- Before the setDraw!!!");
         tPane.setDraw(); 
         System.out.println("Button 1 -- After the setDraw!!!");
    }
}

class myButtonActionListener2 implements ActionListener {
    @Override
    public void actionPerformed(ActionEvent arg0) {
          System.out.println("Button1 clicked 2");
    }
}

第 3 类:(我将这个嵌入到与上面的类相同的文件中——当我完成代码时它将是分开的)

/**
 * This class will draw a cricle with the repaint method
 * @author DDH
 */
class TestPane extends JComponent {
    private static final long serialVersionUID = 1L;
    private static final int LINE_THICKNESS = 4;
      private static final int LINE_GAP = 10;
    private Color lineColor = Color.red;

      /**
      * This method will draw the circle with coordinated (0,0)
      * @param none
      * @return none
      */
      public void setDraw() {
          repaint();//This should call the paintComponent() that is below and paint a circe but it does not for some reason.
      }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);

        int radius = 10;
        BufferedImage buffer = new BufferedImage(radius, radius, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2d = buffer.createGraphics();
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.setRenderingHint        (RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BILINEAR);

        Ellipse2D circle = new Ellipse2D.Float(0, 0, radius,radius);
        Shape clip = g2d.getClip();
        g2d.setClip(circle);
        AffineTransform at = g2d.getTransform();

        g2d.setTransform(AffineTransform.getRotateInstance(
                                                    Math.toRadians(45),
                                                    radius / 2, radius / 2));

        int gap = LINE_GAP;

        g2d.setColor(Color.WHITE);
        g2d.fill(circle);

        g2d.setColor(lineColor);
        //g2d.setStroke(new BasicStroke(LINE_THICKNESS));
        for (int index = 0; index < 10; index++) {
            int x1 = index*gap-(LINE_THICKNESS/2);
            int y1 = 0;
            int x2 = index*gap+(LINE_THICKNESS/2);
            int y2 = radius;
            int width = x2 - x1;
            int height = y2 - y1;

            g2d.fillRect(x1, y1, width, height);
            //g2d.drawLine(index * gap, 0, index * gap, getRadius());
        }

        g2d.setTransform(at);
        g2d.setClip(clip);
        g2d.dispose();
        g.drawImage(buffer, 0, 0, this);
    }

}

从我读过的内容和人们发布的内容来看,这应该有效。有没有办法强制它立即绘画。Repaint() 有时会有一点延迟。我想用它作为游戏的开始,我必须能够创建一个圆圈的 ArrayList,然后立即重新绘制它们。目前这只会在顶部 (0,0) 坐标中绘制一个圆圈。

道格·戴恩斯·豪夫

4

1 回答 1

3

有没有办法强制它立即绘画。

一旦 GUI 可见,它将立即绘制。您不需要做任何特别的事情。不需要 setDraw() 方法。显示 GUI 时将自动绘制所有组件。

      System.out.println("Before the setDraw!!!");
      tPane.setDraw(); 
      System.out.println("After the setDraw!!!");

该代码什么也不做。GUI 尚不可见,因此没有可绘制的内容。除非您在可见 GUI 上实际更改组件的属性,否则没有理由调用重绘。

public void setDraw() {
      repaint();
  }

没有理由创建一个简单地执行 repaint() 的方法,摆脱这个方法。这不是我在你上一篇文章中建议的。我说您创建一个方法来更改将影响组件绘制结果的属性。

我给你举了一个例子,比如当你使用 setForeground() 时,该方法会改变要绘制的文本的颜色,所以当颜色改变时会自动调用 repaint()。

去掉你的paint组件中所有复杂的绘制代码,然后尝试做一个简单的

graphics.drawString();

不要玩旋转和剪辑(即使我对这些概念有疑问,如果做得不正确,你可能不会得到任何东西),直到你得到一些基本的工作。然后,一旦你开始工作,你就会做一些更复杂的事情,一次一步,直到你了解基础知识。在你得到一些简单的工作之前,不要编写复杂的程序。

另外,我不知道您为什么要尝试从缓冲图像中绘制。只需使用传递给 paintComponent() 方法的 Graphics 对象进行绘制。不需要使用 BufferedImage,Swing 已经是双缓冲的,所以你只是让你的代码复杂化了。

你读过自定义绘画教程了吗?它包含一个工作示例。

编辑:

说了以上所有你仍然有两个基本问题:

  1. 您没有将组件添加到面板
  2. 该组件没有首选尺寸,因此无需绘制任何内容。您需要重写 getPreferredSize() 方法以返回要绘制的组件的合理大小。

即使这两个修复也不能解决你复杂绘画的问题,但至少现在我可以得到一个简单的 drawstring(...) 来工作。

于 2013-11-17T01:27:11.357 回答