Filip Piták
Filip Piták

Reputation: 9

Using drawLine Function in Other Function

I'm creating a game where I need to draw some lines and dots. I have a general function called paintDot (check code below) and I want to call it in a different function. I don't know how to call it, any help?

public void paintDot (Graphics g, int x, int y)
{
    super.paintComponent(g);
    g.setColor(Color.BLACK);
    g.fillOval(x,y,15,15);

}

This is the other function/method where I need to call the drawing function: ATM the coordinates are just hardcoded so I know it's working correctly.

As you can see, I'm calling the paintDot method with bad arguements. Don't know what argument should be placed at Graphics g

private void gameWindow (int dif)
{
    this.removeAll();

    areaImage = new JPanel ();
    //distance between points = 75
    //point grid = 7*6
    areaImage.setBounds(50,50,675,600);
    areaImage.setBackground(Color.WHITE);
    areaImage.setBorder(BorderFactory.createLineBorder(Color.black));
    add(areaImage);

    answer = new JTextField();
    answer.setBounds(835,200,150,50);
    answer.setBorder(BorderFactory.createLineBorder(Color.black));
    answer.setHorizontalAlignment(JTextField.CENTER);
    answer.setFont(new Font("Verdana", Font.BOLD, 20));
    add(answer);

    info= new JLabel ("Write your answer here:");
    info.setBounds(830,155,250,50);
    info.setFont(new Font("Verdana", Font.BOLD, 12));
    add(info);

    checkAnswer = new JButton ("Check Answer");
    checkAnswer.setBounds(835,310,150,50);
    checkAnswer.addActionListener(this);
    add(checkAnswer);

    next = new JButton ("Next");
    next.setBounds(835,410,150,50);
    next.addActionListener(this);
    add(next);

    end = new JButton ("End Game");
    end.setBounds(835,510,150,50);
    end.addActionListener(this);
    add(end);

    revalidate();
    repaint();

    int x = 75,y=75;
    for(int num=0;num<6;num++)
    {
        for(int xx=0; x<7;xx++)
        {
            paintDot (areaImage,x,y); // here is the problem
            x=x*2;
        }
        y=y*2;
    }
}

Upvotes: 0

Views: 125

Answers (3)

jD91mZM2
jD91mZM2

Reputation: 35

You could extend JPanel and overwrite the drawing functions, such as the paintComponent(Graphics g) (Thanks camickr) or paintAll(Graphics g) (I believe) method. You might also want to add a JLabel with a BufferedImage using createGraphics()

Please note if you are trying to make a full-fledged game, you would need a game loop and other stuff, which is NOT fun without a library. This is not an attempt in shameless self-promotion, it's a suggestion.

IF you need a game loop, canvases, multiple screens and stuff, a library could be the way to go. I made the library j2D to make 2D games.

Upvotes: 0

camickr
camickr

Reputation: 324147

I have a general function called paintDot (check code below) and I want to call it in a different function

You can't.

Painting can only be done in the paintComponent() method.

You should NEVER be invoking paintComponent() directly.

All painting code MUST be in the paintComponent() method.

If you want to paint 7 dots. Then that painting code MUST be in the paintComponent() method which means the looping code would be in the paintComponent() method and then you invoke the paintDot(...) method from withing the loop. The painting of the dots must be done EVERY time Swing determines the component needs to be repainted.

You have asked several questions on this topic and the answer is always the same. Read the tutorial link you have been given and follow the examples. The tutorial draws a square, but the concept would be similar for drawing 7 dots.

So once again, read the tutorial, download the code and play with the working example. Start by changing the tutorial code to draw 7 dots. Once you understand how that works, then you add the logic to your real code.

The tutorial link is give to you for a reason. If there is something you don't understand in the tutorial, then ask a question, but don't post code that looks nothing like the example from the tutorial and wonder why it doesn't work!

Upvotes: 2

Lajos Arpad
Lajos Arpad

Reputation: 76737

You have this for:

for(int xx=0; x<7;xx++)

where you have an index called xx and you try to do a cycle. The problem is that you test for x < 7 instead of xx < 7 and since x is greater than 7, you will never get into the for.

Upvotes: 1

Related Questions