我正在努力展示我想要绘制的三角形。我已经浏览了网页以及我发现的许多选项,这些选项已经解决了我的显示问题。我的按钮面板显示完美但我的绘图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;
}
}
}
答案 0 :(得分:1)
win.add(this);
win.add(draw, BorderLayout.CENTER);
您可以将三角形面板添加到窗口中。如果未指定约束,则默认为BorderLayout.CENTER。
然后添加“draw”组件,它取代了您的面板,因为只有一个组件可以添加到“CENTER”。
尝试将三角形面板添加到BorderLayout.NORTH
。但是,当您执行此操作时,您还需要覆盖三角形面板的getPreferredSize()
方法,否则首选大小将为(0,0),并且不会绘制任何内容。
阅读Custom Painting上Swing教程中的部分,了解更多信息以及执行此操作的示例。
编辑:
再看看你的代码,我甚至不确定为什么你创建了“绘图”面板。您不添加任何组件。所以只需摆脱“绘图”面板并让你的“三角形”面板显示在BordeLayout.CENTER中,但你仍然需要实现getPreferredSize()
方法,否则pack()方法将忽略“三角形”面板