OnPaint problem

10110010

Mitglied
Hallo leute,

Ich habe in meinem Project die OnPaint Methode überschrieben.

Das problem ist nur, dass der dabei nie zum ende kommt, da er immer wieder von forne beginnt.

MFG 01

Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Mandelbrot
{
    public partial class Form1 : Form
    {
        private bool painted = false;
        private const int MaxIterations = 50;

        //Region
        private const float Min_X = -2.2F;
        private const float Max_X = 1F;
        private const float Min_Y = -1.2F;
        private const float Max_Y = 1.2F;
        
        //Startpunkt
        private float X_Min = Min_X;
        private float X_Max = Max_X;
        private float Y_Min = Min_Y;
        private float Y_Max = Max_Y;

        public Form1()
        {
            InitializeComponent();
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            if (!painted)
            {
                base.OnPaint(e);
                painted = true;

                const float Max_Squared = 2.0F;

                GetProportion();

                int count;
                float XC, YC, dXC, dYC, XZ, YZ, XZ2, YZ2;

                int wi = this.Width;
                int hi = this.Height;
                dXC = (X_Max - X_Min) / (wi - 1);
                dYC = (Y_Max - Y_Min) / (hi - 1);

                XC = X_Min;
                for (float i = 0F; i <= wi - 1; i++)
                {
                    YC = Y_Min;
                    for (float j = 0F; j <= hi - 1; j++)
                    {
                        XZ = 0;
                        YZ = 0;
                        XZ2 = 0;
                        YZ2 = 0;
                        count = 0;
                        while (count < MaxIterations && XZ < Max_Squared && YZ < Max_Squared)
                        {
                            XZ2 = XZ * XZ;
                            YZ2 = YZ * YZ;
                            YZ = 2 * YZ * XZ + YC;
                            XZ = XZ2 - YZ2 + XC;
                            count++;
                        }
                        //Color C = Color.FromArgb(count, count, count);
                        //Pen P=new Pen(C);
                        Pen P = new Pen(Color.Black);
                        if (!(count < MaxIterations))
                        {
                            e.Graphics.DrawLine(P, i, j, i + 0.1F, j + 0.1F);
                        }

                        YC = YC + dYC;
                    }
                    XC = XC + dXC;
                }
                painted = false;
            }
        }

        private void GetProportion()
        {
            float Proportion, FormProportion;
            float hi, wi, mid;

            Proportion = (Y_Max - Y_Min) / (X_Max - X_Min);
            FormProportion = float.Parse(this.Height.ToString()) / float.Parse(this.Width.ToString());
            if (Proportion > FormProportion)
            {
                wi = (Y_Max - Y_Min) / FormProportion;
                mid = (X_Min + X_Max) / 2F;
                X_Min = mid - wi / 2F;
                X_Max = mid + wi / 2F;
            }
            else
            {
                hi = (X_Max - X_Min) * FormProportion;
                mid = (Y_Min + Y_Max) / 2F;
                Y_Min = mid - hi / 2F;
                Y_Max = mid + hi / 2F;
            }
        }

        private void Form1_Resize(object sender, EventArgs e)
        {
            this.Invalidate();
        }
    }
}
 
Hallo,

du sagst beim Einstieg deiner Funktion " painted = true;" aber überschreibst es am Ende wieder mit " painted = false;" somit ist kein Abbruch möglich und "if (!painted)" ist immer gegeben. Ein Rückgabeparameter wäre nicht schlecht.

Code:
protected override bool OnPaint(PaintEventArgs e)

return painted;

Gruss
 
Hi

Hab deinen Code grad kopiert. Das Programm funktioniert ohne Probleme. Der Code wird einmal ausgeführt, wenn kein weiteres Zeichnen von Nöten ist.

Wie hast du denn festgestellt, das das Paint mehrfach ausgeführt wird?
 
Und die Variable painted ist sinnlos.

Solltest du diese am Ende doch mal auf true setzen :D , verschwindet dein gezeichnes eh bei der nächsten Aufforderung zum neuzeichnen des Formulars.

Wenn du diesen Zustand also aus irgend einem Grund halten musst, zeichne auf ein Bitmap.

Code:
Bitmap bmp=new Bitmap(width, height)
using(Graphics g=Graphics.FromImage(bmp))
{
   g.Draw(....); //zeichnen

   g.Flush(); //Zeichen Operationen ausführen
}

form.BackGroundImage=bmp;
 
Hallo leute,

habe das Painted herrausgenommen.

Das Property "DoubleBuffered = true;" hat mir gehollfen, da das Bild erst berechnet wird befor es angezeigt bekommt.

Hier der ganze Quellcode:

Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Mandelbrot
{
    public partial class Form1 : Form
    {
        private const int MaxIterations = 255;

        //Region
        private const float Min_X = -2.2F;
        private const float Max_X = 1F;
        private const float Min_Y = -1.2F;
        private const float Max_Y = 1.2F;
        
        //Startpunkt
        private float X_Min = Min_X;
        private float X_Max = Max_X;
        private float Y_Min = Min_Y;
        private float Y_Max = Max_Y;

        public Form1()
        {
            DoubleBuffered = true;
            InitializeComponent();
        }

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

            const float Max_Squared = 2.0F;

            GetProportion();

            int count;
            float XC, YC, dXC, dYC, XZ, YZ, XZ2, YZ2;

            int wi = this.Width;
            int hi = this.Height;
            dXC = (X_Max - X_Min) / (wi - 1);
            dYC = (Y_Max - Y_Min) / (hi - 1);

            XC = X_Min;
            for (float i = 0F; i <= wi - 1; i++)
            {
                YC = Y_Min;
                for (float j = 0F; j <= hi - 1; j++)
                {
                    XZ = 0;
                    YZ = 0;
                    XZ2 = 0;
                    YZ2 = 0;
                    count = 0;
                    while (count < MaxIterations && XZ < Max_Squared && YZ < Max_Squared)
                    {
                        XZ2 = XZ * XZ;
                        YZ2 = YZ * YZ;
                        YZ = 2F * YZ * XZ + YC;
                        XZ = XZ2 - YZ2 + XC;
                        count++;
                    }
                    Color C = Color.FromArgb(10, count, 10);
                    Pen P = new Pen(C);
                    //Pen P = new Pen(Color.Black);
                    //if (!(count < MaxIterations))
                    //{
                    e.Graphics.DrawLine(P, i, j, i + 0.1F, j + 0.1F);
                    //}

                    YC = YC + dYC;
                }
                XC = XC + dXC;
            }
        }

        private void GetProportion()
        {
            float Proportion, FormProportion;
            float hi, wi, mid;

            Proportion = (Y_Max - Y_Min) / (X_Max - X_Min);
            FormProportion = float.Parse(this.Height.ToString()) / float.Parse(this.Width.ToString());
            if (Proportion > FormProportion)
            {
                wi = (Y_Max - Y_Min) / FormProportion;
                mid = (X_Min + X_Max) / 2F;
                X_Min = mid - wi / 2F;
                X_Max = mid + wi / 2F;
            }
            else
            {
                hi = (X_Max - X_Min) * FormProportion;
                mid = (Y_Min + Y_Max) / 2F;
                Y_Min = mid - hi / 2F;
                Y_Max = mid + hi / 2F;
            }
        }

        private void Form1_Resize(object sender, EventArgs e)
        {
            this.Invalidate();
        }
    }
}

Aber wenn ich das Propperty herrausnehme kann er das nicht zuende Zeichnen.
Das ist allerdings nur bei mir. Auf der Arbeit funktioniert alles perfect!

MFG 01
 
Zurück