0

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;
    }
}
Nathan
  • 8,093
  • 8
  • 50
  • 76
  • Basically I need to make a grid of dots (see comment in code above) – Filip Piták Aug 27 '16 at 08:33
  • 1) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson Aug 27 '16 at 08:37
  • 1
    The basic approach to this type of problem is to create a `Dot` class that knows how (and where) to draw itself. Maintain a collection or array of dots. In the `paintComponent(Graphics)` method, iterate the list and call a `Dot.draw(Graphics)` method to draw each in turn. – Andrew Thompson Aug 27 '16 at 08:40

3 Answers3

2

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!

camickr
  • 321,443
  • 19
  • 166
  • 288
  • Hi, i changed the code so all of it is in one function. ATM I dont really understand, but I've fixed the function (private void paintDotGrid (Graphics g)) but when my question is how do activate this function to draw the Dot grid? E.g. in the same class but other function or constructor. – Filip Piták Sep 06 '16 at 20:37
  • @FilipPiták, you can invoke your paintDot from paintComponent. paintComponent already receives a Graphics object (https://docs.oracle.com/javase/7/docs/api/javax/swing/JComponent.html#paintComponent(java.awt.Graphics)) so pass that object to your method. If you call your method from paintComponent with that g you see in the parameter list and pass the actual x and y you intend to pass, then you should be ok. – Lajos Arpad Sep 07 '16 at 07:08
1

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.

Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
  • This isnt the problem, but i am aware of this – Filip Piták Sep 06 '16 at 20:24
  • @FilipPiták, I believe you this is not the problem, but this is a problem to be fixed as well. In fact, camickr's answer with mine almost gives you the full answer. I use the word "almost", since you still have a question, but I will address it on the comment section below the other answer. – Lajos Arpad Sep 07 '16 at 07:04
0

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.

Community
  • 1
  • 1
jD91mZM2
  • 25
  • 2
  • 8