首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >paintComponent问题显示

paintComponent问题显示
EN

Stack Overflow用户
提问于 2015-04-10 00:55:04
回答 1查看 26关注 0票数 0

我现在很难展示我想画的三角形。我已经浏览过网络和许多我发现我实现的没有解决我的显示问题的选项。我的按钮面板显示得很好,但是我用于绘图的JPanel要么没有被使用,要么没有被绘制。你们能提供的任何帮助都会很棒,我已经盯着看了几天了,还没有任何运气。我有更多的代码要实现,但我希望在添加太多代码之前让代码基本运行。这是我的代码,谢谢你提前提供帮助。

代码语言:javascript
复制
    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;
     } 
}

}

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-04-10 00:58:52

代码语言:javascript
复制
win.add(this);
win.add(draw, BorderLayout.CENTER);

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

然后添加“绘制”组件,它将替换面板,因为只有一个组件可以添加到“中心”。

尝试将三角形面板添加到BorderLayout.NORTH中。但是,当您这样做时,您还需要覆盖三角形面板的getPreferredSize()方法,否则首选的大小将是(0,0),并且没有什么可绘制的。

有关此操作的更多信息和示例,请参阅有关定制绘画的Swing教程中的部分。

编辑:

再看一看你的代码,我甚至不知道你为什么要创建“绘图”面板。你不给它添加任何组件。因此,只需去掉“绘制”面板,让“三角形”面板在BordeLayout.CENTER中显示,但是仍然需要实现getPreferredSize()方法,否则pack()方法将忽略“三角形”面板。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29551569

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档