paintComponent 问题显示

paintComponent problems displaying

我在尝试显示我要绘制的三角形时遇到了麻烦。我查看了网络,发现我实施的许多选项都没有解决我的显示问题。我的按钮面板显示完美,但用于绘图的 JPanel 未被使用或未被绘制。你们能提供的任何帮助都会很棒我已经盯着了几天而且没有任何运气。我有更多的代码要实现,但我想在添加太多之前先了解我的代码 运行 的基本内容。这是我的代码,感谢您提前提供帮助。

    import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.geom.Point2D;

import javax.swing.*;

public class RotateAndShiftTriangles extends JPanel{

int rWidth, rHeight, centerX, centerY, prevMove = -1, maxX, maxY;
float pixelSize;
double radians;
static Point2D pA, pB, pC;

//Default constructor
RotateAndShiftTriangles(){
    rWidth = Integer.parseInt(JOptionPane.showInputDialog("Enter the rWidth of the panel: "));
    rHeight = Integer.parseInt(JOptionPane.showInputDialog("Enter the rHeight of the panel: "));
}

// main method of the program that all it really does is call the create method
public static void main(String [] argv){
    RotateAndShiftTriangles tri = new RotateAndShiftTriangles();
    tri.create();
}

public void create(){
    // Initializing graphics 
    JFrame win = new JFrame();
    Dimension d = new Dimension(800, 600);
    maxX = d.width - 1;
    maxY = d.height - 1;
    pixelSize = Math.max(rWidth/maxX, rHeight/maxY);
    centerX = maxX/2; centerY = maxY/2;
    win.setSize(d);
    win.setMinimumSize(d);
    win.setPreferredSize(d);
    win.getContentPane().setLayout(new BorderLayout());
    Point2D centerPoint = new Point2D.Double(centerX, centerY);
    JPanel draw = new JPanel();
    JPanel buttonPanel = new JPanel();
    JButton shiftButton = new JButton("Shift");
    JButton rotateButton = new JButton("Rotate");
    JButton shiftandRotateButton = new JButton("Shift and Rotate");
    JButton resetButton = new JButton("Reset");
    JButton backButton = new JButton("Back");

    // setting layout
    win.add(this);
    win.add(draw, BorderLayout.CENTER);
    buttonPanel.add(shiftButton);
    buttonPanel.add(rotateButton);
    buttonPanel.add(shiftandRotateButton);
    buttonPanel.add(resetButton);
    buttonPanel.add(backButton);
    win.add(buttonPanel, BorderLayout.SOUTH);
    win.pack();
    win.setLocationRelativeTo(win.getParent());

    // makes window visible and sets the default closing operations
    win.setVisible(true);
    win.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}

// Page 19 mappings to take the logical coords and change them to ints.
int iX(float x){
    return Math.round(centerX + x/pixelSize);
    }
int iY(float y){
    return Math.round(centerY - y/pixelSize);
    }

// Standard paintComponent method 
protected void paintComponent(Graphics g){
    super.paintComponent(g);
    g.setColor(Color.blue);
    g.drawRect(0, 0, maxX, maxY);
    float side = 0.95F * pixelSize, 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;
    pA.setLocation(xA, yA);
    pB.setLocation(xB, yB);
    pC.setLocation(xC, yC);

    for (int i=0; i<50; i++) 
    {  
        g.drawLine(iX(xA), iY(yA), iX(xB), iY(yB));
        g.drawLine(iX(xB), iY(yB), iX(xC), iY(yC));
        g.drawLine(iX(xC), iY(yC), iX(xA), iY(yA));
        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;
     } 
}

}

win.add(this);
win.add(draw, BorderLayout.CENTER);

您将三角形面板添加到 window。当您不指定约束时,它将默认为 BorderLayout.CENTER.

然后添加 "draw" 组件,它会替换您的面板,因为只能将一个组件添加到 "CENTER"。

尝试将三角形面板添加到 BorderLayout.NORTH。然而,当你这样做时,你还需要重写三角形面板的 getPreferredSize() 方法,否则首选大小将是 (0, 0) 并且没有任何东西可以绘制。

阅读有关 Custom Painting 的 Swing 教程部分,了解更多信息和执行此操作的示例。

编辑:

再次查看您的代码,我什至不确定您创建 "draw" 面板的原因。您不向其中添加任何组件。所以只需要去掉 "draw" 面板,让你的 "triangle" 面板显示在 BordeLayout.CENTER 中,但你仍然需要实现 getPreferredSize() 方法,否则 pack() 方法将忽略 "triangle" 面板。