【发布时间】: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