【问题标题】:Having Trouble Keeping Axes Fixed While Adding Images in MATLAB在 MATLAB 中添加图像时无法保持轴固定
【发布时间】:2018-04-17 19:22:16
【问题描述】:

我目前正致力于在 MATLAB 中创建基于 GUI 的 BlackJack 游戏。我为庄家手和闲家手设置了两个轴,如下所示:

self.window = figure('Name', 'Blackjack',...
    'Units', 'normalized',...
    'Position', [.02, .05, .95, .85]);
self.dealerHand = axes('Parent', self.window,...
    'XLimMode', 'manual',...
    'XLim', [0, 10],...
    'YLimMode', 'manual',...
    'YLim', [0, 1],...
    'Title', 'Dealer',...
    'Units', 'normalized',...
    'Position', [.05, .70, .90, .25]);
self.playerHand = axes('Parent', self.window,...
    'XLimMode', 'manual',...
    'XLim', [0, 10],...
    'YLimMode', 'manual',...
    'YLim', [0, 1],...
    'Title', 'Player',...
    'Units', 'normalized',...
    'Position', [.05, .20, .90, .25]);

如您所见,我已将“手”XLim 和 YLim 模式都设置为手动模式。但是,当我运行此脚本时:

bjWindow = BlackJackWindow;
deck = DeckOfCards;
deck.shuffle
playerHand{1} = deck.cards{1};
dealerHand{1} = deck.cards{2};
playerHand{2} = deck.cards{3};
dealerHand{2} = deck.cards{4};
hold on
image(bjWindow.dealerHand, [0, 1], [0, 1], dealerHand{1}.img)
image(bjWindow.dealerHand, [1, 2], [0, 1], dealerHand{2}.img)
hold off
hold on
image(bjWindow.playerHand, [0, 1], [1, 0], playerHand{1}.img)
image(bjWindow.playerHand, [1, 2], [1, 0], playerHand{2}.img)
hold off

庄家“手”自动调整大小并拉伸第二张牌,如下所示:

如果我在第一个代码块中颠倒手牌的顺序,则错误发生在玩家“手牌”中。换句话说,在初始化窗口时首先创建的任何轴都会出现此错误。另一个不那么重要的问题,因为我找到了解决方法,为什么玩家“手”翻转轴,这样我必须将 y 位置设置为 [1, 0] 而不是 [0, 1]?

【问题讨论】:

    标签: image matlab matlab-figure


    【解决方案1】:

    hold on 影响当前坐标区,这是您创建的最后一个坐标区。将图像添加到您制作的第一个轴时,它将被重置。反转是其中的一部分,因为image 在显示图像时会反转 y 轴。

    解决办法是用

    hold(bjWindow.dealerHand,'on')
    hold(bjWindow.playerHand,'on')
    

    我建议不要在绘图后再次关闭延迟。没必要。

    相反,要更改显示的卡片,请替换 image object's CData property。这是存储像素数据的地方。通过更新这个属性,而不是创建一个新的图像对象,你的应用程序会更流畅,你不需要担心删除和重新创建图像对象,保留轴属性等。更新属性是微不足道的,因为你所有的gliphs(我猜)大小相同。

    set(image_handle,'CData',playerHand{1}.img)
    

    【讨论】:

      猜你喜欢
      • 2012-08-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-10
      • 1970-01-01
      • 2021-08-07
      • 1970-01-01
      相关资源
      最近更新 更多