Vlad Tura
Vlad Tura

Reputation: 13

C# draws my image too large and regardless of mouse position

I'm currently working on a battleships game,but i've ran into an issue.So far i've managed to draw the grid.the purpose of the draw method is to draw an image(don't know how to/if i can color a certain surface) inside a square in the grid,when i left-click. the problem here is that,even if the image's size is 25x25(the size of a square) it occupies like half the screen,and that's when it works.50% of the times when i run nothing happens,and the other 50% it draws a huge image in the middle of the screen,regardless of where the cursor is located or if i left-click.

public partial class Form1 : Form
{

    public Form1()
    {
        InitializeComponent();
        this.Paint += new PaintEventHandler(form1_paint);
    }
    private void form1_paint(object sender, PaintEventArgs e)
    {
        draw(e);
    }
    int x,y;
    private void draw(PaintEventArgs e)
    {

        if (MouseButtons.Left != 0)
        {
            x = Cursor.Position.X;
            y = Cursor.Position.Y;
            Image poza = Image.FromFile("D://C//12E//c#//yellow4.jpg");
            if (x < 301 && x > 24 && y < 301 && y > 24)
            {
                PointF coltz = new PointF(x / 25 * 25, y / 25 * 25);
                e.Graphics.DrawImage(poza, coltz);
            }
        }

    }

Does anyone know how i can solve this?or if someone has a better idea for a battleships grid, I am open to suggestions.Thanks!

Upvotes: 1

Views: 179

Answers (2)

Vlad Tura
Vlad Tura

Reputation: 13

the thing is,before adding your code,the draw worked fine.now,every time i move the mouse,the grid gets redrawn,and it looks like it's constantly refreshing,bleeping somehow.not sure how to phrase this

public Form1()
    {
        InitializeComponent();
        Paint += Form1_Paint;
        MouseMove += Form1_MouseMove;
        MouseDown += Form1_MouseMove;
    }

    private int x,y;

    void Form1_Paint(object sender, PaintEventArgs e)
    {
        Draw(e);
    }

    private void Draw(PaintEventArgs e)
    {
        if (MouseButtons == System.Windows.Forms.MouseButtons.Left)
        {
            if (x < 301 && x > 24 && y < 301 && y > 24)
            {
                PointF coltz = new PointF(x / 25 * 25, y / 25 * 25);
                e.Graphics.DrawImage(battleships.Properties.Resources.yellow4, coltz);
            }
        }
    }

    private void Form1_MouseMove(object sender, MouseEventArgs e)
    {
        x = e.X;
        y = e.Y;
        Invalidate();
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);
        Graphics g;

        g = e.Graphics;

        Pen pen = new Pen(Color.Black);
        pen.Width = 1;
        for (int i = 25; i <= 300; i = i + 25)
        {
            g.DrawLine(pen, i, 25, i, 300);
            g.DrawLine(pen, 25, i, 300, i);
        }

    }
}

Upvotes: 0

Łukasz Rejman
Łukasz Rejman

Reputation: 1892

Fisrt of all, this line of code: Cursor.Position.X gives you the global position of the cursor on the screen, not in the game window. I suggest you to handle MouseMove event to get the position relative to the content of your app.

The second thing is that you are loading the image from file on your computer. I think it's better to add the image to your app's resources, so you can load it easier just calling it by name, e.g.: AppName.Properties.Resources.ImageName - it returns Image object you can immediately use.

One more thing. This if (MouseButtons.Left != 0) won't check whether left mouse button is pressed or not. You have to check if MouseButtons property equals System.Windows.Forms.MouseButtons.Left.

Here's the full code that works for me:

public partial class Form1 : Form
{
    private int x, y;

    public Form1()
    {
        InitializeComponent();
        Paint += Form1_Paint;
        MouseMove += Form1_MouseMove;
        MouseDown += Form1_MouseMove;
    }

    void Form1_Paint(object sender, PaintEventArgs e)
    {
        Draw(e);
    }

    private void Draw(PaintEventArgs e)
    {
        if (MouseButtons == System.Windows.Forms.MouseButtons.Left)
        {
            if (x < 301 && x > 24 && y < 301 && y > 24)
            {
                PointF coltz = new PointF(x / 25 * 25, y / 25 * 25);
                e.Graphics.DrawImage(AppName.Properties.Resources.ImageName, coltz);
            }
        }
    }

    private void Form1_MouseMove(object sender, MouseEventArgs e)
    {
        x = e.X;
        y = e.Y;
        Invalidate();
    }
}

And here's the result:
Game

I also subscribed MouseDown event to show yellow rectangle when user clicks the button without moving the cursor.

Upvotes: 2

Related Questions