Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
07/02/15 13:52:10 (9 years ago)
Author:
ascheibe
Message:

#2306 improved BoolMatrix View

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/FitnessLandscapeAnalysis/HeuristicLab.Problems.NK.Views/BoolMatrixBitmapView.cs

    r7141 r12574  
    1 using System;
    2 using System.Collections.Generic;
     1#region License Information
     2/* HeuristicLab
     3 * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
     4 *
     5 * This file is part of HeuristicLab.
     6 *
     7 * HeuristicLab is free software: you can redistribute it and/or modify
     8 * it under the terms of the GNU General Public License as published by
     9 * the Free Software Foundation, either version 3 of the License, or
     10 * (at your option) any later version.
     11 *
     12 * HeuristicLab is distributed in the hope that it will be useful,
     13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
     14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15 * GNU General Public License for more details.
     16 *
     17 * You should have received a copy of the GNU General Public License
     18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
     19 */
     20#endregion
     21
     22using System;
    323using System.ComponentModel;
    424using System.Drawing;
    5 using System.Data;
    6 using System.Linq;
    7 using System.Text;
    8 using System.Windows.Forms;
    9 using HeuristicLab.MainForm;
    1025using HeuristicLab.Core.Views;
    1126using HeuristicLab.Data;
    12 using System.Threading;
     27using HeuristicLab.MainForm;
    1328
    1429namespace HeuristicLab.Problems.NK {
    1530
    16   [View("BoolMatrixBitmapView")]
     31  [View("BoolMatrixView")]
    1732  [Content(typeof(BoolMatrix), IsDefaultView = false)]
    1833  public sealed partial class BoolMatrixView : ItemView {
     34    private BackgroundWorker worker;
    1935
    2036    public new BoolMatrix Content {
     
    2541    public BoolMatrixView() {
    2642      InitializeComponent();
    27     }   
     43    }
    2844
    29     protected override void DeregisterContentEvents() {     
    30       Content.ToStringChanged -= new EventHandler(Content_ToStringChanged);
     45    protected override void DeregisterContentEvents() {
     46      Content.ToStringChanged -= Content_ToStringChanged;
    3147      base.DeregisterContentEvents();
    32     }   
     48    }
    3349
    3450    protected override void RegisterContentEvents() {
    3551      base.RegisterContentEvents();
    36       Content.ToStringChanged += new EventHandler(Content_ToStringChanged);
     52      Content.ToStringChanged += Content_ToStringChanged;
    3753    }
    3854
     
    4359    #endregion
    4460
    45     private BackgroundWorker worker;
    4661    private void Rebuild() {
     62      int width = pictureBox.Width;
     63      int height = pictureBox.Height;
     64      int tileWidth = width / Content.Columns;
     65      int tileHeight = height / Content.Rows;
     66      int border = (int)Math.Round((tileWidth + tileHeight) / 2.0 * 0.05);
     67      var bitmap = new Bitmap(width, height);
     68
    4769      if (worker != null)
    4870        worker.CancelAsync();
     71
     72      if (tileWidth == 0 || tileHeight == 0) {
     73        using (Graphics g = Graphics.FromImage(bitmap)) {
     74          g.DrawString("BoolMatrix is to big to draw.", DefaultFont, new SolidBrush(Color.Black), 10.0f, height / 2.0f);
     75          g.Flush();
     76        }
     77        pictureBox.Image = bitmap;
     78        return;
     79      }
     80
    4981      worker = new BackgroundWorker();
    5082      worker.WorkerSupportsCancellation = true;
    51       Thread.Sleep(1);
    52       var bitmap = new Bitmap(Content.Rows*4, Content.Columns*4);     
    5383      worker.DoWork += (s, a) => {
    5484        using (Graphics g = Graphics.FromImage(bitmap)) {
    55           for (int i = 0; i<Content.Rows; i++) {
    56             for (int j = 0; j<Content.Columns; j++) {
     85          for (int i = 0; i < Content.Rows; i++) {
     86            for (int j = 0; j < Content.Columns; j++) {
    5787              if (worker.CancellationPending) {
    5888                a.Cancel = true;
    5989                break;
    6090              }
    61               g.FillRectangle(Content[i, j] ? Brushes.Green : Brushes.Red,
    62                 i*4, j*4, 3, 3);             
     91              g.FillRectangle(Content[i, j] ? Brushes.Black : Brushes.White, i * tileWidth, j * tileHeight,
     92                                                                    tileWidth - border, tileHeight - border);
    6393            }
    6494          }
     
    78108      base.OnContentChanged();
    79109      if (Content == null) {
    80         pictureBox.Image = new Bitmap(1, 1);       
     110        pictureBox.Image = new Bitmap(1, 1);
    81111      } else {
    82112        Rebuild();
    83113      }
    84114    }
    85 
    86     protected override void SetEnabledStateOfControls() {
    87       base.SetEnabledStateOfControls();     
    88     }
    89 
    90     #region Event Handlers (child controls)   
    91     #endregion
    92115  }
    93116}
Note: See TracChangeset for help on using the changeset viewer.