Java GUI窗口显示垃圾

时间:2015-04-02 15:48:15

标签: java swing paintcomponent

我写了一个Java程序来取一个三角形,根据用户执行的按钮点击,旋转,移动或旋转并移动它。

事先,我指示用户输入逻辑坐标范围,以确定像素坐标将如何映射到实际的x-y坐标系。

最初,我的三角形出现在屏幕中间,单击一个按钮后,三角形会在其上执行某个操作后显示(即旋转,移动等)

然而,在操作完成并重绘三角形后,我看到JPanel左上角也绘制了一个输入框。

我不确定这是如何被吸引到那里的。

代码:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class RotateAndShiftTriangles extends JFrame {
    public static void main(String[] args) { new RotateAndShiftTriangles(); }

   RotateAndShiftTriangles() {
       super("Drawing 50 Triangles");

       final JPanel drawingPanel = new DrawTriangles();
       JPanel buttonPanel = new JPanel();
       JButton rotate = new JButton("Rotate"),
               shift = new JButton("Shift"),
               rotateShift = new JButton("Rotate and Shift"),
               reset = new JButton ("Reset");

       drawingPanel.setBackground(Color.WHITE);

       buttonPanel.add(rotate);
       buttonPanel.add(shift);
       buttonPanel.add(rotateShift);
       buttonPanel.add(reset);

       addWindowListener(new WindowAdapter() {
           public void windowClosing(WindowEvent e) {
               System.exit(0);
           }
       });

       DrawTriangles.rWidth = Float.parseFloat(JOptionPane.showInputDialog("Input rWidth"));
       DrawTriangles.rHeight = Float.parseFloat(JOptionPane.showInputDialog("Input rHeight"));

       rotate.addActionListener(new ActionListener() {

           public void actionPerformed(ActionEvent e) {
               DrawTriangles.rotate = true;
               drawingPanel.repaint();
           }
       });

       shift.addActionListener(new ActionListener() {

           public void actionPerformed(ActionEvent e) {
               DrawTriangles.shift = true;
               drawingPanel.repaint();
           }
       });

       rotateShift.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                DrawTriangles.rotate = true;
                DrawTriangles.shift = true;
                drawingPanel.repaint();
            }
       });

       reset.addActionListener(new ActionListener() {

        @Override
            public void actionPerformed(ActionEvent e) {
                DrawTriangles.reset = true;
                drawingPanel.repaint();
            }
       });

       setSize(600, 400);
       add("South", buttonPanel);
       add("Center", drawingPanel);
       setVisible(true);
   }
}

class DrawTriangles extends JPanel {
    static float rWidth, rHeight, pixelSize;
    static int maxX, maxY, minMaxXY, centerX, centerY;
    static boolean rotate = false, shift = false, reset = false;
    float angle = 0;

    void initialize() {
        Dimension d = getSize();
        maxX = d.width - 1; maxY = d.height - 1;
        pixelSize = Math.max(rWidth / maxX, rHeight / maxY);
        minMaxXY = Math.min(maxX, maxY);
        centerX = maxX/2; centerY = maxY/2;
    }

    public int iX2(float x) { return Math.round(x); }
    public int iY2(float y) { return maxY - Math.round(y); }
    public static int iX(float x) { return Math.round(centerX + x / pixelSize); }
    public static int iY(float y) { return Math.round(centerY - y / pixelSize); }
    public static float fx(int x) { return (x - centerX) * pixelSize; }
    public static float fy(int y) { return (centerY - y) * pixelSize; }

    public void paint(Graphics g) {
        super.paintComponent(g);
        initialize();

        int left = iX(-rWidth/2), right = iX(rWidth/2);
        int top = iY(rHeight/2), bot = iY(-rHeight/2);

        g.drawString("X: " + -rWidth/2 + " Y: " + rHeight/2, left, top + 10);
        g.drawString("X: " + rWidth/2 + " Y: " + rHeight/2, right - 55, top + 10);
        g.drawString("X: " + -rWidth/2 + " Y: " + -rHeight/2, left, bot);
        g.drawString("X: " + rWidth/2 + " Y: " + -rHeight/2, right - 55, bot);

        g.setColor(Color.BLUE);
        g.drawRect(left, top, right - left, bot - top);

        float side = 0.95f * minMaxXY, sideHalf = 0.5F * side,
                h = sideHalf * (float)Math.sqrt(3),
                xA, yA, xB, yB, xC, yC, 
                xA1, yA1, xB1, yB1, xC1, yC1, p, q;

        q = 0.05F;
        p = 1 - q;

        xA = centerX - sideHalf;
        yA = centerY - 0.5F * h;
        xB = centerX + sideHalf;

        yB = yA;

        xC = centerX;
        yC = centerY + 0.5F * h;

        if(!reset) {
            if(rotate) {
                angle += Float.parseFloat(JOptionPane.showInputDialog("Input Angle of Rotation (in degrees)"));
                float xR = fx(Integer.parseInt(JOptionPane.showInputDialog("Input X Coordinate for Rotation"))),
                        yR = fx(Integer.parseInt(JOptionPane.showInputDialog("Input Y Coordinate for Rotation")));

                xA = rotateX(xA, yA, xR, yR, angle);
                yA = rotateY(xA, yA, xR, yR, angle);

                xB = rotateX(xB, yB, xR, yR, angle);
                yB = rotateY(xB, yB, xR, yR, angle);

                xC = rotateX(xC, yC, xR, yR, angle);
                yC = rotateY(xC, yC, xR, yR, angle);

                rotate = false;
            }
            if(shift) {
                float xShift = -fx(Integer.parseInt(JOptionPane.showInputDialog("Input X Coordinate for Shift"))),
                        yShift = -fx(Integer.parseInt(JOptionPane.showInputDialog("Input Y Coordinate for Shift")));

                xA += xShift; 
                yA += yShift;

                xB += xShift;
                yB += yShift;

                xC += xShift;
                yC += yShift;

                shift = false;
            }
        }

        g.setColor(Color.RED);
        for (int i = 0; i < 50; i++) {
            g.drawLine(iX2(xA), iY2(yA), iX2(xB), iY2(yB));
            g.drawLine(iX2(xB), iY2(yB), iX2(xC), iY2(yC));
            g.drawLine(iX2(xC), iY2(yC), iX2(xA), iY2(yA));

            if(i == 0) {
                g.setColor(Color.BLACK);
                g.drawString("A: X- " + xA + " Y- " + yA, 0, 50);
                g.drawString("B: X- " + xB + " Y- " + yB, 0, 60);
                g.drawString("C: X- " + xC + " Y- " + yC, 0, 70);
                g.setColor(Color.RED);
            }

            xA1 = p * xA + q * xB;
            yA1 = p * yA + q * yB;
            xB1 = p * xB + q * xC;
            yB1 = p * yB + q * yC;
            xC1 = p * xC + q * xA;
            yC1 = p * yC + q * yA;

            xA = xA1; xB = xB1; xC = xC1;
            yA = yA1; yB = yB1; yC = yC1;
        }
        if(reset)
            angle = 0;
        reset = false;
    }

    public float rotateX(float x, float y, float xR, float yR, float angle) {
        angle *= (Math.PI / 180.0);
        float c = (float)Math.cos(angle), s = (float)Math.sin(angle),
                xF = x - xR, yF = y - yR,
                rx = xF * c - yF * s;

        return rx + xR;
    }

    public float rotateY(float x, float y, float xR, float yR, float angle) {
        angle *= (Math.PI / 180.0);
        float c = (float)Math.cos(angle), s = (float)Math.sin(angle),
                xF = x - xR, yF = y - yR,
                ry = xF * s + yF * c;

        return ry + yR;
    }
}

我不断得到这个 Java GUI Garbage

4 个答案:

答案 0 :(得分:4)

public void paint(Graphics g) {
    super.paintComponent(g);

不知道这是否是唯一的问题,但自定义绘画是通过覆盖paintComponent()方法完成的:

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

编辑:

其他评论,与问题没有直接关系,但对于正确设计很重要:

add("South", buttonPanel);
add("Center", drawingPanel);

不要使用硬编码文字。布局管理器将提供您可以使用的变量。此外,不推荐使用这种形式的add(...)方法(阅读API)。新表格是:

add(buttonPanel, BordeLayout.PAGE_END);
add("Center", BorderLayout.CENTER);

不要使用静态方法和变量。如果您想更改班级的属性,请创建&#34; setter&#34;方法。例如,创建一个setter方法:

public void setRotate(Boolean rotate)
{
    this.rotate = rotate
    repaint();
} 

此外,并非setter方法调用repaint()方法。这是因为你的自定义类(不是使用该类的代码)应该负责进行重绘。

然后调用setter方法:

//DrawTriangles.rotate = true; // wrong
drawingPanel.setRotate(true);

答案 1 :(得分:4)

您正在{/ 1}} JOptionPane方法中触发<{1}}弹出

调用paint()及其兄弟姐妹应该限制自己重绘对象,没有别的。因此,您的代码将导致您的.paint()方法阻止,直到弹出窗口关闭,然后继续处理它停止的位置,可能仍然在屏幕上拾取工件。正如您在此处看到的那样,背景被绘制(通过调用.paint())然后弹出并关闭弹出窗口,然后运行super.paintComponent()方法的其余部分,但由于背景已经被绘制,没有什么可以重新描述弹出窗口的位置。

您应该移动代码:

.paint()

angle += Float.parseFloat(JOptionPane.showInputDialog("Input Angle of Rotation (in degrees)"));
float xR = fx(Integer.parseInt(JOptionPane.showInputDialog("Input X Coordinate for Rotation"))),
      yR = fx(Integer.parseInt(JOptionPane.showInputDialog("Input Y Coordinate for Rotation")));

进入相应的float xShift = -fx(Integer.parseInt(JOptionPane.showInputDialog("Input X Coordinate for Shift"))), yShift = -fx(Integer.parseInt(JOptionPane.showInputDialog("Input Y Coordinate for Shift"))); 方法,设置必要的值,然后在ActionListener方法中使用它们。


你应该对使用paint().paint()保持一致,就像@camickr建议的那样,没有一种方法可以调用它的兄弟超级。

答案 2 :(得分:2)

看起来只有在显示对话框时才会发生这种情况。我修改了代码并硬编了一些值,它没有问题。

if(!reset) {
        if(rotate) {
            angle += Float.parseFloat("15");
            float xR = fx(3),
                    yR = fx(3);
            // other stuff...
        }

我建议您尝试显示对话框并在重新绘制组件之前设置相应的值,类似于:

       shift.addActionListener(new ActionListener() {

       public void actionPerformed(ActionEvent e) {
           float xShift = -DrawTriangles.fx(Integer.parseInt(JOptionPane.showInputDialog("Input X Coordinate for Shift"))),
                   yShift = -DrawTriangles.fx(Integer.parseInt(JOptionPane.showInputDialog("Input Y Coordinate for Shift")));
           drawingPanel.xShift = xShift;
           drawingPanel.yShift = yShift;
           DrawTriangles.shift = true;
           drawingPanel.repaint();
       }
   });

答案 3 :(得分:0)

使用BufferedImage更正了绘图,但仍然发生了异常。

public void paint(Graphics gg) {
    BufferedImage bf = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_RGB);

    Graphics2D g = bf.createGraphics();

    g.setColor(Color.WHITE);
    g.fillRect(0, 0, getWidth(), getHeight());

...

    gg.drawImage(bf, 0, 0, null);

    if(reset)
        angle = 0;
    reset = false;
}