Reputation: 11
I managed to get the individual pattern to show on the panel. But now I would like to create a Token object ( which is the 2 lines inside a rectangle). I would like to create the Token in Panel. Using the token parameters, I would like to create a rectangle that will enclose the pattern in it. The game token will create the pattern which in turn will create the specific pattern. In this case the Cross Pattern. What am I doing wrong? Is it the way I initialized things?
Here's the pattern I created
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);
}
}
Here's the pattern that will create the specific pattern within the dimensions of the box.
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();
}
Here's my game token where I'll implement the location and size
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);
}
And here is the panel where I'll be creating the token
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);
}
}
Upvotes: 0
Views: 36
Reputation: 347332
Your code generates a compiler exception at g2.draw(this.line2);
, because line2
is undefined and a NullPointerException
at g2.draw(this.line1);
because line1
(and assume line2
) aren't initalised, because you've shadowed your variables...
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
//...
Get rid of the redecleartion of the line1
, top
, bottom
, top1
, bottom1
and line2
...
Unless you have a particularly good reason for doing so, you should override paintComponent
instead of paint
in your GameTokenPanel
.
Instead of using FRAME_WIDTH
and FRAME_HEIGHT
, you should override the getPreferredSize
method of GameTokenPanel
and return the preferred "view" size you want. Currently, the viewable area of the you component will be FRAME_WIDTH - frame decorations horizontal insets
and FRAME_HEIGHT - frame decorations vertical insets
, which may not meet you expectations.
Having said that, you should be passing patternt
the current size of the component, so it can make decisions about how best to render the pattern...
Upvotes: 2