【问题标题】:Java Repaint - JComponet needs to repaint the class when the repaint() is called from another classJava Repaint - 当从另一个类调用 repaint() 时,JComponet 需要重新绘制类
【发布时间】:2013-11-17 00:56:00
【问题描述】:

我仍在尝试让 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) 坐标中绘制一个圆圈。

道格·戴恩斯·豪夫

【问题讨论】:

  • 如需尽快获得更好的帮助,请发帖SSCCE

标签: java swing paintcomponent jcomponent preferredsize


【解决方案1】:

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

只要 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 已经是双缓冲的,所以你只是在复杂化你的代码。

你读过Custom Painting 教程了吗?它包含一个工作示例。

编辑:

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

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

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

【讨论】:

  • 当我设置组件的首选大小时它起作用了。它在我的按钮后添加了文本。我只需要能够知道组件在屏幕上的任何位置。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-06-27
  • 2012-11-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-30
相关资源
最近更新 更多