【问题标题】:Having Trouble on getting showing shape on frame无法在框架上显示形状
【发布时间】:2015-03-10 02:28:56
【问题描述】:

我设法让单独的模式显示在面板上。但现在我想创建一个 Token 对象(这是一个矩形内的 2 条线)。我想在面板中创建令牌。使用令牌参数,我想创建一个将图案包围在其中的矩形。游戏代币将创建模式,而模式又将创建特定模式。在这种情况下,交叉模式。 我究竟做错了什么?是我初始化事物的方式吗?

这是我创建的模式

public  class CrossPattern  extends JPanel{
private Point2D.Double top;
private Point2D.Double bottom;
private Point2D.Double top1;
private Point2D.Double bottom1;
private Line2D.Double line1;
private Line2D.Double line2;
private Rectangle bbox;
public CrossPattern() 
{

     // correct  Cross pattern
     top= new Point2D.Double(bbox.getX(),bbox.getY()) ; 
     bottom= new Point2D.Double(bbox.getX()+bbox.getWidth(),bbox.getY()+bbox.getWidth());  // correct Cross Pattern

     top1= new Point2D.Double(bbox.getX()+bbox.getWidth(),bbox.getY());  // Correct cross Patt
     bottom1= new Point2D.Double(bbox.getX(),bbox.getY()+bbox.getHeight());   // correct Cross Patt

     line1= new Line2D.Double(top,bottom);   // correct Cross Patt
     line2= new Line2D.Double(top1,bottom1);   // Correct cross patt




}
public void draw(Graphics2D g2)
{


g2.draw(this.line1);
g2.draw(this.line2);


}



}

这是在盒子的尺寸内创建特定图案的图案。

public class Pattern 
{


  private CrossPattern pattern1;

  int type;

Random random = new Random();



  public  Pattern(Rectangle bbox)
  {
  int num= 0; //set random num a int value from 0-2
  bbox = bbox;

  if(num==0)
  {
   pattern1 = new CrossPattern();

}

这是我将在其中实现位置和大小的游戏令牌

public class GameToken implements VisibleShape
{
  private boolean visible;
  public    Rectangle bbox;
  private Pattern pattern;
  private   Color color;
  Random random = new Random();

   public GameToken(int x, int y, int width, int height) 
   {
    bbox = new Rectangle ( x,y,width,height); 
   pattern= new Pattern(bbox); 


  }

这是我将创建令牌的面板

public class GameTokenPanel extends JPanel {
private CrossPattern patternt;
private Rectangle rect;
private GameToken token1;
public GameTokenPanel()
{

 token1= new GameToken(50,50,25,25);
repaint();

}
public void paint(Graphics g)
{


super.paint(g);
Graphics2D g2= (Graphics2D) g;

patternt.draw(g2);

}




}

【问题讨论】:

  • 1) 使用逻辑一致的缩进代码行和块的形式。缩进是为了让代码流更容易理解! 2) 曾经只需要源代码中的一个空白行。 { 之后或} 之前的空行通常也是多余的。
  • 另外,请查看your earlier questions 的一些内容,看看是否有您可以accept 的答案。

标签: java swing panel frame shape


【解决方案1】:

您的代码在g2.draw(this.line2); 处生成编译器异常,因为line2 未定义,而NullPointerExceptiong2.draw(this.line1); 处生成,因为line1(并假设line2)未初始化,因为您已隐藏你的变量...

private static class CrossPattern {
    //...
    private Line2D.Double line1;

    public CrossPattern() {
        // Oh, look, you've redefined `line1` as a local variable!
        Line2D.Double line1 = new Line2D.Double(top, bottom);   // correct 
        //...

摆脱line1topbottomtop1bottom1line2的重新清除...

除非您有特别好的理由这样做,否则您应该在 GameTokenPanel 中覆盖 paintComponent 而不是 paint

您应该覆盖GameTokenPanelgetPreferredSize 方法并返回您想要的首选“视图”大小,而不是使用FRAME_WIDTHFRAME_HEIGHT。目前you组件的可视区域为FRAME_WIDTH - frame decorations horizontal insetsFRAME_HEIGHT - frame decorations vertical insets,可能达不到你的预期。

话虽如此,您应该传递patternt 组件的当前大小,以便它可以决定如何最好地呈现模式...

【讨论】:

    猜你喜欢
    • 2021-11-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-11
    • 2015-11-18
    • 2013-11-13
    • 2012-12-17
    • 2018-11-11
    相关资源
    最近更新 更多