java - PaintComponent 显示问题

标签 java jpanel paintcomponent graphics2d

我在尝试显示我要绘制的三角形时遇到了麻烦。我浏览了网络,发现我实现的许多选项都没有解决我的显示问题。我的按钮面板显示完美,但用于绘图的 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);

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

然后添加“draw”组件,该组件将替换您的面板,因为“CENTER”中只能添加一个组件。

尝试将三角形面板添加到BorderLayout.NORTH。但是,当您执行此操作时,您还需要重写三角形面板的 getPreferredSize() 方法,否则首选大小将为 (0, 0) 并且将没有任何内容可绘制。

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

编辑:

再看一下您的代码,我什至不确定您为什么创建“绘图”面板。您无需向其中添加任何组件。因此,只需摆脱“绘制”面板并让“三角形”面板显示在 BordeLayout.CENTER 中,但您仍然需要实现 getPreferredSize() 方法,否则 pack() 方法将忽略“三角形”面板。

关于java - PaintComponent 显示问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29551569/

相关文章:

java - Spring Integration Java DSL — 如何配置 JSON 转换器以采用 Spring boot 的全局设置?

java - 按下 JButton 后将 JTable 放置在 JPanel 上

java - 为中国象棋程序制作棋盘图形用户界面

java - 练习paintComponent方法

java - 用动态值替换 XSLT?

java - 尝试访问数组后出现 ArrayIndexOutOfBoundsException

java - 使用 Tuckey 的 URL 重写过滤器向 URL 添加尾部斜杠

java - 以编程方式放大 JFrame/JPanel

java - 如何在 JPanel 中快速更新图形

java - 为什么我的面板不抽牌?